From 1b669b2c12a53ede6a0cfa0462b47e867ea456c3 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 PR-URL: https://github.com/nodejs/node/pull/36633 Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Danielle Adams --- lib/_http_outgoing.js | 13 +++++++++---- test/parallel/test-http-outgoing-end-multiple.js | 3 +++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 50ef063241b173..872fc04edbadc8 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -819,10 +819,6 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { encoding = null; } - if (this.socket) { - this.socket.cork(); - } - if (chunk) { if (this.finished) { onError(this, @@ -830,6 +826,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') { @@ -841,6 +842,10 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { } return this; } else if (!this._header) { + if (this.socket) { + this.socket.cork(); + } + this._contentLength = 0; this._implicitHeader(); } 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) => {