Skip to content

Commit 8f9b3e9

Browse files
authored
Further tweaks
1 parent fd85278 commit 8f9b3e9

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/content/changelog/workers/2025-09-19-workers-rs-panic-recovery.mdx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { WranglerConfig, Aside } from "~/components";
88

99
In [workers-rs](https://github.com/cloudflare/workers-rs), Rust panics were previously non-recoverable. A panic would put the Worker into an invalid state, and further function calls could result in memory overflows or exceptions.
1010

11-
Now, when a panic occurs, requests in progress during a panic will throw 500 errors, but the Worker will then automatically and instantly recover for future requests.
11+
Now, when a panic occurs, in-flight requests will throw 500 errors, but the Worker will automatically and instantly recover for future requests.
1212

13-
This ensures more reliable deployents. Automatic panic recovery feature is enabled for all new workers-rs deployments as of version 0.6.5, with no further configuration required.
13+
This ensures more reliable deployents. Automatic panic recovery is enabled for all new workers-rs deployments as of version 0.6.5, with no configuration required.
1414

1515
## Fixing Rust Panics with Wasm Bindgen
1616

17-
Rust Workers are built with Wasm Bindgen, which treats panics as non-recoverable - the entire Wasm application is considered to be in an invalid state.
17+
Rust Workers are built with Wasm Bindgen, which treats panics as non-recoverable. After a panic, the entire Wasm application is considered to be in an invalid state.
1818

1919
We now attach a default panic handler in Rust:
2020

@@ -33,21 +33,20 @@ setPanicHook(function (err) {
3333
});
3434
```
3535

36-
When a panic occurs, we then reset the Wasm state to reinitialize the Wasm application back to as it was when the application first started.
36+
When a panic occurs, we reset the Wasm state to revert the Wasm application to how it was when the application started.
3737

3838
## Resetting VM State in Wasm Bindgen
3939

4040
We worked upstream on the Wasm Bindgen project to implement a new [`--experimental-reset-state-function` compilation option](https://github.com/wasm-bindgen/wasm-bindgen/pull/4644) which outputs a new `__wbg_reset_state` function.
4141

42-
This function clears all internal state related to the Wasm VM, and also ensures object references are uniquely associated with the Wasm instance identity. Wasm bindgen exports stateless JS wrapper functions which call into Wasm. Updating their internal
43-
Wasm instance binding to the new instance allows exposing the new Wasm instance without having to rebind the exported functions.
42+
This function clears all internal state related to the Wasm VM, and updates all function bindings in place to reference the new WebAssembly instance.
4443

4544
One other necessary change here was associating Wasm-created JS objects with an instance identity. If a JS object created by an earlier instance is then passed into a new instance later on, a new "stale object" error is specially thrown when using this feature.
4645

4746
## Layered Solution
4847

49-
Building on this new Wasm Bindgen feature, layered with our new default panic handler, the last piece was adding a proxy wrapper to ensure all top-level exported class instantiations (such as for Rust Durable Objects) are fully reinitialized when resetting the Wasm instance. This is because
50-
the workerd runtime will instantiate exported classes, which would then be associated with the Wasm instance. So tracking and reinitializing these exported classes was necessary.
48+
Building on this new Wasm Bindgen feature, layered with our new default panic handler, we also added a proxy wrapper to ensure all top-level exported class instantiations (such as for Rust Durable Objects) are tracked and fully reinitialized when resetting the Wasm instance. This was necessary because
49+
the workerd runtime will instantiate exported classes, which would then be associated with the Wasm instance.
5150

5251
This approach now provides full panic recovery for Rust Workers on subsequent requests.
5352

0 commit comments

Comments
 (0)