Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/web/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function cloneResponse (response) {
if (response.body != null) {
newResponse.body = cloneBody(response.body)

streamRegistry.register(newResponse, new WeakRef(response.body.stream))
streamRegistry.register(newResponse, new WeakRef(newResponse.body.stream))
}

// 4. Return newResponse.
Expand Down
27 changes: 27 additions & 0 deletions test/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,30 @@ test('clone body garbage collection', async () => {
const cloneBody = ref.deref()
assert.equal(cloneBody, undefined, 'clone body was not garbage collected')
})

test('clone body garbage collection should not affect original res', async () => {
let ref

const doFetch = async () => {
const res = new Response('hello world')

// Clone res and consume its body
const clone = res.clone()
await clone.text()
ref = new WeakRef(clone)

return res
}

const res = await doFetch()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res has never had its body read, but ends up being canceled in the finalization registry b/c we register the wrong stream in lib/web/fetch/response.js


// wait for garbage collection
while (true) {
await setImmediate()
global.gc()
if (!ref.deref()) break
}
Comment on lines +317 to +322
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the clone is garbage collected, the original res body stream should not be canceled


await setImmediate()
assert.doesNotThrow(() => res.clone(), 'Body has already been consumed')
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails on main

✖ clone body garbage collection should not affect original res (6.698336ms)
  AssertionError [ERR_ASSERTION]: Got unwanted exception: Body has already been consumed

Loading