summaryrefslogtreecommitdiff
path: root/cli/tokio_util.rs
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-04-14 16:07:24 -0400
committerGitHub <noreply@github.com>2019-04-14 16:07:24 -0400
commitc7e1f8fabdc87d3622fd94769c57197d5dfecad3 (patch)
tree9942097cdbba11f2af002208a2ef70f1f196b594 /cli/tokio_util.rs
parentb413cd5afd6b74b3e50bf6dbb5972ea7df9f34fd (diff)
Fix silent error, add custom panic handler (#2098)
This is to work around Tokio's panic recovery feature. Ref https://github.com/tokio-rs/tokio/issues/495 Ref https://github.com/tokio-rs/tokio/issues/209 Ref https://github.com/denoland/deno/issues/1311 Fixes #2097
Diffstat (limited to 'cli/tokio_util.rs')
-rw-r--r--cli/tokio_util.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/cli/tokio_util.rs b/cli/tokio_util.rs
index 810b826b4..bf27c39f0 100644
--- a/cli/tokio_util.rs
+++ b/cli/tokio_util.rs
@@ -13,10 +13,22 @@ pub fn run<F>(future: F)
where
F: Future<Item = (), Error = ()> + Send + 'static,
{
+ abort_on_panic();
// tokio::runtime::current_thread::run(future)
tokio::run(future)
}
+// Tokio swallows panics. In order to actually crash when we panic, we
+// have to set this custom hook.
+// https://github.com/tokio-rs/tokio/issues/495
+// https://github.com/tokio-rs/tokio/issues/209
+pub fn abort_on_panic() {
+ std::panic::set_hook(Box::new(|panic_info| {
+ eprintln!("{}", panic_info.to_string());
+ std::process::abort();
+ }));
+}
+
pub fn block_on<F, R, E>(future: F) -> Result<R, E>
where
F: Send + 'static + Future<Item = R, Error = E>,
@@ -40,7 +52,10 @@ where
let rt = tokio::runtime::Runtime::new().unwrap();
let mut executor = rt.executor();
let mut enter = tokio_executor::enter().expect("Multiple executors at once");
- tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f());
+ tokio_executor::with_default(&mut executor, &mut enter, move |_enter| {
+ abort_on_panic();
+ f()
+ });
}
#[derive(Debug)]