fix(cache): regenerate stream from source when cache.match is called after GC#4713
Merged
fix(cache): regenerate stream from source when cache.match is called after GC#4713
Conversation
…after GC The cached response's stream could be cancelled by the FinalizationRegistry when a temporary Response object wrapping the inner response was garbage collected. This caused cache.match() to throw "Body has already been consumed" after garbage collection ran between calls. The fix checks if the cached stream is unusable (locked or disturbed) but the body source is available (which cache.put() always stores), and if so, recreates the stream from the source before creating the Response object. Fixes: #4710 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4713 +/- ##
==========================================
+ Coverage 92.85% 93.17% +0.31%
==========================================
Files 109 109
Lines 33809 33872 +63
==========================================
+ Hits 31395 31561 +166
+ Misses 2414 2311 -103 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
metcoder95
approved these changes
Jan 4, 2026
Member
diff --git a/lib/web/cache/cache.js b/lib/web/cache/cache.js
index 70a3787a..10decbed 100644
--- a/lib/web/cache/cache.js
+++ b/lib/web/cache/cache.js
@@ -794,9 +794,9 @@ class Cache {
// 5.5.2
for (const response of responses) {
// 5.5.2.1
- const responseObject = fromInnerResponse(response, 'immutable')
+ const responseObject = fromInnerResponse(cloneResponse(response), 'immutable')
- responseList.push(responseObject.clone())
+ responseList.push(responseObject)
if (responseList.length >= maxResponses) {
break
diff --git a/lib/web/fetch/response.js b/lib/web/fetch/response.js
index b3f942c3..6b954afa 100644
--- a/lib/web/fetch/response.js
+++ b/lib/web/fetch/response.js
@@ -555,7 +555,8 @@ function fromInnerResponse (innerResponse, guard) {
setHeadersList(headers, innerResponse.headersList)
setHeadersGuard(headers, guard)
- if (innerResponse.body?.stream) {
+ // Note: If innerResponse's urlList contains a URL, it is a fetch response.
+ if (innerResponse.urlList.length !== 0 && innerResponse.body?.stream) {
// If the target (response) is reclaimed, the cleanup callback may be called at some point with
// the held value provided for it (innerResponse.body.stream). The held value can be any value:
// a primitive or an object, even undefined. If the held value is an object, the registry keeps
@mcollina |
Co-Authored-By: tsctx <91457664+tsctx@users.noreply.github.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
Member
Author
|
@tsctx ptal |
tsctx
reviewed
Jan 13, 2026
Co-Authored-By: tsctx <91457664+tsctx@users.noreply.github.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
Member
Author
|
@tsctx ptal |
tsctx
approved these changes
Jan 13, 2026
|
Noting I've been making use of the iterations of changes locally using a patch against the package, has been working great for a larger amount of caches, time, & other used resources. |
Merged
slagiewka
pushed a commit
to slagiewka/undici
that referenced
this pull request
Feb 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Root Cause
The bug occurs due to the interaction between the
FinalizationRegistry(used to track Response objects) and thecloneBody()function which mutates the original body's stream in-place:cache.match()callsfromInnerResponse()which creates a temporary Response and registers the cached streamresponse.clone()is called, which tees the stream and mutates the cached stream to be one of the tee outputsclone()re-registers this mutated stream with the temporary Responsecache.match()calls failFix
The fix checks if the cached stream is unusable but
body.sourceis available (whichcache.put()always stores after reading the body bytes), and recreates the stream from source before creating the Response object.Fixes: #4710
Test plan
🤖 Generated with Claude Code