fix(ext/napi): call wrap/ref finalizers at shutdown#32592
Merged
bartlomieju merged 5 commits intodenoland:mainfrom Mar 12, 2026
Merged
fix(ext/napi): call wrap/ref finalizers at shutdown#32592bartlomieju merged 5 commits intodenoland:mainfrom
bartlomieju merged 5 commits intodenoland:mainfrom
Conversation
Run NAPI wrap, external, and add_finalizer callbacks during environment teardown while V8 is still alive, matching Node.js's RefTracker::FinalizeAll() behavior. Previously these finalizers only ran via GC weak callbacks, which never fire for objects still reachable at exit. This fixes native addons like DuckDB that rely on destructor cleanup (e.g. WAL checkpointing) during process exit. Closes denoland#27468 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… test - Don't register instance data finalizers in ref_tracker since they already have their own teardown path in NapiState::Drop - Rename wrap_leak_test.js to wrap_leak.js so deno test doesn't pick it up (it's a standalone script, not a test file) - Simplify instance_data_test.js assertion Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
kajukitli
approved these changes
Mar 10, 2026
Contributor
kajukitli
left a comment
There was a problem hiding this comment.
lgtm, important fix for native addons like DuckDB
the approach is solid:
- track finalizers in
PendingNapiFinalizerlist shared betweenNapiStateandEnv - call them LIFO at shutdown with a valid
HandleScope(so V8 is still alive) - deregister when GC fires naturally via
remove_ref_finalizerto avoid double-calling
matches Node.js's RefTracker::FinalizeAll() behavior.
one minor note: remove_ref_finalizer uses rposition which is O(n) scan. for addons with many refs this could add up, but realistically most addons don't have thousands of wrapped objects so it's fine.
the test coverage is good — wrap_leak.js verifies finalizers run at shutdown even when objects are still reachable.
littledivy
approved these changes
Mar 12, 2026
littledivy
approved these changes
Mar 12, 2026
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.
napi_wrap,napi_create_external,napi_add_finalizer,napi_set_instance_data) in aPendingNapiFinalizerlist shared betweenNapiStateandEnvMainWorkershutdown while V8 is still alive with a properHandleScope, matching Node.js'sRefTracker::FinalizeAll()behaviorThis fixes native addons like DuckDB that rely on C++ destructor cleanup (e.g. WAL checkpointing) during process exit. Previously these finalizers only ran via V8 GC weak callbacks, which never fire for objects still reachable at exit.
Closes #27468