Skip to content

Commit afeb626

Browse files
fix aborting Streams (#3754)
* fix aborting Streams * stop memory leak
1 parent d78b7ca commit afeb626

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

lib/web/fetch/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,8 +1943,10 @@ async function httpNetworkFetch (
19431943
// 19. Run these steps in parallel:
19441944

19451945
// 1. Run these steps, but abort when fetchParams is canceled:
1946-
fetchParams.controller.onAborted = onAborted
1947-
fetchParams.controller.on('terminated', onAborted)
1946+
if (!fetchParams.controller.resume) {
1947+
fetchParams.controller.on('terminated', onAborted)
1948+
}
1949+
19481950
fetchParams.controller.resume = async () => {
19491951
// 1. While true
19501952
while (true) {
@@ -2205,10 +2207,6 @@ async function httpNetworkFetch (
22052207
fetchParams.controller.off('terminated', this.abort)
22062208
}
22072209

2208-
if (fetchParams.controller.onAborted) {
2209-
fetchParams.controller.off('terminated', fetchParams.controller.onAborted)
2210-
}
2211-
22122210
fetchParams.controller.ended = true
22132211

22142212
this.body.push(null)

test/fetch/issue-1711.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,30 @@ test('Redirecting a bunch does not cause a MaxListenersExceededWarning', async (
3131

3232
assert.deepStrictEqual(response.url, `${url}/${redirects - 1}`)
3333
})
34+
35+
test(
36+
'aborting a Stream throws',
37+
() => {
38+
return new Promise((resolve, reject) => {
39+
const httpServer = createServer((request, response) => {
40+
response.end(new Uint8Array(20000))
41+
}).listen(async () => {
42+
const serverAddress = httpServer.address()
43+
44+
if (typeof serverAddress === 'object') {
45+
const abortController = new AbortController()
46+
const readStream = (await fetch(`http://localhost:${serverAddress?.port}`, { signal: abortController.signal })).arrayBuffer()
47+
abortController.abort()
48+
setTimeout(reject)
49+
50+
try {
51+
await readStream
52+
} catch {
53+
httpServer.close()
54+
resolve()
55+
}
56+
}
57+
})
58+
})
59+
}
60+
)

0 commit comments

Comments
 (0)