Skip to content

[v24.x backport] src: detach cppgc wrappers from their Realm before it is freed - #66168

Open
inoway46 wants to merge 1 commit into
nodejs:v24.x-stagingfrom
inoway46:backport-65778-to-v24.x
Open

inoway46 wants to merge 1 commit into
nodejs:v24.x-stagingfrom
inoway46:backport-65778-to-v24.x

Conversation

@inoway46

Copy link
Copy Markdown
Contributor

Backport of #65778 to v24.x, preserving the v24 V8 APIs.

The crash was reproduced on v24.20.0; the backported fix passed the Windows stress reproduction.

Refs: #65756 (comment)

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/realm
  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. v24.x Issues that can be reproduced on v24.x or PRs targeting the v24.x-staging branch. labels Sep 21, 2026
@inoway46
inoway46 marked this pull request as ready for review September 21, 2026 07:46
@inoway46
inoway46 force-pushed the backport-65778-to-v24.x branch from eea6a5e to adbbfa8 Compare September 21, 2026 08:44
`Realm::RunCleanup()` finalizes the cppgc-managed wrappers it tracks
so that none of them touches the Realm once it is gone, but it reaches
them through weak persistents, and the GC clears those as soon as it
finds a wrapper dead. With lazy and concurrent sweeping the destructor
can run much later, so a wrapper collected shortly before
`FreeEnvironment()` and swept after it was skipped by the cleanup and
kept its `realm_`: `~CppgcMixin()` then wrote
`should_purge_empty_cppgc_wrappers_` into the freed Realm, and a
subclass destructor calling `Finalize()` as documented would have
called `Clean()` with a dangling Realm. A Worker that compiles a few
`vm.Script`s, gets a full GC from external memory pressure and calls
`process.exit()` is enough to hit the first case.

Move the Realm pointer into the list node, which the wrapper now owns
and deletes in its destructor. `CppgcWrapperList::Cleanup()` unlinks
every node, finalizing the wrappers that are still alive and clearing
the Realm pointer for the collected ones, which only their own
destructor may still touch. `Realm::PendingCleanup()` accounts for the
list so it is always drained. The purge flag, its GC epilogue callback
and `PurgeEmpty()` are no longer needed, and removing them also stops
the list nodes of wrappers that are alive at `FreeEnvironment()` from
leaking.

Refs: nodejs#56534
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
PR-URL: nodejs#65778
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@inoway46
inoway46 force-pushed the backport-65778-to-v24.x branch from adbbfa8 to 0659664 Compare September 21, 2026 11:51
@codecov

codecov Bot commented Sep 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.23%. Comparing base (9e39360) to head (0659664).
⚠️ Report is 434 commits behind head on v24.x-staging.

Files with missing lines Patch % Lines
src/cppgc_helpers-inl.h 72.72% 0 Missing and 3 partials ⚠️
src/node_realm.cc 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@                Coverage Diff                @@
##           v24.x-staging   #66168      +/-   ##
=================================================
- Coverage          90.31%   90.23%   -0.08%     
=================================================
  Files                711      714       +3     
  Lines             228425   234484    +6059     
  Branches           43160    44625    +1465     
=================================================
+ Hits              206293   211588    +5295     
- Misses             14090    14473     +383     
- Partials            8042     8423     +381     
Files with missing lines Coverage Δ
src/cppgc_helpers.cc 92.85% <100.00%> (+1.19%) ⬆️
src/cppgc_helpers.h 90.00% <100.00%> (-3.34%) ⬇️
src/node_realm-inl.h 92.06% <100.00%> (+1.89%) ⬆️
src/node_realm.h 100.00% <ø> (ø)
src/node_realm.cc 72.95% <0.00%> (-1.30%) ⬇️
src/cppgc_helpers-inl.h 82.35% <72.72%> (-3.86%) ⬇️

... and 221 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@inoway46

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. v24.x Issues that can be reproduced on v24.x or PRs targeting the v24.x-staging branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants