Skip to content

Commit

Permalink
stream: complete pipeline with stdio
Browse files Browse the repository at this point in the history
stdio (stderr & stdout) should for compatibility
reasons not be closed/end():ed. However, this
causes pipeline with a stdio destination to
never finish. This commit fixes this issue at
a performance cost.

Refs: #7606

Fixes: #32363

PR-URL: #32373
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
ronag authored and targos committed Apr 11, 2020
1 parent 9df274a commit 1c47bba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ function pipeline(...streams) {
} else if (isStream(stream)) {
if (isReadable(ret)) {
ret.pipe(stream);

// Compat. Before node v10.12.0 stdio used to throw an error so
// pipe() did/does not end() stdio destinations.
// Now they allow it but "secretly" don't close the underlying fd.
if (stream === process.stdout || stream === process.stderr) {
ret.on('end', () => stream.end());
}
} else {
ret = makeAsyncIterable(ret);

Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-stream-pipeline-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const os = require('os');

if (process.argv[2] === 'child') {
const { pipeline } = require('stream');
pipeline(
process.stdin,
process.stdout,
common.mustCall((err) => {
assert.ifError(err);
})
);
} else {
const cp = require('child_process');
cp.exec([
'echo',
'hello',
'|',
`"${process.execPath}"`,
`"${__filename}"`,
'child'
].join(' '), common.mustCall((err, stdout) => {
assert.ifError(err);
assert.strictEqual(stdout.split(os.EOL).shift().trim(), 'hello');
}));
}

0 comments on commit 1c47bba

Please sign in to comment.