Skip to content

Commit

Permalink
fix(napi): return the join handle when spawning a tokio task. (#1351)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Simon-Laux committed Nov 15, 2022
1 parent b0c248a commit 035def0
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions crates/napi/src/tokio_runtime.rs
Expand Up @@ -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
Expand Down

0 comments on commit 035def0

Please sign in to comment.