Skip to content

fix(test): run Node-API finalizers at test worker shutdown#35695

Merged
bartlomieju merged 1 commit into
mainfrom
fix/napi-finalizers-on-test-shutdown
Jul 1, 2026
Merged

fix(test): run Node-API finalizers at test worker shutdown#35695
bartlomieju merged 1 commit into
mainfrom
fix/napi-finalizers-on-test-shutdown

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

Native addons register finalizers via napi_wrap / napi_add_finalizer
that are meant to run at environment teardown, even when the wrapped value
was never garbage collected during the run. This is how Node behaves: at
process exit napi_env__::DeleteMe() calls RefTracker::FinalizeAll() over
every remaining reference, unconditionally, without forcing a GC between or
after test cases.

Deno already has the equivalent drain, MainWorker::run_napi_ref_finalizers(),
and calls it on the deno run and deno bench paths. The deno test path
tore down the worker without calling it, so any addon that relied on a
shutdown finalizer (for example cleanup like WAL checkpointing) never ran it
when its code was wrapped in a test. This calls the drain after the unload
event in the test path so it matches the run/bench paths and Node.

Adds a regression test that wraps an object inside a Deno.test, keeps it
reachable so GC never collects it, and asserts the finalizer still runs at
test worker shutdown. It mirrors the existing deno run variant.

Fixes #35692

The `deno run` and `deno bench` paths drain the NAPI finalizer queue via
`MainWorker::run_napi_ref_finalizers()` at shutdown, but the `deno test`
path did not, so finalizers registered with `napi_wrap`/`napi_add_finalizer`
never fired for wrapped values held alive until the test run ended.

Call `run_napi_ref_finalizers()` after the unload event in the test path,
matching the run/bench paths and Node.js.
@bartlomieju
bartlomieju merged commit c08eb33 into main Jul 1, 2026
136 checks passed
@bartlomieju
bartlomieju deleted the fix/napi-finalizers-on-test-shutdown branch July 1, 2026 23:13
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.

Deno does not call napi finalizers when running test suites

1 participant