Skip to content

Commit 387d92f

Browse files
rickyescodebytere
authored andcommitted
http: onFinish will not be triggered again when finished
PR-URL: #35845 Fixes: #35833 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 8ae3ffe commit 387d92f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/_http_outgoing.js

+6
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,12 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
814814
}
815815

816816
if (chunk) {
817+
if (this.finished) {
818+
onError(this,
819+
new ERR_STREAM_WRITE_AFTER_END(),
820+
typeof callback !== 'function' ? nop : callback);
821+
return this;
822+
}
817823
write_(this, chunk, encoding, null, true);
818824
} else if (this.finished) {
819825
if (typeof callback === 'function') {

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ const common = require('../common');
33
const assert = require('assert');
44
const http = require('http');
55

6+
const onWriteAfterEndError = common.mustCall((err) => {
7+
assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
8+
}, 2);
9+
610
const server = http.createServer(common.mustCall(function(req, res) {
711
res.end('testing ended state', common.mustCall());
8-
res.end(common.mustCall());
12+
res.end(common.mustCall((err) => {
13+
assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED');
14+
}));
15+
res.end('end', onWriteAfterEndError);
16+
res.on('error', onWriteAfterEndError);
917
res.on('finish', common.mustCall(() => {
1018
res.end(common.mustCall((err) => {
1119
assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED');

0 commit comments

Comments
 (0)