Skip to content

Commit

Permalink
Use tokio_threadpool's new panic_handler (#2188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Apr 23, 2019
1 parent 6caf865 commit 675919e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
4 changes: 1 addition & 3 deletions cli/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ lazy_static! {
static ref C_RID: Mutex<Option<ResourceId>> = Mutex::new(None);
// tokio runtime specifically for spawning logic that is dependent on
// completetion of the compiler worker future
static ref C_RUNTIME: Mutex<Runtime> = Mutex::new(Runtime::new().unwrap());
static ref C_RUNTIME: Mutex<Runtime> = Mutex::new(tokio_util::create_threadpool_runtime());
}

// This corresponds to JS ModuleMetaData.
Expand Down Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions cli/tokio_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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();
}));
let rt = create_threadpool_runtime();
rt.block_on_all(future).unwrap();
}

pub fn block_on<F, R, E>(future: F) -> Result<R, E>
Expand All @@ -48,13 +51,10 @@ pub fn init<F>(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)]
Expand Down

0 comments on commit 675919e

Please sign in to comment.