Skip to content

Commit 0f897d9

Browse files
committed
feedback
1 parent dcb8db3 commit 0f897d9

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
---
22
title: Panic Recovery for Rust Workers
3-
description: Making workers-rs deployments more reliable with panic recovery
3+
description: Deployments on workers-rs are now more reliable with improved panic recovery
44
date: 2025-09-19
55
---
66

77
import { WranglerConfig, Aside } from "~/components";
88

99
A long-standing problem we've had with [workers-rs](https://github.com/cloudflare/workers-rs) is that its Rust panics are non-recoverable, and can put the worker into an invalid state.
1010

11-
Working on the [wasm-bindgen](https://github.com/wasm-bindgen/wasm-bindgen) internals, we've been able to implement a solution for panic recovery to ensure more reliable deployments.
11+
We've been able to implement a solution for panic recovery to ensure more reliable deployments. This new automatic panic recovery feature is enabled for all new workers-rs deployments, as of version 0.6.5, with no further configuration required.
1212

1313
## Fixing Rust Panics with Wasm Bindgen
1414

15-
We use [wasm-bindgen](https://github.com/wasm-bindgen/wasm-bindgen) to build and deploy Rust workers, but Wasm Bindgen is not designed to support recoverable panics.
15+
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, and any further function calls could result in overflows or memory exceptions.
1616

17-
When a panic happens in Wasm Bindgen, the entire Wasm application is considered to be in an invalid state, and any further function calls could result in overflows or memory exceptions.
18-
19-
But Wasm is a virtual machine - if we can reset all the Wasm internal state back to its initial state then we can continue to take new requests.
20-
21-
It's possible to add a panic handler in Rust to detect immediately when a panic happens:
17+
We now attach a default panic handler in Rust:
2218

2319
```rust
2420
std::panic::set_hook(Box::new(move |panic_info| {
2521
hook_impl(panic_info);
2622
}));
2723
```
2824

29-
With some extra wiring we can then export a registration function to JavaScript to allow a custom panic hook to be attached:
25+
Which is registered by default in the JS initialization:
3026

3127
```js
3228
import { setPanicHook } from "./index.js";
@@ -35,7 +31,7 @@ setPanicHook(function (err) {
3531
});
3632
```
3733

38-
We then call a new reset state function when a panic occurs, to reinitialize the Wasm application to back to as it was when the application first started.
34+
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.
3935

4036
## Resetting VM State in Wasm Bindgen
4137

@@ -48,22 +44,16 @@ One other necessary change here was associating Wasm-created JS objects with an
4844

4945
## Layered Solution
5046

51-
By implementing only the minimal primitive upstream in Wasm Bindgen necessary, we could then comprehensively solve the remaining JS state concerns for workers-rs specifically.
52-
53-
For this a proxy wrapper was needed 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
47+
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
5448
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.
5549

56-
This approach is then all that is needed to provide full panic recovery for Rust Workers. Requests in progress during a panic will provide 500 errors, but the worker will then instantly recover for future requests with an instant reset.
50+
This approach now provides full panic recovery for Rust Workers. Requests in progress during a panic will provide 500 errors, but the worker will then instantly recover for future requests with an instant reset.
5751

5852
Of course we never want panics, but when they do happen they are isolated and can be investigated further from the error logs - avoiding broader service disruption.
5953

6054
## WebAssembly Exception Handling
6155

62-
In future, we would like to see full support for recoverable panics without even needing reinitialization at all.
63-
64-
With the [WebAssembly Exception Handling](https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md) proposal part of the newly announced [WebAssembly 3.0](https://webassembly.org/news/2025-09-17-wasm-3.0/) specification,
65-
it would actually be possible to fully unwind panics as normal JS errors. Concurrent requests would no longer fail at all, and all state would remain just fine.
66-
67-
This would be a larger Wasm Bindgen initiative, but a very useful one to explore further.
56+
In future, full support for recoverable panics could be implemented without needing reinitialization at all, utilizing the [WebAssembly Exception Handling](https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md)
57+
proposal, part of the newly announced [WebAssembly 3.0](https://webassembly.org/news/2025-09-17-wasm-3.0/) specification. This would allow unwinding panics as normal JS errors, and concurrent requests would no longer fail.
6858

6959
**We're making significant improvements to the reliability of [Rust Workers](https://github.com/cloudflare/workers-rs). Join us in `#rust-on-workers` on the [Cloudflare Developers Discord](https://discord.gg/cloudflaredev) to stay updated.**

0 commit comments

Comments
 (0)