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.
This commit is contained in:
Simon Laux 2022-11-15 04:50:25 +01:00 committed by GitHub
parent b0c248ad7e
commit 035def0600
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -52,11 +52,15 @@ pub unsafe extern "C" fn shutdown_tokio_rt(arg: *mut c_void) {
}
}
pub fn spawn<F>(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<F>(fut: F) -> tokio::task::JoinHandle<F::Output>
where
F: 'static + Send + Future<Output = ()>,
{
RT.0.spawn(fut);
RT.0.spawn(fut)
}
/// Runs a future to completion