Skip to content

Commit

Permalink
fix(#3736): leaked error event on response body (#3740)
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 authored Oct 16, 2024
1 parent 68107da commit 8e025d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class RequestHandler extends AsyncResource {
this.removeAbortListener = util.addAbortListener(signal, () => {
this.reason = signal.reason ?? new RequestAbortedError()
if (this.res) {
util.destroy(this.res, this.reason)
util.destroy(this.res.on('error', noop), this.reason)
} else if (this.abort) {
this.abort(this.reason)
}
Expand Down
36 changes: 36 additions & 0 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -1337,3 +1337,39 @@ test('request multibyte text with setEncoding', async (t) => {

await t.completed
})

test('#3736 - Aborted Response (without consuming body)', async (t) => {
const plan = tspl(t, { plan: 1 })

const controller = new AbortController()
const server = createServer((req, res) => {
setTimeout(() => {
res.writeHead(200, 'ok', {
'content-type': 'text/plain'
})
res.write('hello from server')
res.end()
}, 100)
})

server.listen(0)

await EE.once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)

after(server.close.bind(server))
after(client.destroy.bind(client))

const { signal } = controller
const promise = client.request({
path: '/',
method: 'GET',
signal
})

controller.abort()

await plan.rejects(promise, { message: 'This operation was aborted' })

await plan.completed
})

0 comments on commit 8e025d1

Please # to comment.