0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-09 05:36:49 -04:00

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

This commit is contained in:
Bartek Iwańczuk 2019-09-12 02:10:14 +02:00 committed by Ryan Dahl
parent 19cd8deaf2
commit 61231912e2

View file

@ -1,5 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::resources::Resource; use crate::resources::Resource;
use deno::ErrBox;
use futures; use futures;
use futures::Future; use futures::Future;
use futures::Poll; use futures::Poll;
@ -10,11 +11,11 @@ use tokio;
use tokio::net::TcpStream; use tokio::net::TcpStream;
use tokio::runtime; 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() runtime::Builder::new()
.panic_handler(|err| std::panic::resume_unwind(err)) .panic_handler(|err| std::panic::resume_unwind(err))
.build() .build()
.unwrap()
} }
pub fn run<F>(future: F) pub fn run<F>(future: F)
@ -22,7 +23,7 @@ where
F: Future<Item = (), Error = ()> + Send + 'static, F: Future<Item = (), Error = ()> + Send + 'static,
{ {
// tokio::runtime::current_thread::run(future) // 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(); 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 /// 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 /// resolve a future without worrying that we'll use up all the threads in the
/// main runtime. /// 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 where
F: Send + 'static + Future<Item = R, Error = E>, F: Send + 'static + Future<Item = R, Error = ErrBox>,
R: Send + 'static, R: Send + 'static,
E: Send + 'static,
{ {
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::thread; use std::thread;
let (sender, receiver) = channel(); let (sender, receiver) = channel();
// Create a new runtime to evaluate the future asynchronously. // Create a new runtime to evaluate the future asynchronously.
thread::spawn(move || { thread::spawn(move || {
let mut rt = create_threadpool_runtime(); let r = match create_threadpool_runtime() {
let r = rt.block_on(future); Ok(mut rt) => rt.block_on(future),
sender.send(r).unwrap(); 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 // 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 where
F: FnOnce(), 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 executor = rt.executor();
let mut enter = tokio_executor::enter().expect("Multiple executors at once"); let mut enter = tokio_executor::enter().expect("Multiple executors at once");
tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f()); tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f());