node icon indicating copy to clipboard operation
node copied to clipboard

Request signal isn't aborted after garbage collection

Open jvaill opened this issue 5 months ago • 9 comments

Bug Description

Passing a signal to Request() and aborting it does not cause the underlying request signal to be aborted after it's been garbage collected. This leads to issues where you want to listen on the request signal even after the request itself falls out of scope - e.g., the request is instantiated with an abort controller signal and downstream code is waiting for that signal to be aborted via request.signal.

Reproducible By

Run the following using node --expose-gc main.js:

const ac = new AbortController();

ac.signal.addEventListener("abort", () => {
  console.log("ac signal aborted");
});

const request = new Request("https://google.ca", { signal: ac.signal });

request.signal.addEventListener("abort", () => {
  console.log("request signal aborted");
});

setTimeout(() => {
  global.gc();
  ac.abort();
}, 0);

Expected Behavior

ac signal aborted and request signal aborted should be logged to the console.

Instead, only ac signal aborted is logged.

Environment

Latest node and undici. Tried it in some older versions as well.

Additional context

A few folks are running into this while trying to close event stream requests in the remix web framework. The underlying requests are never closed because the signal doesn't get aborted.

jvaill avatar Sep 25 '24 21:09 jvaill