From ff932b411d63269dbd4d30ea6bd0aa5160fd8aff Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sat, 9 Oct 2021 22:37:19 +0200 Subject: fix(core): poll async ops eagerly (#12385) Currently all async ops are polled lazily, which means that op initialization code is postponed until control is yielded to the event loop. This has some weird consequences, e.g. ```js let listener = Deno.listen(...); let conn_promise = listener.accept(); listener.close(); // `BadResource` is thrown. A reasonable error would be `Interrupted`. let conn = await conn_promise; ``` JavaScript promises are expected to be eagerly evaluated. This patch makes ops actually do that. --- core/runtime.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'core/runtime.rs') diff --git a/core/runtime.rs b/core/runtime.rs index 1928ff31c..873dcd3f5 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -28,7 +28,6 @@ use futures::future::FutureExt; use futures::stream::FuturesUnordered; use futures::stream::StreamExt; use futures::task::AtomicWaker; -use futures::Future; use std::any::Any; use std::cell::RefCell; use std::collections::HashMap; @@ -36,7 +35,6 @@ use std::convert::TryFrom; use std::ffi::c_void; use std::mem::forget; use std::option::Option; -use std::pin::Pin; use std::rc::Rc; use std::sync::Arc; use std::sync::Mutex; @@ -44,8 +42,7 @@ use std::sync::Once; use std::task::Context; use std::task::Poll; -type PendingOpFuture = - Pin>>; +type PendingOpFuture = OpCall<(PromiseId, OpId, OpResult)>; pub enum Snapshot { Static(&'static [u8]), @@ -1613,6 +1610,7 @@ pub mod tests { use crate::ZeroCopyBuf; use futures::future::lazy; use std::ops::FnOnce; + use std::pin::Pin; use std::rc::Rc; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; @@ -1645,16 +1643,15 @@ pub mod tests { Mode::Async => { assert_eq!(control, 42); let resp = (0, 1, serialize_op_result(Ok(43), rc_op_state)); - Op::Async(Box::pin(futures::future::ready(resp))) + Op::Async(OpCall::ready(resp)) } Mode::AsyncZeroCopy(has_buffer) => { assert_eq!(buf.is_some(), has_buffer); if let Some(buf) = buf { assert_eq!(buf.len(), 1); } - - let resp = serialize_op_result(Ok(43), rc_op_state); - Op::Async(Box::pin(futures::future::ready((0, 1, resp)))) + let resp = (0, 1, serialize_op_result(Ok(43), rc_op_state)); + Op::Async(OpCall::ready(resp)) } } } -- cgit v1.2.3