From 4cc48de47eac336c976198c023fde99b8ad2910f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 26 Dec 2020 11:11:21 +0100 Subject: [PATCH] http: don't cork noop .end() Calling .end() a second time should be a noop and not leave the socket corked. Fixes: https://github.com/nodejs/node/issues/36620 --- lib/_http_outgoing.js | 9 +++++---- test/parallel/test-http-outgoing-end-multiple.js | 3 +++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 8fb64ab82efceb..d487ab5b9b9971 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -810,10 +810,6 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { encoding = null; } - if (this.socket) { - this.socket.cork(); - } - if (chunk) { if (this.finished) { onError(this, @@ -821,6 +817,11 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { typeof callback !== 'function' ? nop : callback); return this; } + + if (this.socket) { + this.socket.cork(); + } + write_(this, chunk, encoding, null, true); } else if (this.finished) { if (typeof callback === 'function') { diff --git a/test/parallel/test-http-outgoing-end-multiple.js b/test/parallel/test-http-outgoing-end-multiple.js index ed42c913375e84..696443f9390cd0 100644 --- a/test/parallel/test-http-outgoing-end-multiple.js +++ b/test/parallel/test-http-outgoing-end-multiple.js @@ -9,10 +9,13 @@ const onWriteAfterEndError = common.mustCall((err) => { const server = http.createServer(common.mustCall(function(req, res) { res.end('testing ended state', common.mustCall()); + assert.strictEqual(res.writableCorked, 0); res.end(common.mustCall((err) => { assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED'); })); + assert.strictEqual(res.writableCorked, 0); res.end('end', onWriteAfterEndError); + assert.strictEqual(res.writableCorked, 0); res.on('error', onWriteAfterEndError); res.on('finish', common.mustCall(() => { res.end(common.mustCall((err) => {