Skip to content

Cloned requests loose abort signal on GC #4068

Closed

Description

@andreas-karlsson

Bug Description

We noticed that AbortSignal.timeout() stopped working in our library with longer timeouts (in our case around 9s). Debugging showed that cloned (or constructor copied) requests won't respect an abort signal if GC has happened in between.

Reproducible By

Run with node --expose-gc

import { createServer } from 'node:http';

createServer(() => {
  // server that just hangs
}).listen(3000);

const ac = new AbortController();

let req = new Request('http://localhost:3000', {
  signal: ac.signal,
});

// cloned or copied requests (req = new Request(req)) breaks abort after gc
req = req.clone();

setTimeout(() => {
  // comment out the gc to make abort work
  global.gc();
  ac.abort();
  console.log('aborted, expecting immediate exception from fetch...');
});

await fetch(req);

Commenting out the cloning and/or the gc will make the code work as expected, i.e. the fetch call rejecting after abort.

Expected Behavior

A cloned request should respect it's abort signal even after a long time (or garbage collection).

Environment

macOS 15.3.1 (arm64), Node v23.4.0, v22.14.0, v20.18.0

Additional context

Debugging found that the difference before/after gc is that the "dependentControllerMap" entry for the signal is missing here

Activity

  1. added
    bug
    on Feb 27, 2025
  2. KhafraDev commented on Feb 27, 2025

    @KhafraDev
    Member

    Debugging found that the difference before/after gc is that the "dependentControllerMap" entry for the signal is missing here

    This looks familiar to another issue with fetch/signals caused by the AbortSignal implementation in node core but I couldn't find the issue. Should this be posted in node core instead? Is there anything we can do to fix this in undici?

  3. andreas-karlsson commented on Feb 28, 2025

    @andreas-karlsson
    Author

    @KhafraDev I haven't managed to reproduce without Request involved. Also holding on strongly to the original request doesn't help. To me it seems odd that this could be a pure AbortSignal problem? Ah, or maybe you're saying that there can be a bug in the internal api of AbortSignal (like dependentControllerMap), but undici is using the api correctly..

  4. shiv19 commented on Sep 27, 2026

    @shiv19

    still happening on Node 22.22, hit via @openrouter/sdk

  5. rikbrown commented on Oct 5, 2026

    @rikbrown

    Still reproduces on Node v24.15.0 (bundled undici 7.24.4) and v24.12.0 (undici 7.16.0), with AbortSignal.timeout() as the parent signal. That is the pattern of SDKs that pass a cloned request to fetch with a timeout: after a collection, the timeout never reaches fetch, and the request waits forever.

    Repro (node --expose-gc repro.mjs gc, or gc hold, or no argument):

    const mode = process.argv[2]
    const hold = process.argv[3] === 'hold'
    const held = []
    let cloneAborted = 0
    let originalAborted = 0
    for (let i = 0; i < 50; i++) {
      const timeout = AbortSignal.timeout(20)
      const original = new Request('https://example.test/', { signal: timeout })
      const clone = original.clone()
      if (hold) held.push(original, clone, timeout)
      await new Promise((resolve) => setImmediate(resolve))
      if (mode === 'gc') globalThis.gc()
      const result = await new Promise((resolve) => {
        const guard = setTimeout(() => resolve('no abort'), 200)
        clone.signal.addEventListener('abort', () => { clearTimeout(guard); resolve('abort') }, { once: true })
      })
      if (result === 'abort') cloneAborted++
      if (original.signal.aborted) originalAborted++
    }
    console.log(`clone aborted ${cloneAborted}/50, original aborted ${originalAborted}/50`)

    Results on v24.15.0, 50 runs each:

    Mode Clone signal aborted Original signal aborted
    Forced GC between clone() and the timeout 0/50 50/50
    Forced GC, with both requests and the timeout signal held in variables 0/50 50/50
    No forced GC 49/50 50/50

    Holding the requests does not help, so the lost link is the clone's internal controller, not the request objects. In a test suite under load, a natural collection hit this in about 2 to 6 of 50 runs of a test that stubs fetch and waits for request.signal to abort.

    The same sequence in workerd 1.20261001.1 aborts the clone 50/50.

    Workaround for a test stub: wait on the parent signal (for example, spy on AbortSignal.timeout and wait on the signal it returned) instead of request.signal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions