Skip to content

src: remove GCProfiler callbacks in stop() - #67799

Open
yohannesjoya wants to merge 2 commits into
nodejs:mainfrom
yohannesjoya:v8-gcprofiler-stop-callbacks
Open

yohannesjoya wants to merge 2 commits into
nodejs:mainfrom
yohannesjoya:v8-gcprofiler-stop-callbacks

Conversation

@yohannesjoya

@yohannesjoya yohannesjoya commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

GCProfiler::Stop() returned the profile but left the GC prologue and epilogue callbacks registered. Only the destructor removed them, and it runs when the binding object is collected. If the object had been promoted, that waits for the next mark-compact, and until then every collection calls SetHeapStatistics() twice per stopped profiler and appends the result to a stream nothing reads again.

This removes the callbacks in Stop(), and the destructor now only removes them if the profiler is still running. V8's GCCallbacks::Remove() expects the callback to be registered, so they mustn't be removed twice. The fix is the one @snovader suggested in the issue, so they're co-author.

Fixes: #67790

With the reproduction from the issue (100 start/stop cycles), allocation went from ~540 to ~2250 ns per call and RSS from 60 to 319 MB on main. With this change it stays at ~470 ns and 85 MB.

The new cctest checks that a stopped profiler records nothing more when a GC runs while its object is still alive. It fails without this change. The JS test checks that stopping one profiler leaves another running profiler's callbacks in place, and that collecting stopped profilers afterwards doesn't crash. The full cctest suite and tools/test.py parallel/test-v8-* pass locally, as do make format-cpp and cpplint.

AI disclosure: I used an AI assistant to help write the test and this description. I reproduced the issue, reviewed the change and ran the tests above myself.

GCProfiler::Stop() returned the profile but left the GC prologue and
epilogue callbacks registered. They were only removed by the destructor,
which runs when the binding object is collected. If the object had been
promoted, that waited for the next mark-compact, and until then every
collection called SetHeapStatistics() twice per stopped profiler and
appended the result to a stream that is never read again. Code that
starts and stops profilers repeatedly got slower and used more memory.

Remove the callbacks in Stop(), and only remove them in the destructor
if the profiler is still running.

Fixes: nodejs#67790
Co-authored-by: Marcus Ording <11672035+snovader@users.noreply.github.com>
Signed-off-by: Yohannes Desta <yohannesdestagebru10@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. v8 engine Issues and PRs related to the V8 dependency. labels Nov 26, 2026
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.51%. Comparing base (c99ebf2) to head (d0d3b28).
⚠️ Report is 472 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67799      +/-   ##
==========================================
- Coverage   92.99%   90.51%   -2.49%     
==========================================
  Files         428      805     +377     
  Lines      195633   285869   +90236     
  Branches    30281    55188   +24907     
==========================================
+ Hits       181937   258764   +76827     
- Misses      13369    17212    +3843     
- Partials      327     9893    +9566     
Files with missing lines Coverage Δ
src/node_v8.cc 86.56% <100.00%> (ø)

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this test is not checking what you want to fix, I don’t see a reliable way to test that from JS.
Could we add a C++ test that checks the profiler stops recording after stop()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a cctest in test/cctest/test_node_v8_gc_profiler.cc. It starts a profiler, forces a GC, calls stop(), then forces another GC while the profiler object is still alive and checks that nothing more was written to the profile stream. It fails without the change in src/node_v8.cc.

I kept the JS test because it covers the other half of the change: the destructor must not remove the callbacks again after stop(), since V8 expects them to still be registered. Happy to drop it if you think it's redundant.

Check from C++ that a stopped profiler no longer records collections
while its binding object is still alive. The JavaScript test cannot
observe this because the extra entries go to a stream that is not read
after stop().

Refs: nodejs#67790
Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
@yohannesjoya
yohannesjoya force-pushed the v8-gcprofiler-stop-callbacks branch from 3de94d7 to d0d3b28 Compare November 27, 2026 12:44
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. v8 engine Issues and PRs related to the V8 dependency.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v8.GCProfiler: stop() leaves the GC callbacks registered until the profiler is collected

3 participants