diff --git a/cli/compiler.rs b/cli/compiler.rs index ae4e3419fb..d327835d33 100644 --- a/cli/compiler.rs +++ b/cli/compiler.rs @@ -35,7 +35,7 @@ lazy_static! { static ref C_RID: Mutex> = Mutex::new(None); // tokio runtime specifically for spawning logic that is dependent on // completetion of the compiler worker future - static ref C_RUNTIME: Mutex = Mutex::new(Runtime::new().unwrap()); + static ref C_RUNTIME: Mutex = Mutex::new(tokio_util::create_threadpool_runtime()); } // This corresponds to JS ModuleMetaData. @@ -111,8 +111,6 @@ fn lazy_start(parent_state: ThreadSafeState) -> ResourceId { let mut runtime = C_RUNTIME.lock().unwrap(); runtime.spawn(lazy(move || { - tokio_util::abort_on_panic(); - worker.then(move |result| -> Result<(), ()> { // Close resource so the future created by // handle_worker_message_stream exits diff --git a/cli/tokio_util.rs b/cli/tokio_util.rs index 5b975ff403..e1f8587c32 100644 --- a/cli/tokio_util.rs +++ b/cli/tokio_util.rs @@ -8,25 +8,28 @@ use std::mem; use std::net::SocketAddr; use tokio; use tokio::net::TcpStream; +use tokio::runtime; + +pub fn create_threadpool_runtime() -> tokio::runtime::Runtime { + // This code can be simplified once the following PR is landed and + // released: https://github.com/tokio-rs/tokio/pull/1055 + use tokio_threadpool::Builder as ThreadPoolBuilder; + let mut threadpool_builder = ThreadPoolBuilder::new(); + threadpool_builder.panic_handler(|err| std::panic::resume_unwind(err)); + #[allow(deprecated)] + runtime::Builder::new() + .threadpool_builder(threadpool_builder) + .build() + .unwrap() +} pub fn run(future: F) where F: Future + 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(); - })); + let rt = create_threadpool_runtime(); + rt.block_on_all(future).unwrap(); } pub fn block_on(future: F) -> Result @@ -48,13 +51,10 @@ pub fn init(f: F) where F: FnOnce(), { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = create_threadpool_runtime(); 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| { - abort_on_panic(); - f() - }); + tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f()); } #[derive(Debug)]