mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 12:16:11 -05:00
fix: panicking when can't create runtime for block_on (#2905)
This commit is contained in:
parent
19cd8deaf2
commit
61231912e2
1 changed files with 17 additions and 11 deletions
|
@ -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;
|
||||
|
@ -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());
|
||||
|
|
Loading…
Add table
Reference in a new issue