diff --git a/lib/child_process.js b/lib/child_process.js index b2ad2b619fa..ebca206df42 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -99,14 +99,22 @@ function ChildProcess () { var stdout = this.stdout = new Stream(); var stderr = this.stderr = new Stream(); - function onClose () { - if (gotCHLD && !stdout.readable && !stderr.readable) { + var stderrClosed = false; + var stdoutClosed = false; + + stderr.addListener('close', function () { + stderrClosed = true; + if (gotCHLD && stdoutClosed) { self.emit('exit', exitCode, termSignal); } - } + }); - stderr.addListener('close', onClose); - stdout.addListener('close', onClose); + stdout.addListener('close', function () { + stdoutClosed = true; + if (gotCHLD && stderrClosed) { + self.emit('exit', exitCode, termSignal); + } + }); internal.onexit = function (code, signal) { gotCHLD = true;