GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lib: fix AbortSignal.any() observed-composite leak · nodejs/node@b95e5f9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • abort_controller.js
  • test/parallel
    • test-abortsignal-drop-settled-signals.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/abort_controller.js‎

Copy file name to clipboardExpand all lines: lib/internal/abort_controller.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ function abortSignal(signal, reason) {
522522
for (let i = 0; i < dependentSignalsToAbort.length; i++) {
523523
const dependentSignal = dependentSignalsToAbort[i];
524524
runAbort(dependentSignal);
525+
// A transitively-aborted dependent signal can never abort again, so there
526+
// is no longer any reason to keep it alive. Dropping it from
527+
// gcPersistentSignals lets it be collected, which in turn prunes its
528+
// WeakRef from its sources' kDependantSignals sets. Otherwise, an observed
529+
// composite that follows a long-lived source stays retained forever and
530+
// its entry accumulates in that source's kDependantSignals.
531+
gcPersistentSignals.delete(dependentSignal);
525532
}
526533

527534
// Clean up the signal from gcPersistentSignals
Collapse file

‎test/parallel/test-abortsignal-drop-settled-signals.mjs‎

Copy file name to clipboardExpand all lines: test/parallel/test-abortsignal-drop-settled-signals.mjs
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,37 @@ describe('when there is a long-lived signal', () => {
142142

143143
run(1);
144144
});
145+
146+
it('drops observed dependent signals once they are transitively aborted', async () => {
147+
const longLived = new AbortController();
148+
const handler = () => {};
149+
const size = () => {
150+
const sym = Object.getOwnPropertySymbols(longLived.signal).find(
151+
(s) => s.toString() === 'Symbol(kDependantSignals)'
152+
);
153+
return sym ? longLived.signal[sym].size : 0;
154+
};
155+
156+
// Each composite observes the long-lived source and a per-request source,
157+
// then is aborted through the per-request source without ever removing its
158+
// listener. The aborted composites can never fire again, so the long-lived
159+
// source's dependant set must not accumulate them. Using a helper keeps the
160+
// last iteration's signals from lingering on the stack for the assertion.
161+
const createObservedAbortedComposite = () => {
162+
const perReq = new AbortController();
163+
const composite = AbortSignal.any([perReq.signal, longLived.signal]);
164+
composite.addEventListener('abort', handler);
165+
perReq.abort();
166+
};
167+
for (let i = 0; i < limit; i++) {
168+
createObservedAbortedComposite();
169+
}
170+
171+
await gcUntil(
172+
'observed dependents are dropped after transitive abort',
173+
() => size() === 0,
174+
);
175+
});
145176
});
146177

147178
it('does not prevent source signal from being GCed if it is short-lived', (t, done) => {

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.