Out-of-Memory errors put the WASM Instance into invalid state in Cloudlare Workers runtime
Summary
We're running automerge which is a rust library compiled to WASM with wasm-bingen bindings. Cloudflare Workers runtime is limited to 128MB of RAM. After a memory allocation fails, the WebAssembly.Memory instance gets put into a "broken state" (I'm a bit out of my depth here).
Effectively, after a failed allocation the WASM isolate becomes unusable.
Currently we work around that by patching wasm-bindgen generrated JS glue code, to allow us to re-create the WebAssembly.Instance.
- Is there a better way to recover after a failed allocation?
- Could wasm-bindgen gracefully handle OOM errors?
- If it's not possible or not practical to handle OOM errors at wasm-bindgen level, could the API to re-initialize
WebAssembly.Instancebe exposed so we don't need to patch?
Additional Details
There are more details, as well as a repro repository in this thread with the Cloudflare Team.
https://github.com/cloudflare/workerd/issues/3264
Ping. We're still affected by this. The current workaround involves patching build artifacts and re-intializing the library manually.
The characteristic error message in cloudflare-workers is Invalid typed array length: undefined for anyone that stumbles onto this.
Note that the latest worker-build now uses the new experimental reinitialization support implemented in wasm bindgen. We are also working on unwind support with this now building as of the latest release.