You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
10
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.
12
12
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.
14
14
15
15
## Fixing Rust Panics with Wasm Bindgen
16
16
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.
18
18
19
19
We now attach a default panic handler in Rust:
20
20
@@ -33,21 +33,20 @@ setPanicHook(function (err) {
33
33
});
34
34
```
35
35
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.
37
37
38
38
## Resetting VM State in Wasm Bindgen
39
39
40
40
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.
41
41
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.
44
43
45
44
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.
46
45
47
46
## Layered Solution
48
47
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.
51
50
52
51
This approach now provides full panic recovery for Rust Workers on subsequent requests.
0 commit comments