Skip to content

fix(napi): hand back threadsafe-function payloads accepted before worker teardown - #50

Merged
steipete merged 2 commits into
mainfrom
fix/napi-tsfn-teardown-ownership
Oct 1, 2026
Merged

steipete merged 2 commits into
mainfrom
fix/napi-tsfn-teardown-ownership

Conversation

@steipete

@steipete steipete commented Oct 1, 2026 •

Copy link
Copy Markdown

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 through call_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:

  • Release changes the TSFN to kClosing; it does not stop the environment.
  • DispatchOne stops dequeueing once closing, but every already-popped call has an AsyncResource::CallbackScope (lines 452–463).
  • InternalCallbackScope::Close drains that callback's microtasks unless the environment is stopping. TSFN closing is not a checkpoint gate.
  • EmptyQueue returns leftovers with NULL env/callback; Finalize does 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/main at 8521c9dd3c747afe5add9f60149d22448cf51d13, preserving both changelog entries. Tested head: dd299054ad9cae6f04d2fdbb2997b137cb397c09.

Check Result
Ownership matrix + abort ordering 23 pass
Full test/napi 671 pass, 22 existing todo, 0 fail
worker_threads 197 pass, 1 existing skip, 0 fail
Web workers 43 pass
Source lints 185 pass
Ownership/order/exception cases with BUN_JSC_validateExceptionChecks=1 25 pass

CI 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

…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
steipete force-pushed the fix/napi-tsfn-teardown-ownership branch from a1c0535 to dd29905 Compare October 1, 2026 11:38
@steipete
steipete merged commit ec5e3f3 into main Oct 1, 2026
11 checks passed
@steipete
steipete deleted the fix/napi-tsfn-teardown-ownership branch October 1, 2026 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant