Skip to content

Commit

Permalink
test: test clean up settled signals
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Nov 26, 2024
1 parent 86f7b92 commit 643efe1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ const dependantSignalsCleanupRegistry = new SafeFinalizationRegistry((signalWeak
const gcPersistentSignals = new SafeSet();

const finalizer = new SafeFinalizationRegistry(({ sourceSignalRef, composedSignalRef }) => {
// TODO: remove ref from source signal
// TODO: remove composed signal from gcPersistentSignals
const composedSignal = composedSignalRef.deref();
if (composedSignal !== undefined) {
composedSignal[kSourceSignals].delete(sourceSignalRef);
gcPersistentSignals.delete(composedSignal);

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);
}
}

// TODO: remove ref from dependant signal
const sourceSignal = sourceSignalRef.deref();
if (sourceSignal !== undefined) {
sourceSignal[kDependantSignals].delete(composedSignalRef);
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();
});
});
});

0 comments on commit 643efe1

Please sign in to comment.