Skip to content

Commit

Permalink
child_process: fix data loss with readable event
Browse files Browse the repository at this point in the history
This commit prevents child process stdio streams from being
automatically flushed on child process exit/close if a 'readable'
event handler has been attached at the time of exit.

Without this, child process stdio data can be lost if the process
exits quickly and a `read()` (e.g. from a 'readable' handler)
hasn't had the chance to get called yet.

Fixes: #5034
PR-URL: #5036
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
mscdex authored and rvagg committed Feb 15, 2016
1 parent 25513a4 commit 9e6ad2d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
18 changes: 15 additions & 3 deletions test/parallel/test-child-process-flush-stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

0 comments on commit 9e6ad2d

Please sign in to comment.