Skip to content

Commit

Permalink
fix: issue 2009 (#2013)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Mar 17, 2023
1 parent 4e1e0d0 commit 6fd7477
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,7 @@ async function httpNetworkFetch (
// 4. Set bytes to the result of handling content codings given
// codings and bytes.
let bytes
let isFailure
try {
const { done, value } = await fetchParams.controller.next()

Expand All @@ -1859,6 +1860,10 @@ async function httpNetworkFetch (
bytes = undefined
} else {
bytes = err

// err may be propagated from the result of calling readablestream.cancel,
// which might not be an error. https://github.com/nodejs/undici/issues/2009
isFailure = true
}
}

Expand All @@ -1878,7 +1883,7 @@ async function httpNetworkFetch (
timingInfo.decodedBodySize += bytes?.byteLength ?? 0

// 6. If bytes is failure, then terminate fetchParams’s controller.
if (isErrorLike(bytes)) {
if (isFailure) {
fetchParams.controller.terminate(bytes)
return
}
Expand Down
28 changes: 28 additions & 0 deletions test/fetch/issue-2009.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const { test } = require('tap')
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')

test('issue 2009', async (t) => {
const server = createServer((req, res) => {
res.setHeader('a', 'b')
res.flushHeaders()

res.socket.end()
}).listen(0)

t.teardown(server.close.bind(server))
await once(server, 'listening')

for (let i = 0; i < 10; i++) {
await t.resolves(
fetch(`http://localhost:${server.address().port}`).then(
async (resp) => {
await resp.body.cancel('Some message')
}
)
)
}
})

0 comments on commit 6fd7477

Please sign in to comment.