src: do not track weak BaseObjects as childrens of Realms

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.

6e60ab744e 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 <anna@addaleax.net>
PR-URL: https://github.com/nodejs/node/pull/63842
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Anna Henningsen authored and Node.js GitHub Bot committed 2026-06-13 23:12:35 +00:00
1 parent 17285d34de
commit 2fb168f756
4 files changed
+39 -4

No files matched your search

+8 -2
View File
@@ -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);
}
}
}
+4 -1
View File
@@ -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);
}
}
}
+14
View File
@@ -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,
};
+13 -1
View File
@@ -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', [