Skip to content

Commit 80337a8

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

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/internal/process/stdio.js

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

11-
function dummyDestroy(err, cb) { cb(err); }
11+
function dummyDestroy(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 (typeof cb === '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
function getMainThreadStdio() {
1433
var stdin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { Transform, Readable, pipeline } = require('stream');
5+
const assert = require('assert');
6+
7+
const reader = new Readable({
8+
read(size) { this.push('foo'); }
9+
});
10+
11+
let count = 0;
12+
13+
const err = new Error('this-error-gets-hidden');
14+
15+
const transform = new Transform({
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)