Skip to content
This repository
Browse code

child process: fix processes with IPC channel don't emit 'close'

With this patch the IPC socket is no longer available in the
ChildProcess.stdio array. This shouldn't be very problematic, since
this socket was effectively non-functional; it would never emit any
events.
  • Loading branch information...
commit b0d2795fe9501218dd8c953252fffe2d05d59c2a 1 parent 355c148
Bert Belder authored August 30, 2012
10  lib/child_process.js
@@ -300,6 +300,9 @@ function setupChannel(target, channel) {
300 300
     } else {
301 301
       this.buffering = false;
302 302
       target.disconnect();
  303
+      channel.onread = nop;
  304
+      channel.close();
  305
+      maybeClose(target);
303 306
     }
304 307
   };
305 308
 
@@ -745,7 +748,7 @@ ChildProcess.prototype.spawn = function(options) {
745 748
       ipc = createPipe(true);
746 749
       ipcFd = i;
747 750
 
748  
-      acc.push({ type: 'pipe', handle: ipc });
  751
+      acc.push({ type: 'pipe', handle: ipc, ipc: true });
749 752
     } else if (typeof stdio === 'number' || typeof stdio.fd === 'number') {
750 753
       acc.push({ type: 'fd', fd: stdio.fd || stdio });
751 754
     } else if (getHandleWrapType(stdio) || getHandleWrapType(stdio.handle) ||
@@ -796,6 +799,11 @@ ChildProcess.prototype.spawn = function(options) {
796 799
   stdio.forEach(function(stdio, i) {
797 800
     if (stdio.type === 'ignore') return;
798 801
 
  802
+    if (stdio.ipc) {
  803
+      self._closesNeeded++;
  804
+      return;
  805
+    }
  806
+
799 807
     if (stdio.handle) {
800 808
       // when i === 0 - we're dealing with stdin
801 809
       // (which is the only one writable pipe)
3  test/fixtures/child-process-message-and-exit.js
... ...
@@ -0,0 +1,3 @@
  1
+
  2
+process.send('hello');
  3
+process.exit(0);
57  test/simple/test-child-process-fork-close.js
... ...
@@ -0,0 +1,57 @@
  1
+// Copyright Joyent, Inc. and other Node contributors.
  2
+//
  3
+// Permission is hereby granted, free of charge, to any person obtaining a
  4
+// copy of this software and associated documentation files (the
  5
+// "Software"), to deal in the Software without restriction, including
  6
+// without limitation the rights to use, copy, modify, merge, publish,
  7
+// distribute, sublicense, and/or sell copies of the Software, and to permit
  8
+// persons to whom the Software is furnished to do so, subject to the
  9
+// following conditions:
  10
+//
  11
+// The above copyright notice and this permission notice shall be included
  12
+// in all copies or substantial portions of the Software.
  13
+//
  14
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21
+
  22
+var assert = require('assert'),
  23
+    common = require('../common'),
  24
+    fork = require('child_process').fork,
  25
+    fork = require('child_process').fork;
  26
+
  27
+var cp = fork(common.fixturesDir + '/child-process-message-and-exit.js');
  28
+
  29
+var gotMessage = false,
  30
+    gotExit = false,
  31
+    gotClose = false;
  32
+
  33
+cp.on('message', function(message) {
  34
+  assert(!gotMessage);
  35
+  assert(!gotClose);
  36
+  assert.strictEqual(message, 'hello');
  37
+  gotMessage = true;
  38
+});
  39
+
  40
+cp.on('exit', function() {
  41
+  assert(!gotExit);
  42
+  assert(!gotClose);
  43
+  gotExit = true;
  44
+});
  45
+
  46
+cp.on('close', function() {
  47
+  assert(gotMessage);
  48
+  assert(gotExit);
  49
+  assert(!gotClose);
  50
+  gotClose = true;
  51
+});
  52
+
  53
+process.on('exit', function() {
  54
+  assert(gotMessage);
  55
+  assert(gotExit);
  56
+  assert(gotClose);
  57
+});

0 notes on commit b0d2795

Please sign in to comment.
Something went wrong with that request. Please try again.