diff --git a/lib/api/api-request.js b/lib/api/api-request.js index ced5590d210..be17d628774 100644 --- a/lib/api/api-request.js +++ b/lib/api/api-request.js @@ -73,7 +73,7 @@ class RequestHandler extends AsyncResource { this.removeAbortListener = util.addAbortListener(this.signal, () => { this.reason = this.signal.reason ?? new RequestAbortedError() if (this.res) { - util.destroy(this.res, this.reason) + util.destroy(this.res.on('error', util.nop), this.reason) } else if (this.abort) { this.abort(this.reason) } diff --git a/test/client-request.js b/test/client-request.js index c67cecdb7f3..c5e90e840ee 100644 --- a/test/client-request.js +++ b/test/client-request.js @@ -1252,3 +1252,39 @@ test('request post body DataView', 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 +}) diff --git a/test/http2.js b/test/http2.js index d6840a1bd15..3f7ab3deb24 100644 --- a/test/http2.js +++ b/test/http2.js @@ -334,23 +334,15 @@ test( after(() => server.close()) after(() => client.close()) - t = tspl(t, { plan: 2 }) + t = tspl(t, { plan: 1 }) - try { - await client.request({ - path: '/', - method: 'GET', - headers: { - 'x-my-header': 'foo' - } - }) - } catch (error) { - t.strictEqual( - error.message, - 'Client network socket disconnected before secure TLS connection was established' - ) - t.strictEqual(error.code, 'ECONNRESET') - } + await t.rejects(client.request({ + path: '/', + method: 'GET', + headers: { + 'x-my-header': 'foo' + } + })) } )