Skip to content

Commit c86c380

Browse files
authored
feat: support --no-panic-recovery flag to disable panic reinitialization (#835)
1 parent 8694ff3 commit c86c380

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

test/src/counter.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::SomeSharedData;
1111
#[durable_object]
1212
pub struct Counter {
1313
count: RefCell<usize>,
14+
unstored_count: RefCell<usize>,
1415
state: State,
1516
initialized: RefCell<bool>,
1617
env: Env,
@@ -20,6 +21,7 @@ impl DurableObject for Counter {
2021
fn new(state: State, env: Env) -> Self {
2122
Self {
2223
count: RefCell::new(0),
24+
unstored_count: RefCell::new(0),
2325
initialized: RefCell::new(false),
2426
state,
2527
env,
@@ -47,13 +49,15 @@ impl DurableObject for Counter {
4749
.empty());
4850
}
4951

52+
*self.unstored_count.borrow_mut() += 1;
5053
*self.count.borrow_mut() += 10;
5154
let count = *self.count.borrow();
5255
self.state.storage().put("count", count).await?;
5356

5457
Response::ok(format!(
55-
"[durable_object]: self.count: {}, secret value: {}",
58+
"[durable_object]: self.count: {}, self.unstored_count: {}, secret value: {}",
5659
self.count.borrow(),
60+
self.unstored_count.borrow(),
5761
self.env.secret("SOME_SECRET")?
5862
))
5963
}

test/tests/panic.spec.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
import { describe, test, expect } from "vitest";
2-
import { mf } from "./mf";
2+
import { mf, mfUrl } from "./mf";
33

44
describe("Panic Hook with WASM Reinitialization", () => {
55
test("panic recovery tests", async () => {
66
// basic panic recovery
77
{
8-
const panicResp = await mf.dispatchFetch("http://fake.host/test-panic");
8+
await mf.dispatchFetch(`${mfUrl}durable/COUNTER`);
9+
const resp = await mf.dispatchFetch(`${mfUrl}durable/COUNTER`);
10+
expect(await resp.text()).toContain("unstored_count: 2");
11+
12+
const panicResp = await mf.dispatchFetch(`${mfUrl}test-panic`);
913
expect(panicResp.status).toBe(500);
1014

1115
const panicText = await panicResp.text();
1216
expect(panicText).toContain("Workers runtime canceled");
1317

14-
const normalResp = await mf.dispatchFetch("http://fake.host/durable/hello");
15-
expect(normalResp.status).toBe(200);
16-
17-
const normalText = await normalResp.text();
18-
expect(normalText).toContain("Hello from my-durable-object!");
18+
const normalResp = await mf.dispatchFetch(`${mfUrl}durable/COUNTER`);
19+
expect(await normalResp.text()).toContain("unstored_count: 1");
1920
}
2021

2122
// multiple requests after panic all succeed
2223
{
23-
const panicResp = await mf.dispatchFetch("http://fake.host/test-panic");
24+
const panicResp = await mf.dispatchFetch(`${mfUrl}test-panic`);
2425
expect(panicResp.status).toBe(500);
2526

2627
const requests = [
27-
mf.dispatchFetch("http://fake.host/durable/hello"),
28-
mf.dispatchFetch("http://fake.host/durable/hello"),
29-
mf.dispatchFetch("http://fake.host/durable/hello"),
28+
mf.dispatchFetch(`${mfUrl}durable/hello`),
29+
mf.dispatchFetch(`${mfUrl}durable/hello`),
30+
mf.dispatchFetch(`${mfUrl}durable/hello`),
3031
];
3132

3233
const responses = await Promise.all(requests);
@@ -41,9 +42,9 @@ describe("Panic Hook with WASM Reinitialization", () => {
4142
// simultaneous requests during panic handling
4243
{
4344
const simultaneousRequests = [
44-
mf.dispatchFetch("http://fake.host/test-panic"), // This will panic
45-
mf.dispatchFetch("http://fake.host/durable/hello"), // This should succeed after reinitialization
46-
mf.dispatchFetch("http://fake.host/durable/hello"),
45+
mf.dispatchFetch(`${mfUrl}test-panic`), // This will panic
46+
mf.dispatchFetch(`${mfUrl}durable/hello`), // This should succeed after reinitialization
47+
mf.dispatchFetch(`${mfUrl}durable/hello`),
4748
];
4849

4950
const responses = await Promise.all(simultaneousRequests);
@@ -64,12 +65,10 @@ describe("Panic Hook with WASM Reinitialization", () => {
6465
// worker continues to function normally after multiple panics
6566
{
6667
for (let cycle = 1; cycle <= 3; cycle++) {
67-
const panicResp = await mf.dispatchFetch("http://fake.host/test-panic");
68+
const panicResp = await mf.dispatchFetch(`${mfUrl}test-panic`);
6869
expect(panicResp.status).toBe(500);
6970

70-
const recoveryResp = await mf.dispatchFetch(
71-
"http://fake.host/durable/hello"
72-
);
71+
const recoveryResp = await mf.dispatchFetch(`${mfUrl}durable/hello`);
7372
expect(recoveryResp.status).toBe(200);
7473
}
7574
}

worker-build/src/main.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@ fn update_package_json() -> Result<()> {
5656
}
5757

5858
pub fn main() -> Result<()> {
59-
let first_arg = env::args().nth(1);
60-
if matches!(first_arg.as_deref(), Some("--version") | Some("-v")) {
59+
let args: Vec<_> = env::args().collect();
60+
if matches!(
61+
args.first().map(String::as_str),
62+
Some("--version") | Some("-v")
63+
) {
6164
println!("{}", VERSION);
6265
return Ok(());
6366
}
67+
let no_panic_recovery = args.iter().any(|a| a == "--no-panic-recovery");
6468

6569
let out_path = output_path("");
6670
if out_path.exists() {
@@ -72,15 +76,21 @@ pub fn main() -> Result<()> {
7276

7377
wasm_pack_build.init()?;
7478

75-
let module_target = wasm_pack_build.supports_target_module_and_reset_state()?
76-
|| env::var("CUSTOM_SHIM").is_ok();
79+
let supports_reset_state = wasm_pack_build.supports_target_module_and_reset_state()?;
80+
let module_target =
81+
supports_reset_state && !no_panic_recovery && env::var("CUSTOM_SHIM").is_err();
7782
if module_target {
7883
wasm_pack_build
7984
.extra_args
8085
.push("--experimental-reset-state-function".to_string());
8186
wasm_pack_build.run()?;
8287
} else {
83-
eprintln!("A newer version of wasm-bindgen is available. Update to use the latest workers-rs features.");
88+
if supports_reset_state {
89+
// Enable once we have DO bindings to offer an alternative
90+
// eprintln!("Using CUSTOM_SHIM will be deprecated in a future release.");
91+
} else {
92+
eprintln!("A newer version of wasm-bindgen is available. Update to use the latest workers-rs features.");
93+
}
8494
wasm_pack_build.target = Target::Bundler;
8595
wasm_pack_build.run()?;
8696
}

worker-build/src/wasm_pack/command/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ pub struct BuildOptions {
176176
/// List of extra options to pass to `cargo build`
177177
pub extra_options: Vec<String>,
178178

179+
#[clap(long, hide = true)]
180+
/// Pass-through for --no-panic-recovery
181+
pub no_panic_recovery: bool,
182+
179183
#[deprecated(note = "runtime-detected")]
180184
#[allow(dead_code)]
181185
#[clap(long = "weak-refs", hide = true)]

0 commit comments

Comments
 (0)