Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(#3736): leaked error event on response body #3740

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