Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
child_process: fix data loss with readable event
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
  • Loading branch information
mscdex committed Feb 4, 2016
commit 3e7af8d792757ce17314824e2abc4418b520be2f
7 changes: 5 additions & 2 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -1094,8 +1094,11 @@ util.inherits(ChildProcess, EventEmitter);
function flushStdio(subprocess) {
if (subprocess.stdio == null) return;
subprocess.stdio.forEach(function(stream, fd, stdio) {
if (!stream || !stream.readable || stream._consuming ||
stream._readableState.flowing)
if (!stream ||
!stream.readable ||
stream._consuming ||
stream._readableState.flowing ||
stream._readableState.readableListening)
return;
stream.resume();
});
Expand Down
17 changes: 17 additions & 0 deletions test/simple/test-child-process-flush-stdio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
var cp = require('child_process');
var common = require('../common');
var assert = require('assert');

var buffer = [];
var 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() {
var buf;
while (buf = this.read())
buffer.push(buf);
});