Skip to content

Miniflare fetch responses lose unread bodies when the original Undici Response is garbage collected #16217

Open

Description

@shuuheyhey

What versions & operating system are you using?

  • Miniflare: 5.20261006.1-alpha (unmodified npm package for the failing run)
  • Undici: 7.29.1 (Miniflare's pinned dependency)
  • Node.js: reproduced separately on 24.19.0 and 26.11.0
  • macOS: 26.6.2, arm64
  • Bun 1.4.3 used only for dependency installation; the reproduction executes with Node.

Please provide a link to a minimal reproduction

https://gist.github.com/shuuheyhey/35fe90e04fa91c39f3560c27ef84f66f

The gist contains a standalone package, frozen lockfile, local HTTP reproduction, and a small patch to the published package for a before/after comparison. It needs no Cloudflare account, credentials, workerd, remote service, or application code.

Describe the Bug

A Miniflare fetch() Response can lose its unread body even while the caller retains the Response. This occurs when GC collects the original Undici Response that Miniflare wrapped.

Steps:

  1. Save the gist's files into an empty directory and run bun install --frozen-lockfile.
  2. Run node --expose-gc repro.mjs.
  3. The reproduction fetches 20 responses from a local HTTP server, keeps all returned Responses reachable, yields across 10 explicit GC cycles, then reads their bodies. The GC probe has no fixed sleep or network retry.

Expected: all 20 bodies remain readable while their Responses are reachable.

Actual on both Node versions: all 20 bodies were cancelled with Response object has been garbage collected; subsequent reads fail with TypeError: Body is unusable: Body has already been read.

Cause / proposed fix:

src/http/fetch.ts returns new Response(response.body, response). The wrapper shares the body but does not keep the original Undici Response alive. Undici registers the original Response with a finalizer and cancels its unread/unlocked body when that Response is collected.

The same wrapping remains in the upstream source checked on 2026-10-12. A possible fix in src/http/response.ts is to retain the original owner only while a wrapper sharing its body remains reachable:

const responseBodyOwners = new WeakMap<Response, BaseResponse>();

// In Response's constructor, after super(body, init):
if (body != null && init instanceof BaseResponse && body === init.body) {
  responseBodyOwners.set(this, init);
}

Using weak keys allows abandoned wrappers and their owners to be collected. This avoids buffering bodies or changing the mutable-header behavior of Miniflare Responses.

To try the equivalent compiled-package patch included in the gist:

git apply --directory=node_modules/miniflare compiled-fix.patch
node --expose-gc repro.mjs

On both Node 24.19.0 and 26.11.0 this changes the result to 20 pass, 0 fail, 0 GC cancellations, exit 0. The patch only adds owner retention; the reproduction and GC timing are unchanged.

I am submitting the reproduction and proposed fix as an issue in accordance with the repository's registered-collaborator contribution policy. Please let me know if you would like a source patch with regression coverage in a PR.

Please provide any relevant error logs

Unpatched, Node 24.19.0 (26.11.0 gives the same result):

{
  "node": "v24.19.0",
  "platform": "darwin/arm64",
  "passed": 0,
  "failed": 20,
  "cancellations": ["Response object has been garbage collected"],
  "cancellationCount": 20,
  "errors": ["TypeError: Body is unusable: Body has already been read"]
}

Activity

  1. workers-devprod commented on Oct 12, 2026

    @workers-devprod
    Contributor

    Automated triage

    Issue assessment

    • Actionability: ✅ Actionable.
    • Type: Bug.
    • Reproduction: Automatic attempt requested.
    • Labels:
      • bug (high)
      • package:miniflare (high)
    Actionability and missing information

    Miniflare fetch() Responses lose unread bodies when the wrapped Undici Response is GC'd while still reachable; report provides versions, a self-contained local repro with steps, expected/actual behavior, error logs, and a proposed WeakMap fix retaining the original response owner.

    Reproduction reason: Self-contained local reproduction with explicit steps, pinned versions, and error logs; no suspicious or unsafe instructions, so the GC-driven body cancellation can be verified directly.

    Reproduction

    • Result: ⚠️ Blocked.
    • Summary: Automated reproduction failed before recording a result.
    Evidence and attempted steps

    Blocker: Automated reproduction failed before recording a result.

    Agent outcome: Failed.

    Failure type: submission_timeout

    Failure: Submission exceeded the configured timeout.

    Details: The operation ran longer than the configured durability timeout.

    Submission ID: sub_ik_22e8f37a18715d0772874c3354b3355e


    This summary was generated by an LLM and may contain errors. Please verify its findings.

    Triage bot commands and options

    Repository collaborators with write or admin access can trigger actions by commenting on this issue:

    • @workers-devprod reproduce: Attempt to reproduce the issue using its description and recent comments.
  2. moved this from Untriaged to Backlog in workers-sdkon Nov 5, 2026
  3. added a commit that references this issue on Nov 19, 2026
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

    bugpackage:miniflare

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions