Skip to content

Commit ed41d04

Browse files
author
lazerg
committed
lib: keep AbortSignal.any() timeout sources alive
Signed-off-by: lazerg <lazerg2@gmail.com> Assisted-by: Claude
1 parent 855e3f7 commit ed41d04

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎lib/internal/abort_controller.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,10 @@ class AbortSignal extends EventTarget {
412412
[kRemoveListener](size, type, listener, capture) {
413413
super[kRemoveListener](size, type, listener, capture);
414414
const isTimeoutOrNonEmptyCompositeSignal = this[kTimeout] || (this[kComposite] && this[kSourceSignals]?.size);
415-
if (isTimeoutOrNonEmptyCompositeSignal && type === 'abort' && size === 0) {
415+
if (isTimeoutOrNonEmptyCompositeSignal &&
416+
type === 'abort' &&
417+
size === 0 &&
418+
!this[kDependantSignals]?.size) {
416419
gcPersistentSignals.delete(this);
417420
}
418421
}

‎test/parallel/test-abort-controller-any-timeout.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,39 @@ describe('AbortSignal.any() with timeout signals', () => {
3939
clearTimeout(timeout);
4040
}
4141
});
42+
43+
it('should abort after a listener is removed from the timeout signal', async () => {
44+
const signal = (() => {
45+
const timeoutSignal = AbortSignal.timeout(common.platformTimeout(100));
46+
const anySignal = AbortSignal.any([timeoutSignal]);
47+
const listener = () => {};
48+
timeoutSignal.addEventListener('abort', listener);
49+
timeoutSignal.removeEventListener('abort', listener);
50+
return anySignal;
51+
})();
52+
let timeout;
53+
54+
const abortPromise = Promise.race([
55+
once(signal, 'abort').then(() => {
56+
throw signal.reason;
57+
}),
58+
new Promise((resolve) => {
59+
timeout = setTimeout(resolve, common.platformTimeout(10000));
60+
}),
61+
]);
62+
63+
setImmediate(common.mustCall(() => globalThis.gc()));
64+
65+
try {
66+
await assert.rejects(
67+
() => abortPromise,
68+
{
69+
name: 'TimeoutError',
70+
message: 'The operation was aborted due to timeout'
71+
}
72+
);
73+
} finally {
74+
clearTimeout(timeout);
75+
}
76+
});
4277
});

0 commit comments

Comments
 (0)