Merge pull request #1075 from martinjlowm/fix/cleanup-registered-hook

fix(napi): cleanup registered hook upon unloading module with tokio_rt
This commit is contained in:
LongYinan 2022-02-17 22:07:49 +08:00 committed by GitHub
commit ab3ea5a58a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 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<F>(fut: F)