Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: clean up persisted signals when they are settled #56001

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,21 @@ const dependantSignalsCleanupRegistry = new SafeFinalizationRegistry((signalWeak
}
});
});

const gcPersistentSignals = new SafeSet();

const finalizer = new SafeFinalizationRegistry(({ sourceSignalRef, composedSignalRef }) => {
const composedSignal = composedSignalRef.deref();
if (composedSignal !== undefined) {
composedSignal[kSourceSignals].delete(sourceSignalRef);

if (composedSignal[kSourceSignals].size === 0) {
// This signal will no longer abort. There's no need to keep it in the gcPersistentSignals set.
gcPersistentSignals.delete(composedSignal);
}
}
});

const kAborted = Symbol('kAborted');
const kReason = Symbol('kReason');
const kCloneData = Symbol('kCloneData');
Expand Down Expand Up @@ -258,6 +271,7 @@ class AbortSignal extends EventTarget {
resultSignal[kSourceSignals].add(signalWeakRef);
signal[kDependantSignals].add(resultSignalWeakRef);
dependantSignalsCleanupRegistry.register(resultSignal, signalWeakRef);
finalizer.register(signal, { sourceSignalRef: signalWeakRef, composedSignalRef: resultSignalWeakRef });
} else if (!signal[kSourceSignals]) {
continue;
} else {
Expand Down Expand Up @@ -434,6 +448,7 @@ class AbortController {
*/
get signal() {
this.#signal ??= new AbortSignal(kDontThrowSymbol);

return this.#signal;
}

Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-abortsignal-drop-settled-signals.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ function runShortLivedSourceSignal(limit, done) {
run(1);
};

function runWithOrphanListeners(limit, done) {
let composedSignalRef;
const composedSignalRefs = [];
const handler = () => { };

function run(iteration) {
const ac = new AbortController();
if (iteration > limit) {
setImmediate(() => {
global.gc();
setImmediate(() => {
global.gc();

done(composedSignalRefs);
});
});
return;
}

composedSignalRef = new WeakRef(AbortSignal.any([ac.signal]));
composedSignalRef.deref().addEventListener('abort', handler);

composedSignalRefs.push(composedSignalRef);

setImmediate(() => {
run(iteration + 1);
});
}

run(1);
}

const limit = 10_000;

describe('when there is a long-lived signal', () => {
Expand Down Expand Up @@ -120,3 +152,17 @@ it('drops settled dependant signals when signal is composite', (t, done) => {
});
});
});

it('drops settled signals even when there are listeners', (t, done) => {
runWithOrphanListeners(limit, (signalRefs) => {
setImmediate(() => {
global.gc();

const unGCedSignals = [...signalRefs].filter((ref) => ref.deref());

t.assert.strictEqual(unGCedSignals.length, 0);

done();
});
});
});
Loading