src: let Environments on one isolate share a cleanup hook - #65777
Open
codebytere wants to merge 1 commit into
Open
src: let Environments on one isolate share a cleanup hook#65777codebytere wants to merge 1 commit into
codebytere wants to merge 1 commit into
Conversation
Collaborator
codebytere
force-pushed
the
fix/embedder-cleanup-hook-registry
branch
from
September 4, 2026 08:47
c16a038 to
61f728b
Compare
Collaborator
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65777 +/- ##
==========================================
- Coverage 90.17% 90.16% -0.02%
==========================================
Files 771 771
Lines 265097 265111 +14
Branches 50362 50371 +9
==========================================
- Hits 239054 239027 -27
- Misses 17004 17014 +10
- Partials 9039 9070 +31
🚀 New features to boost your workflow:
|
Collaborator
codebytere
force-pushed
the
fix/embedder-cleanup-hook-registry
branch
from
September 4, 2026 17:19
61f728b to
df3574c
Compare
Collaborator
Collaborator
Collaborator
The registry behind `AddEnvironmentCleanupHook()` is keyed on
{isolate, fun, arg} and asserts that every insertion is unique. Two
Environments on one isolate that register the same hook, which the
Node-API documentation allows per environment, abort the process on
the second `napi_add_env_cleanup_hook()`.
Key the registry on `arg` only and tell entries apart by Environment:
adding the same hook to one Environment twice still aborts as
documented, and removal prefers the current Environment's registration,
falling back to a matching one from another Environment when there is
no current context. Because the entry to remove after a hook has run can
no longer be found by {isolate, fun, arg} alone, `CleanupHookThunkRun()`
marks its entry as running and erases exactly that entry afterwards; a
removal of a running entry (a hook removing itself, as `~ObjectWrap()`
does) is a no-op, which keeps the use-after-free fixed by nodejs#65630 fixed.
Refs: nodejs#63985
Refs: nodejs#65630
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
codebytere
force-pushed
the
fix/embedder-cleanup-hook-registry
branch
from
September 7, 2026 22:03
df3574c to
ed4bf45
Compare
Collaborator
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.
Two Environments that share an isolate and register the same cleanup hook abort the process on the second
napi_add_env_cleanup_hook(), which is a normal thing for an addon loaded into several Environments to do (Electron subframes and same-process child windows share Blink's isolate). The process-global registry from #63985 is keyed on{isolate, fun, arg}and CHECKs that every insertion is unique.The registry is now keyed on
argwith the Environment carried in each entry: the same hook twice in one Environment still aborts as documented, removal prefers the current Environment's entry and falls back to a matching one, and a running hook's entry is erased byCleanupHookThunkRun()itself so a hook that removes itself (as~ObjectWrap()does) stays safe, keeping #65630 fix intact.Tests:
EnvironmentTest.SameCleanupHookInTwoEnvironmentsOnOneIsolate(aborted before),RemoveCleanupHookOfOtherEnvironmentOnSameIsolate,CleanupHookRemovesItselfWhileRunning;test/addons,test/node-apiandtest/js-native-apipass.Refs: #63985
Refs: #65630
Disclosure: the code, tests and this description were written by Claude Code, directed and reviewed by @codebytere.