Skip to content

Commit a9a2dd3

Browse files
ronagdanielleadams
authored andcommitted
http: don't cork noop .end()
Calling .end() a second time should be a noop and not leave the socket corked. Fixes: #36620 PR-URL: #36633 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Danielle Adams <[email protected]>
1 parent e8bb1f7 commit a9a2dd3

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/_http_outgoing.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -819,17 +819,18 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
819819
encoding = null;
820820
}
821821

822-
if (this.socket) {
823-
this.socket.cork();
824-
}
825-
826822
if (chunk) {
827823
if (this.finished) {
828824
onError(this,
829825
new ERR_STREAM_WRITE_AFTER_END(),
830826
typeof callback !== 'function' ? nop : callback);
831827
return this;
832828
}
829+
830+
if (this.socket) {
831+
this.socket.cork();
832+
}
833+
833834
write_(this, chunk, encoding, null, true);
834835
} else if (this.finished) {
835836
if (typeof callback === 'function') {
@@ -841,6 +842,10 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
841842
}
842843
return this;
843844
} else if (!this._header) {
845+
if (this.socket) {
846+
this.socket.cork();
847+
}
848+
844849
this._contentLength = 0;
845850
this._implicitHeader();
846851
}

test/parallel/test-http-outgoing-end-multiple.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ const onWriteAfterEndError = common.mustCall((err) => {
99

1010
const server = http.createServer(common.mustCall(function(req, res) {
1111
res.end('testing ended state', common.mustCall());
12+
assert.strictEqual(res.writableCorked, 0);
1213
res.end(common.mustCall((err) => {
1314
assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED');
1415
}));
16+
assert.strictEqual(res.writableCorked, 0);
1517
res.end('end', onWriteAfterEndError);
18+
assert.strictEqual(res.writableCorked, 0);
1619
res.on('error', onWriteAfterEndError);
1720
res.on('finish', common.mustCall(() => {
1821
res.end(common.mustCall((err) => {

0 commit comments

Comments
 (0)