Skip to content

Commit 387d92f

Browse files
rickyescodebytere
authored andcommitted
http: onFinish will not be triggered again when finished
PR-URL: #35845Fixes: #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

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎lib/_http_outgoing.js‎

Lines changed: 6 additions & 0 deletions
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+
newERR_STREAM_WRITE_AFTER_END(),
820+
typeofcallback!=='function' ? nop : callback);
821+
returnthis;
822+
}
817823
write_(this,chunk,encoding,null,true);
818824
}elseif(this.finished){
819825
if(typeofcallback==='function'){

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ const common = require('../common');
33
constassert=require('assert');
44
consthttp=require('http');
55

6+
constonWriteAfterEndError=common.mustCall((err)=>{
7+
assert.strictEqual(err.code,'ERR_STREAM_WRITE_AFTER_END');
8+
},2);
9+
610
constserver=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)