Skip to content

Commit

Permalink
http: don't cork noop .end()
Browse files Browse the repository at this point in the history
Calling .end() a second time should be a noop and not
leave the socket corked.

Fixes: #36620

PR-URL: #36633
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Danielle Adams <adamzdanielle@gmail.com>
  • Loading branch information
ronag authored and danielleadams committed Jan 12, 2021
1 parent e8bb1f7 commit a9a2dd3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,17 +819,18 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
encoding = null;
}

if (this.socket) {
this.socket.cork();
}

if (chunk) {
if (this.finished) {
onError(this,
new ERR_STREAM_WRITE_AFTER_END(),
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') {
Expand All @@ -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();
}
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-http-outgoing-end-multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit a9a2dd3

Please sign in to comment.