content icon indicating copy to clipboard operation
content copied to clipboard

No information about AbortSignal.any (AbortSignal.timeout, Promise.race, Promise.any) lack of unsubscription capabilities and its consequences.

Open user3232 opened this issue 4 months ago • 4 comments

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static

What specific section or headline is this issue about?

Description

What information was incorrect, unhelpful, or incomplete?

No information about unsubscribing from signals iterable, or lack of it, and potential consequences.

What did you expect to see?

Warning that using AbortSignal.any in a loop can potentially cause memory leak. Alternative ways to overcome this.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
  • Folder: en-us/web/api/abortsignal/any_static
  • MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static
  • GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/api/abortsignal/any_static/index.md
  • Last commit: https://github.com/mdn/content/commit/a4fcf79b60471db6f148fa4ba36f2cdeafbbeb70
  • Document last modified: 2025-10-30T21:49:49.000Z

user3232 avatar Jul 12 '26 22:07 user3232

Hi @user3232, I understand the basic idea of this issue, but could you be more specific about what kind of scenario (in terms of a small code example) you are talking about? What kind of content would you like to see added other than simply "Cancel other operations when using any() if they are not used elsewhere"?

Josh-Cena avatar Aug 11 '26 17:08 Josh-Cena

Maybe it would be useful to add note:

Beware Resulting signal won't be garbage collected until:

  • one of signals is aborted,
  • or all signals (including resulting) are garbage collected.

Why?

Because I believe that code composing 'slow' and 'fast' signals using AbortSignal.any would leak memory:

const global = new AbortController()
while(1000) {
  console.log('hello')
  const local = new AbortController()
  const signal = AbortSignal.any([global.signal, local.signal])
  console.log({any: signal.aborted, global: global.aborted, local: local.aborted)
} 
// 1000 signal's still stays in memory
function runOrAbort(local: AbortSignal) {
  const signal = AbortSignal.any([global.signal, local.signal])
  signal.throwIfAborted()
  console.log('hello')
}
// every run will leave signal in memory, until global is gc or signalled

And I think that application using and composing 2 signals its something that may happen in live, and there is no shame in it.

Same applies to Promise.race, and probably all structures composed from something that not offer something like unsubscribe or dispose etc. Promise.race example:

const global = Promise.withResolvers<void>()
while(1000) {
  const local = Promise.withResolvers<string>()
  local.resolve('hello')
  const result = await Promise.race([global.promise, local.promise])
  console.log(result)
}
// 1000 `global.then(...)` promises pending in memory.
async function runOrAbort(local: AbortSignal) {
  const result = await Promise.any([global.promise, local.promise, 'hello'])
  console.log(result)
}
// every run will leave `global.promise.then(...)`, `local.promise.then(...)` until 
// promises resolves or `global.promise` unreferenced / gc'ed

What do you think?

user3232 avatar Aug 11 '26 19:08 user3232

I see, yeah that's reasonable.

Josh-Cena avatar Aug 11 '26 19:08 Josh-Cena

Hi! I’d like to work on this issue.

Based on the clarified example above, I’m planning to keep the change focused on AbortSignal.any() and add a short note explaining the potential reference retention when repeatedly composing a long-lived signal with short-lived signals, along with a small example showing the pattern to avoid.

I’ll avoid expanding the scope into Promise.race() or Promise.any() unless maintainers prefer that to be documented separately.

Please let me know if that scope looks right.

surajchi avatar Aug 20 '26 07:08 surajchi