Skip to content

Commit 6fd7477

Browse files
authoredMar 17, 2023
fix: issue 2009 (#2013)
1 parent 4e1e0d0 commit 6fd7477

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
 

‎lib/fetch/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,7 @@ async function httpNetworkFetch (
18451845
// 4. Set bytes to the result of handling content codings given
18461846
// codings and bytes.
18471847
let bytes
1848+
let isFailure
18481849
try {
18491850
const { done, value } = await fetchParams.controller.next()
18501851

@@ -1859,6 +1860,10 @@ async function httpNetworkFetch (
18591860
bytes = undefined
18601861
} else {
18611862
bytes = err
1863+
1864+
// err may be propagated from the result of calling readablestream.cancel,
1865+
// which might not be an error. https://github.com/nodejs/undici/issues/2009
1866+
isFailure = true
18621867
}
18631868
}
18641869

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

18801885
// 6. If bytes is failure, then terminate fetchParams’s controller.
1881-
if (isErrorLike(bytes)) {
1886+
if (isFailure) {
18821887
fetchParams.controller.terminate(bytes)
18831888
return
18841889
}

‎test/fetch/issue-2009.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { fetch } = require('../..')
5+
const { createServer } = require('http')
6+
const { once } = require('events')
7+
8+
test('issue 2009', async (t) => {
9+
const server = createServer((req, res) => {
10+
res.setHeader('a', 'b')
11+
res.flushHeaders()
12+
13+
res.socket.end()
14+
}).listen(0)
15+
16+
t.teardown(server.close.bind(server))
17+
await once(server, 'listening')
18+
19+
for (let i = 0; i < 10; i++) {
20+
await t.resolves(
21+
fetch(`http://localhost:${server.address().port}`).then(
22+
async (resp) => {
23+
await resp.body.cancel('Some message')
24+
}
25+
)
26+
)
27+
}
28+
})

0 commit comments

Comments
 (0)
Please sign in to comment.