fix(ext/napi): defer GC weak-callback finalizers to the event loop#33260
Open
bartlomieju wants to merge 1 commit intomainfrom
Open
fix(ext/napi): defer GC weak-callback finalizers to the event loop#33260bartlomieju wants to merge 1 commit intomainfrom
bartlomieju wants to merge 1 commit intomainfrom
Conversation
Previously, napi Reference weak-callback finalizers were called directly during V8 GC. This is unsafe because user-provided finalizer callbacks can re-enter V8, allocate GC'd objects, or trigger cascading weak callbacks — all of which lead to undefined behavior during a GC pause. Node.js avoids this by deferring finalizers via EnqueueFinalizer so they run on the next event loop tick. This commit adopts the same pattern using V8CrossThreadTaskSpawner to schedule deferred finalizer execution. Also implements node_api_post_finalizer which was previously a no-op. Ref #33137 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
miracatbot
approved these changes
Apr 13, 2026
miracatbot
left a comment
There was a problem hiding this comment.
Looks good to me.
Deferring weak-callback finalizers off the V8 GC path is the right fix here. The callback/data/env handling in Reference::weak_callback() looks careful, and removing the finalizer from the ref tracker before scheduling should avoid double-finalization during shutdown.
The regression test also checks the important behavioral contract: not during gc(), yes after yielding back to the event loop.
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.
Summary
V8CrossThreadTaskSpawner, matching Node.js'sEnqueueFinalizerpatternnode_api_post_finalizer(was a no-op)Background
Deep comparison of Node-API implementations between Node.js and Deno revealed that Deno was calling user-provided finalizer callbacks directly during V8 GC pauses. This is unsafe — finalizers can re-enter V8, allocate GC'd objects, or trigger cascading weak callbacks, all of which are undefined behavior during a GC pause. Node.js avoids this by deferring finalizers via
EnqueueFinalizerto the event loop.This is the most likely root cause of the intermittent
napi_unwrapfailures reported in #33137 (Vite 8 / rolldown builds). The timing-dependent nature of the bug matches: it only manifests when GC fires at a particular moment during napi class instance lifecycle.Test plan
deferred_finalizer_test.js: creates an external with a finalizer, triggers GC, asserts finalizer has NOT run yet, yields to event loop, asserts finalizer HAS runcross_env_test.js)cargo check,tools/format.js,tools/lint.jsall cleanRef #33137
🤖 Generated with Claude Code