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) => {