Repository navigation
fix(napi): hand back threadsafe-function payloads accepted before worker teardown - #50
Merged
Merged
Conversation
…ker teardown Adapt the checkpoint-before-dequeue ordering from oven-sh#36831. Keep accepted payloads and the native callback available to worker cleanup when a microtask stops the VM, and make the live-env dispatch invariant explicit. Cover bounded and unbounded queues, worker exit and termination, abort, and native release racing teardown. Validate against Node 24.21.0 and the native watcher stress harness.
Return a dequeued payload through the native NULL-env callback if its env is absent, without panicking or silently dropping it. Reuse the hand-back helper with a one-item iterator. Drain the previous callback microtasks even when that callback aborted the TSFN, matching Node 24 CallbackScope. Add bounded and unbounded synchronous-abort ordering regressions.
steipete
force-pushed
the
fix/napi-tsfn-teardown-ownership
branch
from
October 1, 2026 11:38
a1c0535 to
dd29905
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes accepted Node-API threadsafe-function payloads being lost when a worker stops in the microtask checkpoint between callbacks. The checkpoint now runs before dequeueing or changing last-item state, with no TSFN mutex held. A stopped checkpoint leaves the payload and native callback available to worker teardown.
If
call()has no env, it returns its owned payload throughcall_js(NULL, NULL, context, data)using the existing hand-back helper. This path neither panics nor silently discards a custom native callback's payload. The helper accepts an iterator so the one-item fallback needs no allocation. Existing live-env worker-cleanup delivery and NULL-env abort delivery remain intact.The checkpoint also runs when the preceding callback aborted the TSFN. Node 24.21.0 makes the distinction explicit:
Releasechanges the TSFN tokClosing; it does not stop the environment.DispatchOnestops dequeueing once closing, but every already-popped call has anAsyncResource::CallbackScope(lines 452–463).InternalCallbackScope::Closedrains that callback's microtasks unless the environment is stopping. TSFN closing is not a checkpoint gate.EmptyQueuereturns leftovers with NULL env/callback;Finalizedoes that before the finalizer.Accordingly, the
!self.is_closing()checkpoint gate is removed. Two new native regressions synchronously abort from the first callback and verify that its microtask runs before queued payloads are handed back. With the gate, zero of the seven/unbounded or three/bounded hand-backs observe the microtask; Node 24.21.0 and this revision observe all of them. The first callback still has no preceding checkpoint.The original 21 cases cover two/eight payloads, bounded/unbounded queues, natural worker exit,
process.exit(),terminate(), last-thread release, abort, and a native release racing cleanup. They assert exact ownership at finalization and after worker exit, no duplicate/late delivery, and no remaining TSFN allocation.The missing-env branch cannot be reached deterministically through a valid public N-API call: dispatch checks closing before dequeueing, teardown closes/drains before clearing the env, and no checkpoint remains between dequeue and
call. No fault-injection API was added. The same NULL-env hand-back helper is exercised directly by the abort cases.Adapts the pre-dequeue checkpoint ordering from open oven-sh/bun#36831, with thanks to @robobun, independently of its broader nested-dispatch changes. Related upstream lifecycle fixes oven-sh#39810, oven-sh#39848, and oven-sh#42797 remain intact.
How did you verify your code works?
Fresh Linux x64 release builds on AWS (
bun scripts/build.ts --profile=release --lto=off). Node 24.21.0 passes all 23 native cases. The previous PR runtime passes the original 21 but fails both new abort-ordering cases; the revised runtime passes all 23.Rebased onto
openclaw/mainat8521c9dd3c747afe5add9f60149d22448cf51d13, preserving both changelog entries. Tested head:dd299054ad9cae6f04d2fdbb2997b137cb397c09.test/napiworker_threadsBUN_JSC_validateExceptionChecks=1CI attached to this exact head: OpenClaw build and test, Format, Lint, Source lints. All four completed successfully on this exact head.
The two-payload worker-exit reproduction passes 25/25 runs. Native inotify stress passes all nine scenarios, including 96 workers and 1,152 registrations with no live payloads, TSFNs, registrations, pending sets, or hub threads afterward. The native addon was rebuilt from its current main source for this proof.
Rust formatting and the fork branch's independent Codex P2 review pass. macOS remains pending prerelease asset proof; no macOS build was run for this change.
Known failures (fork issues are disabled): no remaining failures in the completed AWS suites. Existing todos/skips are listed above.
Upstream PR: oven-sh#44378