提交 61231912 编写于 作者: B Bartek Iwańczuk 提交者: Ryan Dahl

fix: panicking when can't create runtime for block_on (#2905)

上级 19cd8dea
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::resources::Resource;
use deno::ErrBox;
use futures;
use futures::Future;
use futures::Poll;
......@@ -10,11 +11,11 @@ use tokio;
use tokio::net::TcpStream;
use tokio::runtime;
pub fn create_threadpool_runtime() -> tokio::runtime::Runtime {
pub fn create_threadpool_runtime(
) -> Result<tokio::runtime::Runtime, tokio::io::Error> {
runtime::Builder::new()
.panic_handler(|err| std::panic::resume_unwind(err))
.build()
.unwrap()
}
pub fn run<F>(future: F)
......@@ -22,7 +23,7 @@ where
F: Future<Item = (), Error = ()> + Send + 'static,
{
// tokio::runtime::current_thread::run(future)
let rt = create_threadpool_runtime();
let rt = create_threadpool_runtime().expect("Unable to create Tokio runtime");
rt.block_on_all(future).unwrap();
}
......@@ -39,22 +40,27 @@ where
/// given future. This is useful when we want to block the main runtime to
/// resolve a future without worrying that we'll use up all the threads in the
/// main runtime.
pub fn block_on<F, R, E>(future: F) -> Result<R, E>
pub fn block_on<F, R>(future: F) -> Result<R, ErrBox>
where
F: Send + 'static + Future<Item = R, Error = E>,
F: Send + 'static + Future<Item = R, Error = ErrBox>,
R: Send + 'static,
E: Send + 'static,
{
use std::sync::mpsc::channel;
use std::thread;
let (sender, receiver) = channel();
// Create a new runtime to evaluate the future asynchronously.
thread::spawn(move || {
let mut rt = create_threadpool_runtime();
let r = rt.block_on(future);
sender.send(r).unwrap();
let r = match create_threadpool_runtime() {
Ok(mut rt) => rt.block_on(future),
Err(e) => Err(ErrBox::from(e)),
};
sender
.send(r)
.expect("Unable to send blocking future result")
});
receiver.recv().unwrap()
receiver
.recv()
.expect("Unable to receive blocking future result")
}
// Set the default executor so we can use tokio::spawn(). It's difficult to
......@@ -65,7 +71,7 @@ pub fn init<F>(f: F)
where
F: FnOnce(),
{
let rt = create_threadpool_runtime();
let rt = create_threadpool_runtime().expect("Unable to create Tokio 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| f());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册