wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Out-of-Memory errors put the WASM Instance into invalid state in Cloudlare Workers runtime

Open dmaretskyi opened this issue 6 months ago • 3 comments

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.Instance be 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

dmaretskyi avatar Jun 26 '25 10:06 dmaretskyi

Ping. We're still affected by this. The current workaround involves patching build artifacts and re-intializing the library manually.

dmaretskyi avatar Dec 01 '25 09:12 dmaretskyi

The characteristic error message in cloudflare-workers is Invalid typed array length: undefined for anyone that stumbles onto this.

dmaretskyi avatar Dec 01 '25 09:12 dmaretskyi

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.

guybedford avatar Dec 01 '25 13:12 guybedford