Re: [whatwg/fetch] Proposal: fetch with multiple AbortSignals (#905)

@jakearchibald My comment about node.js was based on [MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) - they must be outdated then.

I think the AbortController stays in memory until it gets aborted. In case of early return this may happen much after the return (or never happen), hence the memory leak.

I spent some time on this and created the example below. Run `test1()` in memory profiler with and without the commented line below. Check when the 80MB are deallocated in each case. WIth this line, it get deallocated immediatelly. Without this line, it get deallocated only after 10 seconds.
```javascript
function anySignal(signals) {
  const controller = new AbortController();
  controller.data = Array(20000000).fill(1);

  for (const signal of signals) {
    if (signal.aborted) {
      //controller.abort(); // <-------- this
      return signal;
    }

    signal.addEventListener("abort", () => controller.abort(signal.reason), {
      signal: controller.signal,
    });
  }

  return controller.signal;
}

function test1() {
  const signal1 = AbortSignal.timeout(10000);
  const signal2 = AbortSignal.abort();
  const signal = anySignal([signal1, signal2]);
  signal1.addEventListener('abort', () => console.log('done!'));
}
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/905#issuecomment-1426609108
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/fetch/issues/905/1426609108@github.com>

Received on Saturday, 11 February 2023 04:22:03 UTC