未验证 提交 675919e9 编写于 作者: R Ryan Dahl 提交者: GitHub

Use tokio_threadpool's new panic_handler (#2188)

上级 6caf8655
......@@ -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.
......@@ -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
......
......@@ -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>
......@@ -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)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册