Don't emit 'exit' twice from child process
authorRyan Dahl <ry@tinyclouds.org>
Sun, 9 May 2010 06:28:26 +0000 (23:28 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Sun, 9 May 2010 06:28:26 +0000 (23:28 -0700)
lib/child_process.js

index b2ad2b6..ebca206 100644 (file)
@@ -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;