Skip to content

src: stop leaking a CppHeap in CommonEnvironmentSetup - #65792

Open
codebytere wants to merge 1 commit into
nodejs:mainfrom
codebytere:fix/embedder-setup-cppheap-leak
Open

src: stop leaking a CppHeap in CommonEnvironmentSetup#65792
codebytere wants to merge 1 commit into
nodejs:mainfrom
codebytere:fix/embedder-setup-cppheap-leak

Conversation

@codebytere

Copy link
Copy Markdown
Member

CommonEnvironmentSetup's constructor created a v8::CppHeap for its Isolate::CreateParams up front, but on the non-snapshotting path it then calls NewIsolate(), which ignores params->cpp_heap and attaches either the heap from IsolateSettings or a fresh one. The heap created first was never attached to an isolate and never destroyed, so every CommonEnvironmentSetup::Create() / CreateFromSnapshot() leaked one CppHeap. cppgc's process-wide heap registry keeps a pointer to it, which is why LeakSanitizer does not report it; counting CppHeap constructions against destructions over two setups shows 4 against 2 before this change and 2 against 2 after.

The heap is now only created on the snapshotting path, where the params are handed to the SnapshotCreator and the heap does get attached.

Tests: existing test/embedding suite; no new test, since there is no public API to enumerate detached CppHeaps.

Refs: #55337


Disclosure: the code and this description were written by Claude Code, directed and reviewed by @codebytere.

`CommonEnvironmentSetup` created a `CppHeap` for its `CreateParams`
before deciding how to create the isolate, but `NewIsolate()` ignores
`params->cpp_heap` and attaches a heap of its own (or the one from
`IsolateSettings`). The first heap was never attached or destroyed, so
every non-snapshotting setup leaked one `CppHeap`; cppgc's heap
registry keeps it reachable, which is why LSAN stays quiet about it.

Only create the heap on the snapshotting path, where the params go to
the `SnapshotCreator` directly.

Refs: nodejs#55337
Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Sep 4, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@codecov

codecov Bot commented Sep 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.15%. Comparing base (2befec5) to head (455f726).
⚠️ Report is 77 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65792      +/-   ##
==========================================
+ Coverage   89.99%   90.15%   +0.15%     
==========================================
  Files         757      769      +12     
  Lines      257739   261448    +3709     
  Branches    48881    49661     +780     
==========================================
+ Hits       231961   235715    +3754     
+ Misses      16861    16749     -112     
- Partials     8917     8984      +67     
Files with missing lines Coverage Δ
src/api/embed_helpers.cc 75.00% <100.00%> (ø)

... and 101 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.

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++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants