Commit aafad9009ab for nodejs

commit aafad9009ab1391ff668e34e2e6b98bdfae5bc89
Author: Pushkar Kirange <71223317+Pushkar236@users.noreply.github.com>
Date:   Fri Dec 4 21:53:51 2026 +0530

    lib: keep AbortSignal.any() timeout sources alive

    Removing the last abort listener from a timeout signal also removed it
    from the signals kept alive until they abort, even when an
    AbortSignal.any() composite still depended on it. The timeout could
    then be collected before it fired, and the composite never aborted.

    Only release the signal when no composite depends on it.

    Fixes: https://github.com/nodejs/node/issues/66610
    Refs: https://github.com/nodejs/node/pull/66615
    Signed-off-by: lazerg <lazerg2@gmail.com>
    Signed-off-by: Pushkar Kirange <71223317+Pushkar236@users.noreply.github.com>
    PR-URL: https://github.com/nodejs/node/pull/67830
    Reviewed-By: Chemi Atlow <chemi@atlow.co.il>

diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js
index b47af84a598..8640044d8f8 100644
--- a/lib/internal/abort_controller.js
+++ b/lib/internal/abort_controller.js
@@ -448,7 +448,12 @@ class AbortSignal extends EventTarget {
   [kRemoveListener](size, type, listener, capture) {
     FunctionPrototypeCall(eventTargetRemoveListener, this, size, type, listener, capture);
     const isTimeoutOrNonEmptyCompositeSignal = this[kTimeout] || (this[kComposite] && this[kSourceSignals]?.size);
-    if (isTimeoutOrNonEmptyCompositeSignal && type === 'abort' && size === 0) {
+    // A signal that composites depend on must stay alive until it aborts,
+    // even when its own last listener is removed.
+    if (isTimeoutOrNonEmptyCompositeSignal &&
+        type === 'abort' &&
+        size === 0 &&
+        !this[kDependantSignals]?.size) {
       gcPersistentSignals.delete(this);
     }
   }
diff --git a/test/parallel/test-abort-controller-any-timeout.js b/test/parallel/test-abort-controller-any-timeout.js
index 675be3af703..cb7eb0a96a1 100644
--- a/test/parallel/test-abort-controller-any-timeout.js
+++ b/test/parallel/test-abort-controller-any-timeout.js
@@ -39,4 +39,39 @@ describe('AbortSignal.any() with timeout signals', () => {
       clearTimeout(timeout);
     }
   });
+
+  it('should abort after a listener is removed from the timeout signal', async () => {
+    const signal = (() => {
+      const timeoutSignal = AbortSignal.timeout(common.platformTimeout(100));
+      const anySignal = AbortSignal.any([timeoutSignal]);
+      const listener = () => {};
+      timeoutSignal.addEventListener('abort', listener);
+      timeoutSignal.removeEventListener('abort', listener);
+      return anySignal;
+    })();
+    let timeout;
+
+    const abortPromise = Promise.race([
+      once(signal, 'abort').then(() => {
+        throw signal.reason;
+      }),
+      new Promise((resolve) => {
+        timeout = setTimeout(resolve, common.platformTimeout(10000));
+      }),
+    ]);
+
+    setImmediate(common.mustCall(() => globalThis.gc()));
+
+    try {
+      await assert.rejects(
+        () => abortPromise,
+        {
+          name: 'TimeoutError',
+          message: 'The operation was aborted due to timeout'
+        }
+      );
+    } finally {
+      clearTimeout(timeout);
+    }
+  });
 });