forked from cloudflare/workers-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Because of a limitation with wasm_bindgen (rustwasm/wasm-bindgen#2724), a Rust panic in asynchronous code isn't able to reject the Promise it happened in. This change makes the main worker Promise cancellable, with a panic hook or by rewriting the wasm binary, it's possible to reject the Promise and handle the error. The worker concurrency model makes rejecting the Promise not a guarantee in cases the worker has a lot of traffic that cause a panic. See comment in source code. Usage (for now), add to your worker: ```rust fn start() { std::panic::set_hook(Box::new(|i| { __workers_rs_cancel(); worker::console_error_panic_hook::hook(i); })); } ``` For example, previously: ``` POST https://example.com/ - Exception Thrown @ 8/10/2023, 12:26:59 PM ✘ [ERROR] Error: The script will never generate a response. ``` with this change: ``` POST https://example.com/ - Exception Thrown @ 8/10/2023, 12:27:05 PM (error) panicked at 'oops', src/lib.rs:43:5 Stack: Error at entry.js:746:17 at logError (entry.js:304:14) at __wbg_new_abda76e883ba8a5f (entry.js:745:10) at console_error_panic_hook::hook::hb5e89e21e6f36fcb (wasm://wasm/0351507a:wasm-function[174]:0x1bca6) at test_coredump_worker::start_start_glue::{{closure}}::hc242a0946799f25b (wasm://wasm/0351507a:wasm-function[854]:0x2d662) at std::panicking::rust_panic_with_hook::h85b7f6628c291e12 (wasm://wasm/0351507a:wasm-function[269]:0x2234c) at std::panicking::begin_panic_handler::{{closure}}::h1e17bad04a5713a4 (wasm://wasm/0351507a:wasm-function[323]:0x24aa6) at std::sys_common::backtrace::__rust_end_short_backtrace::h63adae5c31458c4b (wasm://wasm/0351507a:wasm-function[875]:0x2d72e) at rust_begin_unwind (wasm://wasm/0351507a:wasm-function[491]:0x29a2d) at core::panicking::panic_fmt::hf5c4cd929d4aaa9e (wasm://wasm/0351507a:wasm-function[601]:0x2ba1d) ```
- Loading branch information
Showing
6 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::js_sys::Promise; | ||
use crate::wasm_bindgen::prelude::*; | ||
use crate::AbortSignal; | ||
|
||
pub fn make(signal: AbortSignal, p: Promise) -> Promise { | ||
Promise::new(&mut |resolve, reject| { | ||
let msg = "Request has been aborted"; | ||
|
||
if signal.aborted() { | ||
reject | ||
.call1(&JsValue::undefined(), &msg.into()) | ||
.unwrap_throw(); | ||
} | ||
|
||
{ | ||
let reject = reject.clone(); | ||
let on_abort = Closure::<dyn FnMut(JsValue)>::new(move |_| { | ||
reject | ||
.call1(&JsValue::undefined(), &msg.into()) | ||
.unwrap_throw(); | ||
}); | ||
signal.set_onabort(Some(&on_abort.as_ref().unchecked_ref())); | ||
// prevent the closure of being dropped before JS tries | ||
// to call it. | ||
on_abort.forget(); | ||
} | ||
|
||
// Listen for the initial promise completion | ||
{ | ||
let resolve2 = Closure::new(move |val| { | ||
resolve.call1(&JsValue::undefined(), &val).unwrap_throw(); | ||
}); | ||
let reject2 = Closure::new(move |val| { | ||
reject.call1(&JsValue::undefined(), &val).unwrap_throw(); | ||
}); | ||
p.then2(&resolve2, &reject2); | ||
} | ||
() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters