Skip to content

Commit f34bb96

Browse files
mcollinaBethGriggs
authored andcommitted
process: make stdout and stderr emit 'close' on destroy
Fix: #26550 PR-URL: #26691 Fixes: https://github.com/falseFixes: #26550 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 274b97c commit f34bb96

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

‎lib/internal/process/stdio.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,26 @@ const{
88
exports.setupProcessStdio=setupProcessStdio;
99
exports.getMainThreadStdio=getMainThreadStdio;
1010

11-
functiondummyDestroy(err,cb){cb(err);}
11+
functiondummyDestroy(err,cb){
12+
// SyncWriteStream does not use the stream
13+
// destroy mechanism for some legacy reason.
14+
// TODO(mcollina): remove when
15+
// https://github.com/nodejs/node/pull/26690 lands.
16+
if(typeofcb==='function'){
17+
cb(err);
18+
}
19+
20+
// We need to emit 'close' anyway so that the closing
21+
// of the stream is observable. We just make sure we
22+
// are not going to do it twice.
23+
// The 'close' event is needed so that finished and
24+
// pipeline work correctly.
25+
if(!this._writableState.emitClose){
26+
process.nextTick(()=>{
27+
this.emit('close');
28+
});
29+
}
30+
}
1231

1332
functiongetMainThreadStdio(){
1433
varstdin;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
const{ Transform, Readable, pipeline }=require('stream');
5+
constassert=require('assert');
6+
7+
constreader=newReadable({
8+
read(size){this.push('foo');}
9+
});
10+
11+
letcount=0;
12+
13+
consterr=newError('this-error-gets-hidden');
14+
15+
consttransform=newTransform({
16+
transform(chunk,enc,cb){
17+
if(count++>=5)
18+
this.emit('error',err);
19+
else
20+
cb(null,count.toString()+'\n');
21+
}
22+
});
23+
24+
pipeline(
25+
reader,
26+
transform,
27+
process.stdout,
28+
common.mustCall((e)=>{
29+
assert.strictEqual(e,err);
30+
})
31+
);

0 commit comments

Comments
(0)