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
  • Loading branch information
ronag committed Dec 26, 2020
1 parent 7303afb commit 4cc48de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/_http_outgoing.js
Expand Up @@ -810,17 +810,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 Down
3 changes: 3 additions & 0 deletions test/parallel/test-http-outgoing-end-multiple.js
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 4cc48de

Please sign in to comment.