Skip to content

Commit

Permalink
http: onFinish will not be triggered again when finished
Browse files Browse the repository at this point in the history
PR-URL: #35845
Fixes: #35833
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
rickyes authored and codebytere committed Nov 22, 2020
1 parent 8ae3ffe commit 387d92f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,12 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
}

if (chunk) {
if (this.finished) {
onError(this,
new ERR_STREAM_WRITE_AFTER_END(),
typeof callback !== 'function' ? nop : callback);
return this;
}
write_(this, chunk, encoding, null, true);
} else if (this.finished) {
if (typeof callback === 'function') {
Expand Down
10 changes: 9 additions & 1 deletion test/parallel/test-http-outgoing-end-multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ const common = require('../common');
const assert = require('assert');
const http = require('http');

const onWriteAfterEndError = common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
}, 2);

const server = http.createServer(common.mustCall(function(req, res) {
res.end('testing ended state', common.mustCall());
res.end(common.mustCall());
res.end(common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED');
}));
res.end('end', onWriteAfterEndError);
res.on('error', onWriteAfterEndError);
res.on('finish', common.mustCall(() => {
res.end(common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED');
Expand Down

0 comments on commit 387d92f

Please sign in to comment.