Skip to content
New issue

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

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

Already on GitHub? # to your account

fix aborting Streams #3754

Merged
merged 2 commits into from
Oct 29, 2024
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
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()
}
}
})
})
}
)
Loading