node/test/parallel/test-child-process-stdio-reuse-readable-stdio.js
Anna Henningsen cc7b3fbaab child_process: only stop readable side of stream passed to proc
Closing the underlying resource completely has the unwanted side effect
that the stream can no longer be used at all, including passing it
to other child processes.

What we want to avoid is accidentally reading from the stream;
accordingly, it should be sufficient to stop its readable side
manually, and otherwise leave the underlying resource intact.

Fixes: https://github.com/nodejs/node/issues/27097
Refs: https://github.com/nodejs/node/pull/21209

PR-URL: https://github.com/nodejs/node/pull/27373
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
2019-04-29 20:01:27 +05:30

28 lines
973 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const common = require('../common');
const assert = require('assert');
const { spawn } = require('child_process');
// Check that, once a child process has ended, its safe to read from a pipe
// that the child had used as input.
// We simulate that using cat | (head -n1; ...)
const p1 = spawn('cat', { stdio: ['pipe', 'pipe', 'inherit'] });
const p2 = spawn('head', ['-n1'], { stdio: [p1.stdout, 'pipe', 'inherit'] });
// First, write the line that gets passed through p2, making 'head' exit.
p1.stdin.write('hello\n');
p2.stdout.setEncoding('utf8');
p2.stdout.on('data', common.mustCall((chunk) => {
assert.strictEqual(chunk, 'hello\n');
}));
p2.on('exit', common.mustCall(() => {
// We can now use cats output, because 'head' is no longer reading from it.
p1.stdin.end('world\n');
p1.stdout.setEncoding('utf8');
p1.stdout.on('data', common.mustCall((chunk) => {
assert.strictEqual(chunk, 'world\n');
}));
p1.stdout.resume();
}));