Skip to content

Commit fa937d5

Browse files
committed
Fix silent error, add custom panic handler
This is to work around Tokio's panic recovery feature. Ref tokio-rs/tokio#495 Ref tokio-rs/tokio#209 Ref denoland#1311 Fixes denoland#2097
1 parent b413cd5 commit fa937d5

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

cli/compiler.rs

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ fn lazy_start(parent_state: ThreadSafeState) -> ResourceId {
111111

112112
let mut runtime = C_RUNTIME.lock().unwrap();
113113
runtime.spawn(lazy(move || {
114+
// Tokio swallows panics. In order to actually crash when we panic, we
115+
// have to set this custom hook.
116+
std::panic::set_hook(Box::new(|panic_info| {
117+
eprintln!("{}", panic_info.to_string());
118+
std::process::abort();
119+
}));
120+
114121
worker.then(move |result| -> Result<(), ()> {
115122
// Close resource so the future created by
116123
// handle_worker_message_stream exits

cli/tokio_util.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ pub fn run<F>(future: F)
1313
where
1414
F: Future<Item = (), Error = ()> + Send + 'static,
1515
{
16+
// Tokio swallows panics. In order to actually crash when we panic, we
17+
// have to set this custom hook.
18+
std::panic::set_hook(Box::new(|panic_info| {
19+
eprintln!("{}", panic_info.to_string());
20+
std::process::abort();
21+
}));
1622
// tokio::runtime::current_thread::run(future)
1723
tokio::run(future)
1824
}
@@ -40,7 +46,15 @@ where
4046
let rt = tokio::runtime::Runtime::new().unwrap();
4147
let mut executor = rt.executor();
4248
let mut enter = tokio_executor::enter().expect("Multiple executors at once");
43-
tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f());
49+
tokio_executor::with_default(&mut executor, &mut enter, move |_enter| {
50+
// Tokio swallows panics. In order to actually crash when we panic, we
51+
// have to set this custom hook.
52+
std::panic::set_hook(Box::new(|panic_info| {
53+
eprintln!("{}", panic_info.to_string());
54+
std::process::abort();
55+
}));
56+
f()
57+
});
4458
}
4559

4660
#[derive(Debug)]

cli/worker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ mod tests {
324324
onmessage = function(e) {
325325
console.log("msg from main script", e.data);
326326
if (e.data == "exit") {
327-
close();
327+
delete window.onmessage;
328328
return;
329329
} else {
330330
console.assert(e.data === "hi");

0 commit comments

Comments
 (0)