next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Patched fetch ignores the AbortSignal for a Request input once a GC runs (`fetch(new Request(url), { signal })`)

Open gregorjan opened this issue 3 weeks ago • 1 comments

Link to the code that reproduces this issue

https://github.com/gregorjan/next-abort-repro

To Reproduce

  1. npm install && npm run build
  2. Terminal 1: npm run backend (a backend that answers every request after 6 s)
  3. Terminal 2: FORCE_GC=1 NODE_OPTIONS=--expose-gc npm start
  4. Open /request (fetch(new Request(url), { signal })) and /url (fetch(url, { signal })). Both abort after 1.5 s; FORCE_GC=1 runs gc() every 50 ms while they wait, standing in for a busy server.

Current vs. Expected behavior

Expected: both pages reject with AbortError after about 1.5 s, as plain fetch does in Node.

Current: /request ignores the abort and resolves when the backend answers, although signal.aborted is true (5 of 5 runs). /url rejects at 1.5 s (5 of 5).

/request: answered 200 after 6002 ms (signal.aborted: true, forced gc: every 50 ms)
/url:     rejected AbortError after 1501 ms (signal.aborted: true, forced gc: every 50 ms)

Without the forced GC both reject at 1.5 s; under real load the GC makes /request miss its abort now and then. Same result on [email protected] (Node 24.22.1, undici 7.30.1): /request answered after 6 s in 5 of 5 runs, /url rejected at 1.5 s in 5 of 5.

Likely cause: for a Request input plus init, patch-fetch builds an intermediate new Request(input, init) and fetches with another Request built from it. undici makes a Request's signal follow its parent's only through a WeakRef (nodejs/undici#1926), and nothing keeps the intermediate Request alive, so once it is collected the abort no longer reaches the running fetch. Keeping the intermediate Request alive until the fetch settles, or passing init.signal straight to the final fetch, would fix it.

This was reported before as #100375, which the bot closed for a missing reproduction link.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Debian 6.12.111-1 (2026-09-28)
  Available memory (MB): 9951
  Available CPU cores: 8
Binaries:
  Node: 24.16.0 (undici 7.25.0)
  npm: 11.13.0
Relevant Packages:
  next: 16.5.0-canary.48 // Latest available version is detected (16.5.0-canary.48).
  react: 19.3.1
  react-dom: 19.3.1
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Server Components, fetch (patch-fetch)

Which stage(s) are affected? (Select all that apply)

next start (local)

gregorjan avatar Nov 12 '26 22:11 gregorjan

Hi! Could I look into this? I’d add a regression test for fetch(new Request(url, { signal })) that checks whether aborting still cancels the request after garbage collection. I’d also verify that the direct fetch(url, { signal }) path keeps working.

seragAhmed avatar Nov 13 '26 08:11 seragAhmed