Skip to content

Commit 6c468df

Browse files
mscdexMyles Borins
authored and
Myles Borins
committed
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 PR-URL: #5037 Reviewed-By: James M Snell <[email protected]>
1 parent 018e4e0 commit 6c468df

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Diff for: lib/child_process.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1094,8 +1094,11 @@ util.inherits(ChildProcess, EventEmitter);
10941094
function flushStdio(subprocess) {
10951095
if (subprocess.stdio == null) return;
10961096
subprocess.stdio.forEach(function(stream, fd, stdio) {
1097-
if (!stream || !stream.readable || stream._consuming ||
1098-
stream._readableState.flowing)
1097+
if (!stream ||
1098+
!stream.readable ||
1099+
stream._consuming ||
1100+
stream._readableState.flowing ||
1101+
stream._readableState.readableListening)
10991102
return;
11001103
stream.resume();
11011104
});

Diff for: test/simple/test-child-process-flush-stdio.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
var cp = require('child_process');
3+
var common = require('../common');
4+
var assert = require('assert');
5+
6+
var buffer = [];
7+
var p = cp.spawn('echo', ['123']);
8+
p.on('close', common.mustCall(function(code, signal) {
9+
assert.strictEqual(code, 0);
10+
assert.strictEqual(signal, null);
11+
assert.strictEqual(Buffer.concat(buffer).toString().trim(), '123');
12+
}));
13+
p.stdout.on('readable', function() {
14+
var buf;
15+
while (buf = this.read())
16+
buffer.push(buf);
17+
});

0 commit comments

Comments
 (0)