From 2fb168f75648d68c76ad5bb6f6cf2a801adcbb30 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 10 Jun 2026 23:17:30 +0200 Subject: [PATCH] src: do not track weak `BaseObject`s as childrens of `Realm`s Heap dumps are used for analyzing the relationship between objects that keep each other alive, so that retainers of memory can be detected and memory leaks fixed. 6e60ab744e6a13 broke this for a wide range of `BaseObject` instances. Marking all `BaseObject`s, including weak ones, as children of `Realm` and `Environment` instances gives the incorrect impression that they are held alive by those objects, effectively hiding the "real" set of strong roots that keep other objects alive through GC. An upcoming change in V8 will allow us to represent this relationship between `Realm` and `Environment` and its `BaseObject` and `CppgcMixin` instances properly. Refs: https://github.com/v8/v8/commit/e37cadf1143a8c5bbe44c0408186b5a26cc23863 Refs: https://github.com/nodejs/node/pull/57417 Signed-off-by: Anna Henningsen PR-URL: https://github.com/nodejs/node/pull/63842 Reviewed-By: Joyee Cheung Reviewed-By: Chengzhong Wu --- src/base_object.cc | 10 ++++++++-- src/cppgc_helpers.cc | 5 ++++- test/common/heap.js | 14 ++++++++++++++ test/pummel/test-heapdump-zlib.js | 14 +++++++++++++- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/base_object.cc b/src/base_object.cc index e93ca47341d..abef21b35b7 100644 --- a/src/base_object.cc +++ b/src/base_object.cc @@ -174,9 +174,15 @@ void BaseObjectList::Cleanup() { } } -void BaseObjectList::MemoryInfo(node::MemoryTracker* tracker) const { +void BaseObjectList::MemoryInfo(MemoryTracker* tracker) const { for (auto bo : *this) { - if (bo->IsDoneInitializing()) tracker->Track(bo); + if (bo->IsDoneInitializing()) { + // TODO(addaleax): Add weak edges instead of no edges once + // https://github.com/v8/v8/commit/e37cadf1143a8c5bbe44c0408186b5a26cc23863 + // is available for us + tracker->Track( + bo, bo->persistent().IsWeak() ? MemoryTracker::kWeakEdge : nullptr); + } } } diff --git a/src/cppgc_helpers.cc b/src/cppgc_helpers.cc index 4e3ffb678c2..7c557a822d2 100644 --- a/src/cppgc_helpers.cc +++ b/src/cppgc_helpers.cc @@ -16,7 +16,10 @@ void CppgcWrapperList::MemoryInfo(MemoryTracker* tracker) const { for (auto node : *this) { CppgcMixin* ptr = node->persistent.Get(); if (ptr != nullptr) { - tracker->Track(ptr); + // TODO(addaleax): Add weak edges instead of no edges once + // https://github.com/v8/v8/commit/e37cadf1143a8c5bbe44c0408186b5a26cc23863 + // is available for us + tracker->Track(ptr, MemoryTracker::kWeakEdge); } } } diff --git a/test/common/heap.js b/test/common/heap.js index ac7e090326c..6c83ca99782 100644 --- a/test/common/heap.js +++ b/test/common/heap.js @@ -336,6 +336,19 @@ function validateByRetainingPath(...args) { return validateByRetainingPathFromNodes(nodes, ...args); } +function getRetainingNodes(startingNode, filter) { + const seen = new Set(); + function listNodes(node) { + if (!filter(node) || seen.has(node)) return; + seen.add(node); + for (const edge of node.incomingEdges) { + listNodes(edge.from); + } + } + listNodes(startingNode); + return [...seen]; +} + module.exports = { recordState, validateSnapshotNodes, @@ -343,4 +356,5 @@ module.exports = { validateByRetainingPathFromNodes, getHeapSnapshotOptionTests, createJSHeapSnapshot, + getRetainingNodes, }; diff --git a/test/pummel/test-heapdump-zlib.js b/test/pummel/test-heapdump-zlib.js index 9f11fbcadad..f6ca5ceaab8 100644 --- a/test/pummel/test-heapdump-zlib.js +++ b/test/pummel/test-heapdump-zlib.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); -const { validateByRetainingPath, validateByRetainingPathFromNodes } = require('../common/heap'); +const { validateByRetainingPath, validateByRetainingPathFromNodes, getRetainingNodes } = require('../common/heap'); const zlib = require('zlib'); // Before zlib stream is created, no ZlibStream should be created. @@ -27,6 +27,18 @@ const gzip = zlib.createGzip(); assert.strictEqual(withMemory.length, 0); } +{ + // Assert that the `ZlibStream` has no unexpected connections + // to other `Node / ...` nodes. + const contexts = validateByRetainingPath('Node / ZlibContext', []); + assert.deepStrictEqual( + getRetainingNodes(contexts[0], (node) => node.name?.startsWith('Node /')) + .map((node) => node.name).sort(), [ + 'Node / ZlibContext', + 'Node / ZlibStream', + ]); +} + // After zlib stream is written, zlib_memory should be created. gzip.write('hello world', common.mustCall(() => { validateByRetainingPath('Node / ZlibStream', [