Cloudflare workers and webassembly "script exceeded time limit" when reusing object

I have a worker in development that uses web assembly (compiled from rust).

To avoid excess CPU, I tried using a global variable to reuse a struct setup in a preview request. The worker now fails with “script exceeded time limit”.

The struct should be extremely small (in theory, I know wasm can in inefficient with memory) so this is very surprising.

There’s a PR showing the change at Switch to JSX parsing using typescript by samuelcolvin · Pull Request #1 · samuelcolvin/edgerender · GitHub

Can anyone explain this or point me to how to fix this?

Feel free to reply here or on github.

Update: I’m guessing this is because the ENV object in javascript only stores a pointer to the object in webassembly, although that js object is stored and available on the second request, the associated struct in webassembly has been destroyed. So accessing the env the second time causes the equivalent of a segfault and the worker crashes with the above error.

Is that correct? If so is there any way to tell prevent the wasm struct from being destroyed between requests?

Update 2: I see the error is happening on await wasm_bindgen(wasm), not when accessing ENV.

This was solved by switching form mode = "rust" to webpack and wasm-pack-plugin, see here.

Shame no support is given through this forum through.

This is a user forum, there is no formal support given. There is no guarantee that someone knows an answer and that everyone sees each post. I didn’t see this, for example, until now and I wouldn’t have even tried replying since I don’t know rust especially in conjunction with WASM and Workers.

2 Likes