diff --git a/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs index 04beba6f..db7804fc 100644 --- a/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs @@ -8,7 +8,7 @@ use std::sync::{ }; #[cfg(all(feature = "napi4", not(target_arch = "wasm32")))] -use crate::bindgen_prelude::{CUSTOM_GC_TSFN, CUSTOM_GC_TSFN_CLOSED, THREADS_CAN_ACCESS_ENV}; +use crate::bindgen_prelude::{CUSTOM_GC_TSFN, THREADS_CAN_ACCESS_ENV, THREAD_DESTROYED}; pub use crate::js_values::TypedArrayType; use crate::{check_status, sys, Error, Result, Status}; @@ -68,9 +68,7 @@ macro_rules! impl_typed_array { if let Some((ref_, env)) = self.raw { #[cfg(all(feature = "napi4", not(target_arch = "wasm32")))] { - if CUSTOM_GC_TSFN_CLOSED - .with(|closed| closed.load(std::sync::atomic::Ordering::Relaxed)) - { + if THREAD_DESTROYED.with(|closed| closed.load(std::sync::atomic::Ordering::Relaxed)) { return; } if !THREADS_CAN_ACCESS_ENV diff --git a/crates/napi/src/bindgen_runtime/js_values/buffer.rs b/crates/napi/src/bindgen_runtime/js_values/buffer.rs index aab0fcef..4133bb80 100644 --- a/crates/napi/src/bindgen_runtime/js_values/buffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/buffer.rs @@ -10,7 +10,7 @@ use std::sync::Arc; use std::sync::Mutex; #[cfg(all(feature = "napi4", not(target_arch = "wasm32")))] -use crate::bindgen_prelude::{CUSTOM_GC_TSFN, CUSTOM_GC_TSFN_CLOSED, THREADS_CAN_ACCESS_ENV}; +use crate::bindgen_prelude::{CUSTOM_GC_TSFN, THREADS_CAN_ACCESS_ENV, THREAD_DESTROYED}; use crate::{bindgen_prelude::*, check_status, sys, Result, ValueType}; #[cfg(all(debug_assertions, not(windows)))] @@ -36,8 +36,7 @@ impl Drop for Buffer { if let Some((ref_, env)) = self.raw { #[cfg(all(feature = "napi4", not(target_arch = "wasm32")))] { - if CUSTOM_GC_TSFN_CLOSED.with(|closed| closed.load(std::sync::atomic::Ordering::Relaxed)) - { + if THREAD_DESTROYED.with(|closed| closed.load(std::sync::atomic::Ordering::Relaxed)) { return; } if !THREADS_CAN_ACCESS_ENV diff --git a/crates/napi/src/bindgen_runtime/module_register.rs b/crates/napi/src/bindgen_runtime/module_register.rs index 5ef58a8e..57bcb813 100644 --- a/crates/napi/src/bindgen_runtime/module_register.rs +++ b/crates/napi/src/bindgen_runtime/module_register.rs @@ -170,7 +170,7 @@ pub(crate) static CUSTOM_GC_TSFN: AtomicPtr = thread_local! { // CustomGC ThreadsafeFunction may be deleted during the process exit. // And there may still some Buffer alive after that. - pub(crate) static CUSTOM_GC_TSFN_CLOSED: AtomicBool = AtomicBool::new(false); + pub(crate) static THREAD_DESTROYED: AtomicBool = AtomicBool::new(false); } #[cfg(all(feature = "napi4", not(target_arch = "wasm32")))] // Store thread id of the thread that created the CustomGC ThreadsafeFunction. @@ -674,7 +674,7 @@ unsafe extern "C" fn custom_gc_finalize( finalize_data: *mut std::ffi::c_void, finalize_hint: *mut std::ffi::c_void, ) { - CUSTOM_GC_TSFN_CLOSED.with(|closed| { + THREAD_DESTROYED.with(|closed| { closed.store(true, Ordering::Relaxed); }); } diff --git a/crates/napi/src/js_values/deferred.rs b/crates/napi/src/js_values/deferred.rs index 23ac087a..0265190b 100644 --- a/crates/napi/src/js_values/deferred.rs +++ b/crates/napi/src/js_values/deferred.rs @@ -3,7 +3,7 @@ use std::marker::PhantomData; use std::os::raw::c_void; use std::ptr; -use crate::bindgen_runtime::ToNapiValue; +use crate::bindgen_runtime::{ToNapiValue, THREAD_DESTROYED}; use crate::{check_status, JsError, JsObject, Value}; use crate::{sys, Env, Error, Result}; @@ -106,6 +106,12 @@ extern "C" fn napi_resolve_deferred context: *mut c_void, data: *mut c_void, ) { + #[cfg(not(target_arch = "wasm32"))] + { + if THREAD_DESTROYED.with(|closed| closed.load(std::sync::atomic::Ordering::Relaxed)) { + return; + } + } let deferred = context as sys::napi_deferred; let resolver = unsafe { Box::from_raw(data as *mut Result) }; let result = resolver diff --git a/examples/napi/package.json b/examples/napi/package.json index 526eb162..b2042bc3 100644 --- a/examples/napi/package.json +++ b/examples/napi/package.json @@ -31,6 +31,6 @@ "environmentVariables": { "TS_NODE_PROJECT": "../tsconfig.json" }, - "timeout": "5m" + "timeout": "10m" } }