diff --git a/crates/napi/src/bindgen_runtime/module_register.rs b/crates/napi/src/bindgen_runtime/module_register.rs index e33384c5..ea4849a6 100644 --- a/crates/napi/src/bindgen_runtime/module_register.rs +++ b/crates/napi/src/bindgen_runtime/module_register.rs @@ -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 ); diff --git a/crates/napi/src/tokio_runtime.rs b/crates/napi/src/tokio_runtime.rs index 8828e331..9fbac24b 100644 --- a/crates/napi/src/tokio_runtime.rs +++ b/crates/napi/src/tokio_runtime.rs @@ -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 unsafe 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(fut: F)