Skip to content

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

Open
yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:v8-gcprofiler-stop-callbacks
Open

yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:v8-gcprofiler-stop-callbacks

Conversation

@yohannesjoya

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 effect is only visible as GC cost and native memory, so the new test doesn't time anything. It checks that stopping one profiler leaves another running profiler's callbacks in place, and that collecting stopped profilers afterwards doesn't crash. tools/test.py parallel/test-v8-* passes locally (3 repeats), 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.49%. Comparing base (c99ebf2) to head (e45a007).
⚠️ Report is 459 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67799      +/-   ##
==========================================
- Coverage   92.99%   90.49%   -2.51%     
==========================================
  Files         428      805     +377     
  Lines      195633   285820   +90187     
  Branches    30281    55173   +24892     
==========================================
+ Hits       181937   258654   +76717     
- Misses      13369    17255    +3886     
- Partials      327     9911    +9584     
Files with missing lines Coverage Δ
src/node_v8.cc 86.56% <100.00%> (ø)

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

2 participants