Skip to content

fix(cache): keep body readable after cache.add()/addAll() - #5861

Closed
DevGnott wants to merge 1 commit into
nodejs:mainfrom
DevGnott:fix/cache-match-after-add
Closed

DevGnott wants to merge 1 commit into
nodejs:mainfrom
DevGnott:fix/cache-match-after-add

Conversation

@DevGnott

Copy link
Copy Markdown

This relates to...

Fixes #5859

Rationale

cache.add() / cache.addAll() fully read the response body while storing
it (added in #5844 to avoid the hang from #5615). Reading disturbs the stored
response's stream, so a later cache.match() / cache.matchAll() tees a
disturbed stream in cloneBody() and throws
TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is locked.

Cache.put() doesn't hit this because it stores the read bytes as
body.source. This applies the same idea to the add/addAll path.

Changes

In processResponseConsumeBody, rebuild a fresh, regenerable body from the
bytes that were just read, so the stored response can be cloned later.

Features

N/A

Bug Fixes

  • cache.match() / cache.matchAll() no longer throw ERR_INVALID_STATE
    after cache.add() / cache.addAll().

Breaking Changes and Deprecations

N/A

Status

  • I have read and agreed to the Developer's Certificate of Origin
  • Tested
  • Benchmarked (optional)
  • Documented
  • Review ready
  • In review
  • Merge ready

cache.add()/addAll() fully read the response body (to avoid hanging,
nodejs#5615/nodejs#5844), which disturbs the stored response's stream. A later
cache.match()/matchAll() then tees that disturbed stream in cloneBody()
and throws `ERR_INVALID_STATE: ReadableStream is locked`.

Rebuild a fresh, regenerable body from the bytes read in
processResponseConsumeBody, mirroring how Cache.put stores body.source.

Fixes nodejs#5859

Signed-off-by: DevGnott <strelov.andre98@gmail.com>
@KhafraDev KhafraDev closed this Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cache.match not working after cache.add

2 participants