fix(napi): cleanup registered hook upon unloading module with tokio_rt

This commit is contained in:
Martin Madsen 2022-02-16 15:27:14 +01:00
parent 14773af159
commit 5a0c1c2af3
2 changed files with 8 additions and 2 deletions

View file

@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::c_void;
use std::ffi::CStr;
use std::ptr;
use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
@ -417,7 +418,7 @@ unsafe extern "C" fn napi_register_module_v1(
crate::tokio_runtime::TOKIO_RT_REF_COUNT.fetch_add(1, Ordering::Relaxed);
assert_eq!(
unsafe {
sys::napi_add_env_cleanup_hook(env, Some(crate::shutdown_tokio_rt), ptr::null_mut())
sys::napi_add_env_cleanup_hook(env, Some(crate::shutdown_tokio_rt), env as *mut c_void)
},
sys::Status::napi_ok
);

View file

@ -35,7 +35,7 @@ pub(crate) static TOKIO_RT_REF_COUNT: AtomicUsize = AtomicUsize::new(0);
#[doc(hidden)]
#[inline(never)]
pub extern "C" fn shutdown_tokio_rt(_arg: *mut c_void) {
pub extern "C" fn shutdown_tokio_rt(arg: *mut c_void) {
if TOKIO_RT_REF_COUNT.fetch_sub(1, Ordering::Relaxed) == 0 {
let sender = &RT.1;
if let Err(e) = sender.clone().try_send(()) {
@ -47,6 +47,11 @@ pub extern "C" fn shutdown_tokio_rt(_arg: *mut c_void) {
}
}
}
unsafe {
let env: sys::napi_env = arg as *mut sys::napi_env__;
sys::napi_remove_env_cleanup_hook(env, Some(shutdown_tokio_rt), arg);
}
}
pub fn spawn<F>(fut: F)