diff --git a/crates/napi/src/js_values/deferred.rs b/crates/napi/src/js_values/deferred.rs index f7c1189c..9e972b18 100644 --- a/crates/napi/src/js_values/deferred.rs +++ b/crates/napi/src/js_values/deferred.rs @@ -102,6 +102,7 @@ struct DeferredData Result> { resolver: Result, #[cfg(feature = "deferred_trace")] trace: DeferredTrace, + tsfn: sys::napi_threadsafe_function, } pub struct JsDeferred Result> { @@ -164,6 +165,7 @@ impl Result> JsDeferred Result> JsDeferred ) { let deferred = context.cast(); let deferred_data: Box> = unsafe { Box::from_raw(data.cast()) }; + let tsfn = deferred_data.tsfn; let result = deferred_data .resolver .and_then(|resolver| resolver(Env::from_raw(env))) .and_then(|res| unsafe { ToNapiValue::to_napi_value(env, res) }); - if let Err(e) = result.and_then(|res| { + let release_tsfn_result = check_status!( + unsafe { + sys::napi_release_threadsafe_function(tsfn, sys::ThreadsafeFunctionReleaseMode::release) + }, + "Release threadsafe function in JsDeferred failed" + ); + + if let Err(e) = release_tsfn_result.and(result).and_then(|res| { check_status!( unsafe { sys::napi_resolve_deferred(env, deferred, res) }, "Resolve deferred value failed" diff --git a/examples/napi/__tests__/worker-thread.spec.ts b/examples/napi/__tests__/worker-thread.spec.ts index e0023c6b..856e2c99 100644 --- a/examples/napi/__tests__/worker-thread.spec.ts +++ b/examples/napi/__tests__/worker-thread.spec.ts @@ -8,15 +8,11 @@ import { Animal, Kind, DEFAULT_COST, shutdownRuntime } from '../index.cjs' const __dirname = join(fileURLToPath(import.meta.url), '..') -const t = - // aarch64-unknown-linux-gnu is extremely slow in CI, skip it or it will timeout - process.arch === 'arm64' && process.platform === 'linux' ? test.skip : test - const concurrency = (process.platform === 'win32' || process.platform === 'darwin' || (process.platform === 'linux' && - process.arch === 'x64' && + (process.arch === 'x64' || process.arch === 'arm64') && // @ts-expect-error process?.report?.getReport()?.header?.glibcVersionRuntime)) && !process.env.WASI_TEST && @@ -28,7 +24,7 @@ test.after(() => { shutdownRuntime() }) -t('should be able to require in worker thread', async (t) => { +test('should be able to require in worker thread', async (t) => { await Promise.all( Array.from({ length: concurrency }).map(() => { const w = new Worker(join(__dirname, 'worker.js'), { @@ -52,7 +48,7 @@ t('should be able to require in worker thread', async (t) => { ) }) -t('custom GC works on worker_threads', async (t) => { +test('custom GC works on worker_threads', async (t) => { await Promise.all( Array.from({ length: concurrency }).map(() => Promise.all([ @@ -95,7 +91,7 @@ t('custom GC works on worker_threads', async (t) => { ) }) -t('should be able to new Class in worker thread concurrently', async (t) => { +test('should be able to new Class in worker thread concurrently', async (t) => { await Promise.all( Array.from({ length: concurrency }).map(() => { const w = new Worker(join(__dirname, 'worker.js'), {