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/ops_json.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'core/ops_json.rs') diff --git a/core/ops_json.rs b/core/ops_json.rs index 0ca7e5ce4..dca9a9a77 100644 --- a/core/ops_json.rs +++ b/core/ops_json.rs @@ -1,6 +1,7 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use crate::error::AnyError; +use crate::ops::OpCall; use crate::serialize_op_result; use crate::Op; use crate::OpFn; @@ -35,7 +36,7 @@ pub fn void_op_async() -> Box { let op_id = payload.op_id; let pid = payload.promise_id; let op_result = serialize_op_result(Ok(()), state); - Op::Async(Box::pin(futures::future::ready((pid, op_id, op_result)))) + Op::Async(OpCall::ready((pid, op_id, op_result))) }) } @@ -127,7 +128,7 @@ where use crate::futures::FutureExt; let fut = op_fn(state.clone(), a, b) .map(move |result| (pid, op_id, serialize_op_result(result, state))); - Op::Async(Box::pin(fut)) + Op::Async(OpCall::eager(fut)) }) } @@ -159,7 +160,7 @@ where use crate::futures::FutureExt; let fut = op_fn(state.clone(), a, b) .map(move |result| (pid, op_id, serialize_op_result(result, state))); - Op::AsyncUnref(Box::pin(fut)) + Op::AsyncUnref(OpCall::eager(fut)) }) } -- cgit v1.2.3