Skip to content

Commit c7e1f8f

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

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

cli/compiler.rs

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

112112
let mut runtime = C_RUNTIME.lock().unwrap();
113113
runtime.spawn(lazy(move || {
114+
tokio_util::abort_on_panic();
115+
114116
worker.then(move |result| -> Result<(), ()> {
115117
// Close resource so the future created by
116118
// handle_worker_message_stream exits

cli/tokio_util.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@ pub fn run<F>(future: F)
1313
where
1414
F: Future<Item = (), Error = ()> + Send + 'static,
1515
{
16+
abort_on_panic();
1617
// tokio::runtime::current_thread::run(future)
1718
tokio::run(future)
1819
}
1920

21+
// Tokio swallows panics. In order to actually crash when we panic, we
22+
// have to set this custom hook.
23+
// https://github.com/tokio-rs/tokio/issues/495
24+
// https://github.com/tokio-rs/tokio/issues/209
25+
pub fn abort_on_panic() {
26+
std::panic::set_hook(Box::new(|panic_info| {
27+
eprintln!("{}", panic_info.to_string());
28+
std::process::abort();
29+
}));
30+
}
31+
2032
pub fn block_on<F, R, E>(future: F) -> Result<R, E>
2133
where
2234
F: Send + 'static + Future<Item = R, Error = E>,
@@ -40,7 +52,10 @@ where
4052
let rt = tokio::runtime::Runtime::new().unwrap();
4153
let mut executor = rt.executor();
4254
let mut enter = tokio_executor::enter().expect("Multiple executors at once");
43-
tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f());
55+
tokio_executor::with_default(&mut executor, &mut enter, move |_enter| {
56+
abort_on_panic();
57+
f()
58+
});
4459
}
4560

4661
#[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)