Repository navigation
Description
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.0and26.11.0 - macOS:
26.6.2, arm64 - Bun
1.4.3used only for dependency installation; the reproduction executes with Node.
Please provide a link to a minimal reproduction
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:
- Save the gist's files into an empty directory and run
bun install --frozen-lockfile. - Run
node --expose-gc repro.mjs. - 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.mjsOn 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"]
}Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
workers-devprod commentedon Oct 12, 2026
Automated triage
Issue assessment
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
Evidence and attempted steps
Blocker: Automated reproduction failed before recording a result.
Agent outcome: Failed.
Failure type:
submission_timeoutFailure: Submission exceeded the configured timeout.
Details: The operation ran longer than the configured durability timeout.
Submission ID:
sub_ik_22e8f37a18715d0772874c3354b3355eThis 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.Fix flaky docs 404 routing test (#231)