Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tokio_threadpool's new panic_handler #2188

Merged
merged 1 commit into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use tokio_threadpool's new panic_handler
  • Loading branch information
ry committed Apr 23, 2019
commit 920560adef3b91b898dfb46f68134c0b3eb8fdc1
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