diff --git a/lib/api/api-request.js b/lib/api/api-request.js index ced5590d210..396b430f50e 100644 --- a/lib/api/api-request.js +++ b/lib/api/api-request.js @@ -154,6 +154,11 @@ class RequestHandler extends AsyncResource { } onComplete (trailers) { + if (this.removeAbortListener) { + this.removeAbortListener() + this.removeAbortListener = null + } + util.parseHeaders(trailers, this.trailers) this.res.push(null) } diff --git a/test/request-signal.js b/test/request-signal.js index fd4d2f885a5..94ba8ec796b 100644 --- a/test/request-signal.js +++ b/test/request-signal.js @@ -36,9 +36,11 @@ test('post abort signal', async (t) => { server.listen(0, async () => { const ac = new AbortController() - const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal }) + const uresPromise = request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal }) ac.abort() + try { + const ures = await uresPromise /* eslint-disable-next-line no-unused-vars */ for await (const chunk of ures.body) { // Do nothing... @@ -61,9 +63,11 @@ test('post abort signal w/ reason', async (t) => { server.listen(0, async () => { const ac = new AbortController() const _err = new Error() - const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal }) + const uresPromise = request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal }) ac.abort(_err) + try { + const ures = await uresPromise /* eslint-disable-next-line no-unused-vars */ for await (const chunk of ures.body) { // Do nothing... @@ -74,3 +78,20 @@ test('post abort signal w/ reason', async (t) => { }) await t.completed }) + +test('post abort signal after request completed', async (t) => { + t = tspl(t, { plan: 1 }) + + const server = createServer((req, res) => { + res.end('asd') + }) + after(() => server.close()) + + server.listen(0, async () => { + const ac = new AbortController() + const ures = await request(`http://0.0.0.0:${server.address().port}`, { signal: ac.signal }) + ac.abort() + t.equal(await ures.body.text(), 'asd') + }) + await t.completed +})