Skip to content

Commit

Permalink
process: make stdout and stderr emit 'close' on destroy
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mcollina authored and BethGriggs committed Jul 9, 2019
1 parent 7f3c29c commit 80337a8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/internal/process/stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,26 @@ const {
exports.setupProcessStdio = setupProcessStdio;
exports.getMainThreadStdio = getMainThreadStdio;

function dummyDestroy(err, cb) { cb(err); }
function dummyDestroy(err, cb) {
// SyncWriteStream does not use the stream
// destroy mechanism for some legacy reason.
// TODO(mcollina): remove when
// https://github.com/nodejs/node/pull/26690 lands.
if (typeof cb === 'function') {
cb(err);
}

// We need to emit 'close' anyway so that the closing
// of the stream is observable. We just make sure we
// are not going to do it twice.
// The 'close' event is needed so that finished and
// pipeline work correctly.
if (!this._writableState.emitClose) {
process.nextTick(() => {
this.emit('close');
});
}
}

function getMainThreadStdio() {
var stdin;
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-stdout-pipeline-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const common = require('../common');
const { Transform, Readable, pipeline } = require('stream');
const assert = require('assert');

const reader = new Readable({
read(size) { this.push('foo'); }
});

let count = 0;

const err = new Error('this-error-gets-hidden');

const transform = new Transform({
transform(chunk, enc, cb) {
if (count++ >= 5)
this.emit('error', err);
else
cb(null, count.toString() + '\n');
}
});

pipeline(
reader,
transform,
process.stdout,
common.mustCall((e) => {
assert.strictEqual(e, err);
})
);

0 comments on commit 80337a8

Please sign in to comment.