diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index a6f9ed9fa476fd..f482d85a2c84d3 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -217,7 +217,7 @@ util.inherits(ChildProcess, EventEmitter); function flushStdio(subprocess) { if (subprocess.stdio == null) return; subprocess.stdio.forEach(function(stream, fd, stdio) { - if (!stream || !stream.readable) + if (!stream || !stream.readable || stream._readableState.readableListening) return; stream.resume(); }); diff --git a/test/parallel/test-child-process-flush-stdio.js b/test/parallel/test-child-process-flush-stdio.js index 5fd7eb3bc99922..dacc1226f30d03 100644 --- a/test/parallel/test-child-process-flush-stdio.js +++ b/test/parallel/test-child-process-flush-stdio.js @@ -8,10 +8,22 @@ const p = cp.spawn('echo'); p.on('close', common.mustCall(function(code, signal) { assert.strictEqual(code, 0); assert.strictEqual(signal, null); + spawnWithReadable(); })); p.stdout.read(); -setTimeout(function() { - p.kill(); -}, 100); +function spawnWithReadable() { + const buffer = []; + const p = cp.spawn('echo', ['123']); + p.on('close', common.mustCall(function(code, signal) { + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + assert.strictEqual(Buffer.concat(buffer).toString().trim(), '123'); + })); + p.stdout.on('readable', function() { + let buf; + while (buf = this.read()) + buffer.push(buf); + }); +}