From 5633c62219e199baac833e8862d60333d85dc3d3 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 28 Oct 2024 13:57:58 +0100 Subject: [PATCH] http: don't emit error after destroy PR-URL: https://github.com/nodejs/node/pull/55457 Reviewed-By: Yagiz Nizipli Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca Reviewed-By: Ethan Arrowood Reviewed-By: James M Snell Reviewed-By: Jake Yuesong Li --- lib/_http_outgoing.js | 6 ++++- test/parallel/test-http-outgoing-destroyed.js | 24 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 7a402a0f900..23b850d1522 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -908,6 +908,10 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) { }; function onError(msg, err, callback) { + if (msg.destroyed) { + return; + } + const triggerAsyncId = msg.socket ? msg.socket[async_id_symbol] : undefined; defaultTriggerAsyncIdScope(triggerAsyncId, process.nextTick, @@ -919,7 +923,7 @@ function onError(msg, err, callback) { function emitErrorNt(msg, err, callback) { callback(err); - if (typeof msg.emit === 'function' && !msg._closed) { + if (typeof msg.emit === 'function' && !msg.destroyed) { msg.emit('error', err); } } diff --git a/test/parallel/test-http-outgoing-destroyed.js b/test/parallel/test-http-outgoing-destroyed.js index 2dd3d76fde7..4f8fd47eaa8 100644 --- a/test/parallel/test-http-outgoing-destroyed.js +++ b/test/parallel/test-http-outgoing-destroyed.js @@ -51,5 +51,27 @@ const assert = require('assert'); .on('error', common.mustCall()) .write('asd'); }); - +} + +{ + const server = http.createServer(common.mustCall((req, res) => { + assert.strictEqual(res.closed, false); + res.end(); + res.destroy(); + // Make sure not to emit 'error' after .destroy(). + res.end('asd'); + assert.strictEqual(res.errored, undefined); + })).listen(0, () => { + http + .request({ + port: server.address().port, + method: 'GET' + }) + .on('response', common.mustCall((res) => { + res.resume().on('end', common.mustCall(() => { + server.close(); + })); + })) + .end(); + }); }