Skip to content

Commit

Permalink
fix aborting Streams (#3754)
Browse files Browse the repository at this point in the history
* fix aborting Streams

* stop memory leak
  • Loading branch information
epistemancering authored Oct 29, 2024
1 parent d78b7ca commit afeb626
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1943,8 +1943,10 @@ async function httpNetworkFetch (
// 19. Run these steps in parallel:

// 1. Run these steps, but abort when fetchParams is canceled:
fetchParams.controller.onAborted = onAborted
fetchParams.controller.on('terminated', onAborted)
if (!fetchParams.controller.resume) {
fetchParams.controller.on('terminated', onAborted)
}

fetchParams.controller.resume = async () => {
// 1. While true
while (true) {
Expand Down Expand Up @@ -2205,10 +2207,6 @@ async function httpNetworkFetch (
fetchParams.controller.off('terminated', this.abort)
}

if (fetchParams.controller.onAborted) {
fetchParams.controller.off('terminated', fetchParams.controller.onAborted)
}

fetchParams.controller.ended = true

this.body.push(null)
Expand Down
27 changes: 27 additions & 0 deletions test/fetch/issue-1711.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,30 @@ test('Redirecting a bunch does not cause a MaxListenersExceededWarning', async (

assert.deepStrictEqual(response.url, `${url}/${redirects - 1}`)
})

test(
'aborting a Stream throws',
() => {
return new Promise((resolve, reject) => {
const httpServer = createServer((request, response) => {
response.end(new Uint8Array(20000))
}).listen(async () => {
const serverAddress = httpServer.address()

if (typeof serverAddress === 'object') {
const abortController = new AbortController()
const readStream = (await fetch(`http://localhost:${serverAddress?.port}`, { signal: abortController.signal })).arrayBuffer()
abortController.abort()
setTimeout(reject)

try {
await readStream
} catch {
httpServer.close()
resolve()
}
}
})
})
}
)

0 comments on commit afeb626

Please sign in to comment.