Files
node/test/parallel/test-async-local-storage-weak-asyncwrap-leak.js
Anna Henningsen 6ad5f7cb80 src: convert context_frame field in AsyncWrap to internal field
Using an internal field instead of a `v8::Global<>` removes
an unnecessary memory leak footgun.

This includes a test that demonstrates the issue, albeit
using internal APIs.

It's worth noting that if this PR is not accepted, we'd still
be missing memory tracking for the `context_frame_` field,
and we'd need to add it through our memory tracking API.

PR-URL: https://github.com/nodejs/node/pull/62103
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2026-03-06 16:44:34 +00:00

50 lines
1.5 KiB
JavaScript

// Flags: --expose-gc
'use strict';
const common = require('../common');
const assert = require('node:assert');
const zlib = require('node:zlib');
const v8 = require('node:v8');
const { AsyncLocalStorage } = require('node:async_hooks');
// This test verifies that referencing an AsyncLocalStorage store from
// a weak AsyncWrap does not prevent the store from being garbage collected.
// We use zlib streams as examples of weak AsyncWraps here, but the
// issue is not specific to zlib.
class Store {}
let zlibDone = false;
// Use immediates to ensure no accidental async context propagation occurs
setImmediate(common.mustCall(() => {
const asyncLocalStorage = new AsyncLocalStorage();
const store = new Store();
asyncLocalStorage.run(store, common.mustCall(() => {
(async () => {
const zlibStream = zlib.createGzip();
// (Make sure this test does not break if _handle is renamed
// to something else)
assert.strictEqual(typeof zlibStream._handle, 'object');
// Create backreference from AsyncWrap to store
store.zlibStream = zlibStream._handle;
// Let the zlib stream finish (not strictly necessary)
zlibStream.end('hello world');
await zlibStream.toArray();
zlibDone = true;
})().then(common.mustCall());
}));
}));
const finish = common.mustCall(() => {
global.gc();
// Make sure the ALS instance has been garbage-collected
assert.strictEqual(v8.queryObjects(Store), 0);
});
const interval = setInterval(() => {
if (zlibDone) {
clearInterval(interval);
finish();
}
}, 5);