summaryrefslogtreecommitdiff
path: root/cli/tokio_util.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-09-12 02:10:14 +0200
committerRyan Dahl <ry@tinyclouds.org>2019-09-11 20:10:14 -0400
commit61231912e2c4f6cc671a061468b864f371a276a2 (patch)
tree01b716a30a7cafd02b6728007da75002e37254d8 /cli/tokio_util.rs
parent19cd8deaf211fde97b9755a8cf93987a74d813b0 (diff)
fix: panicking when can't create runtime for block_on (#2905)
Diffstat (limited to 'cli/tokio_util.rs')
-rw-r--r--cli/tokio_util.rs28
1 files changed, 17 insertions, 11 deletions
diff --git a/cli/tokio_util.rs b/cli/tokio_util.rs
index 7bbfeee65..831419759 100644
--- a/cli/tokio_util.rs
+++ b/cli/tokio_util.rs
@@ -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());