diff options
author | Gireesh Punathil <gpunathi@in.ibm.com> | 2018-06-08 08:33:37 -0400 |
---|---|---|
committer | Gireesh Punathil <gpunathi@in.ibm.com> | 2019-02-06 17:50:46 +0530 |
commit | b1f82e4342f8a630b1ef83cd33781a725428f569 (patch) | |
tree | 86f05ec37b0ee204b606b35dc25f479963226f3d /test | |
parent | 3732d7786bcb5722b9a4345e1345ff5ad81a7888 (diff) | |
download | android-node-v8-b1f82e4342f8a630b1ef83cd33781a725428f569.tar.gz android-node-v8-b1f82e4342f8a630b1ef83cd33781a725428f569.tar.bz2 android-node-v8-b1f82e4342f8a630b1ef83cd33781a725428f569.zip |
child_process: close pipe ends that are re-piped
when t0 and t1 are spawned with t0's outputstream [1, 2] is piped into
t1's input, a new pipe is created which uses a copy of the t0's fd.
This leaves the original copy in Node parent, unattended. Net result is
that when t0 produces data, it gets bifurcated into both the copies
Detect the passed handle to be of 'wrap' type and close after the
native spawn invocation by which time piping would have been over.
Fixes: https://github.com/nodejs/node/issues/9413
Fixes: https://github.com/nodejs/node/issues/18016
PR-URL: https://github.com/nodejs/node/pull/21209
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-child-process-pipe-dataflow.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/test/parallel/test-child-process-pipe-dataflow.js b/test/parallel/test-child-process-pipe-dataflow.js new file mode 100644 index 0000000000..501fb29032 --- /dev/null +++ b/test/parallel/test-child-process-pipe-dataflow.js @@ -0,0 +1,51 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); +const fs = require('fs'); +const spawn = require('child_process').spawn; +const tmpdir = require('../common/tmpdir'); + +let cat, grep, wc; + +const KB = 1024; +const MB = KB * KB; + + +// Make sure process chaining allows desired data flow: +// check cat <file> | grep 'x' | wc -c === 1MB +// This helps to make sure no data is lost between pipes. + +{ + tmpdir.refresh(); + const file = path.resolve(tmpdir.path, 'data.txt'); + const buf = Buffer.alloc(MB).fill('x'); + + // Most OS commands that deal with data, attach special + // meanings to new line - for example, line buffering. + // So cut the buffer into lines at some points, forcing + // data flow to be split in the stream. + for (let i = 0; i < KB; i++) + buf[i * KB] = 10; + fs.writeFileSync(file, buf.toString()); + + cat = spawn('cat', [file]); + grep = spawn('grep', ['x'], { stdio: [cat.stdout, 'pipe', 'pipe'] }); + wc = spawn('wc', ['-c'], { stdio: [grep.stdout, 'pipe', 'pipe'] }); + + wc.stdout.on('data', common.mustCall(function(data) { + assert.strictEqual(data.toString().trim(), MB.toString()); + })); + + cat.on('exit', common.mustCall(function(code) { + assert.strictEqual(code, 0); + })); + + grep.on('exit', common.mustCall(function(code) { + assert.strictEqual(code, 0); + })); + + wc.on('exit', common.mustCall(function(code) { + assert.strictEqual(code, 0); + })); +} |