lib: fix AbortSignal.any() observed-composite leak

An observed composite signal (one with an `abort` listener) is added to
gcPersistentSignals so it stays alive long enough to fire. When one of
its sources aborted, the composite was marked aborted but never removed
from that set, so it was retained forever and its WeakRef was never
pruned from a long-lived source's kDependantSignals. That set therefore
grew without bound.

Drop each transitively-aborted dependent from gcPersistentSignals, the
same way the directly-aborted signal is already removed, so it can be
collected and its dependant entries pruned.

Fixes: https://github.com/nodejs/node/issues/64476
Signed-off-by: Paul Bouchon <mail@bitpshr.net>
PR-URL: https://github.com/nodejs/node/pull/64481
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com>
This commit is contained in:
Paul Bouchon authored and Antoine du Hamel committed 2026-08-06 16:00:18 +02:00
1 parent 539f5dc8f9
commit 06b54fe7d3
2 files changed
+38

No files matched your search

+7
View File
@@ -522,6 +522,13 @@ function abortSignal(signal, reason) {
for (let i = 0; i < dependentSignalsToAbort.length; i++) {
const dependentSignal = dependentSignalsToAbort[i];
runAbort(dependentSignal);
// A transitively-aborted dependent signal can never abort again, so there
// is no longer any reason to keep it alive. Dropping it from
// gcPersistentSignals lets it be collected, which in turn prunes its
// WeakRef from its sources' kDependantSignals sets. Otherwise, an observed
// composite that follows a long-lived source stays retained forever and
// its entry accumulates in that source's kDependantSignals.
gcPersistentSignals.delete(dependentSignal);
}
// Clean up the signal from gcPersistentSignals
@@ -142,6 +142,37 @@ describe('when there is a long-lived signal', () => {
run(1);
});
it('drops observed dependent signals once they are transitively aborted', async () => {
const longLived = new AbortController();
const handler = () => {};
const size = () => {
const sym = Object.getOwnPropertySymbols(longLived.signal).find(
(s) => s.toString() === 'Symbol(kDependantSignals)'
);
return sym ? longLived.signal[sym].size : 0;
};
// Each composite observes the long-lived source and a per-request source,
// then is aborted through the per-request source without ever removing its
// listener. The aborted composites can never fire again, so the long-lived
// source's dependant set must not accumulate them. Using a helper keeps the
// last iteration's signals from lingering on the stack for the assertion.
const createObservedAbortedComposite = () => {
const perReq = new AbortController();
const composite = AbortSignal.any([perReq.signal, longLived.signal]);
composite.addEventListener('abort', handler);
perReq.abort();
};
for (let i = 0; i < limit; i++) {
createObservedAbortedComposite();
}
await gcUntil(
'observed dependents are dropped after transitive abort',
() => size() === 0,
);
});
});
it('does not prevent source signal from being GCed if it is short-lived', (t, done) => {