Skip to content

fix(ext/napi): call wrap/ref finalizers at shutdown#32592

Merged
bartlomieju merged 5 commits intodenoland:mainfrom
bartlomieju:fix/napi-wrap-finalizer-shutdown
Mar 12, 2026
Merged

fix(ext/napi): call wrap/ref finalizers at shutdown#32592
bartlomieju merged 5 commits intodenoland:mainfrom
bartlomieju:fix/napi-wrap-finalizer-shutdown

Conversation

@bartlomieju
Copy link
Copy Markdown
Member

@bartlomieju bartlomieju commented Mar 9, 2026

  • Track NAPI finalizer callbacks (from napi_wrap, napi_create_external, napi_add_finalizer, napi_set_instance_data) in a PendingNapiFinalizer list shared between NapiState and Env
  • Call tracked finalizers in LIFO order during MainWorker shutdown while V8 is still alive with a proper HandleScope, matching Node.js's RefTracker::FinalizeAll() behavior
  • When finalizers fire naturally via GC weak callbacks, they deregister from the tracker to avoid double-calling

This 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

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>
@nathanwhit nathanwhit requested a review from devsnek March 9, 2026 17:44
bartlomieju and others added 3 commits March 10, 2026 17:35
… 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>
Copy link
Copy Markdown
Contributor

@kajukitli kajukitli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, important fix for native addons like DuckDB

the approach is solid:

  • track finalizers in PendingNapiFinalizer list shared between NapiState and Env
  • call them LIFO at shutdown with a valid HandleScope (so V8 is still alive)
  • deregister when GC fires naturally via remove_ref_finalizer to 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.

@bartlomieju bartlomieju merged commit aa6ee7d into denoland:main Mar 12, 2026
112 checks passed
@bartlomieju bartlomieju deleted the fix/napi-wrap-finalizer-shutdown branch March 12, 2026 16: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.

Behavior is different from node with duckdb library

3 participants