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

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

Merged
merged 5 commits into from
Sep 12, 2019
Merged
Changes from 4 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
27 changes: 16 additions & 11 deletions cli/tokio_util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// 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;
Expand All @@ -10,19 +11,19 @@ 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)
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();
}

Expand All @@ -39,20 +40,24 @@ 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();
thread::spawn(move || match create_threadpool_runtime() {
Ok(mut rt) => {
let fut_r = rt.block_on(future);
sender.send(fut_r).unwrap()
}
Err(e) => {
let err = Err(ErrBox::from(e));
sender.send(err).unwrap()
}
});
receiver.recv().unwrap()
}
Expand All @@ -65,7 +70,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());
Expand Down