Skip to content

Commit

Permalink
http: don't emit error after destroy
Browse files Browse the repository at this point in the history
PR-URL: #55457
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ethan Arrowood <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jake Yuesong Li <[email protected]>
  • Loading branch information
ronag authored and aduh95 committed Dec 10, 2024
1 parent aa6b0e8 commit 025f057
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-http-outgoing-destroyed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}

0 comments on commit 025f057

Please sign in to comment.