Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue 2009 #2013

Merged
merged 1 commit into from
Mar 17, 2023
Merged
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
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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let isFailure
let isFailure = false

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')
}
)
)
}
})