Skip to content

Commit 8b65aa7

Browse files
mcollinatargos
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 <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 220f67c commit 8b65aa7

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
@@ -2,7 +2,26 @@
22

33
exports.getMainThreadStdio = getMainThreadStdio;
44

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

726
function getMainThreadStdio() {
827
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)