From 035def060077a94e87c7fbf6303b197d66f6e573 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Tue, 15 Nov 2022 04:50:25 +0100 Subject: [PATCH] fix(napi): return the join handle when spawning a tokio task. (#1351) we need this to be able to safely drop an struct, that makes use of an async task. the drop function needs to be able to call `.abort()` on the join handle to avoid memory corruption and undefined bahviour. --- crates/napi/src/tokio_runtime.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/napi/src/tokio_runtime.rs b/crates/napi/src/tokio_runtime.rs index ceb8507e8a..932d387df1 100644 --- a/crates/napi/src/tokio_runtime.rs +++ b/crates/napi/src/tokio_runtime.rs @@ -52,11 +52,15 @@ pub unsafe extern "C" fn shutdown_tokio_rt(arg: *mut c_void) { } } -pub fn spawn(fut: F) +/// Spawns a future onto the Tokio runtime. +/// +/// Depending on where you use it, you should await or abort the future in your drop function. +/// To avoid undefined behavior and memory corruptions. +pub fn spawn(fut: F) -> tokio::task::JoinHandle where F: 'static + Send + Future, { - RT.0.spawn(fut); + RT.0.spawn(fut) } /// Runs a future to completion