From 606611708c4351e9f5e0e3b975f9331d95168efb Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 25 Jun 2021 13:15:35 +0900 Subject: fix(runtime/signal): use op_async_unref for op_signal_poll (#11097) --- core/lib.rs | 1 + core/ops_json.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) (limited to 'core') diff --git a/core/lib.rs b/core/lib.rs index 9235d12e4..f02ff15c6 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -73,6 +73,7 @@ pub use crate::ops_builtin::op_close; pub use crate::ops_builtin::op_print; pub use crate::ops_builtin::op_resources; pub use crate::ops_json::op_async; +pub use crate::ops_json::op_async_unref; pub use crate::ops_json::op_sync; pub use crate::resources::Resource; pub use crate::resources::ResourceId; diff --git a/core/ops_json.rs b/core/ops_json.rs index 07208a420..25752322a 100644 --- a/core/ops_json.rs +++ b/core/ops_json.rs @@ -51,6 +51,9 @@ where /// Creates an op that passes data asynchronously using JSON. /// +/// When this op is dispatched, the runtime doesn't exit while processing it. +/// Use op_async_unref instead if you want to make the runtime exit while processing it. +/// /// The provided function `op_fn` has the following parameters: /// * `Rc`: the op state, can be used to read/write resources in the runtime from an op. /// * `V`: the deserializable value that is passed to the Rust function. @@ -99,6 +102,37 @@ where }) } +/// Creates an op that passes data asynchronously using JSON. +/// +/// When this op is dispatched, the runtime still can exit while processing it. +/// +/// The other usages are the same as `op_async`. +pub fn op_async_unref(op_fn: F) -> Box +where + F: Fn(Rc>, A, B) -> R + 'static, + A: DeserializeOwned, + B: DeserializeOwned, + R: Future> + 'static, + RV: Serialize + 'static, +{ + Box::new(move |state, payload| -> Op { + let pid = payload.promise_id; + // Deserialize args, sync error on failure + let args = match payload.deserialize() { + Ok(args) => args, + Err(err) => { + return Op::Sync(serialize_op_result(Err::<(), AnyError>(err), state)) + } + }; + let (a, b) = args; + + use crate::futures::FutureExt; + let fut = op_fn(state.clone(), a, b) + .map(move |result| (pid, serialize_op_result(result, state))); + Op::AsyncUnref(Box::pin(fut)) + }) +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3