child_process: do not disconnect on exit emit
authorAndreas Madsen <amwebdk@gmail.com>
Tue, 31 Jan 2012 16:14:42 +0000 (17:14 +0100)
committerisaacs <i@izs.me>
Wed, 1 Feb 2012 01:22:21 +0000 (17:22 -0800)
When using isolate the .fork would break because it had
no .disconnect method. This remove the exit handler there
would call .disconnect since it was not required.
It also change .disconnect to throw if the channel is closed,
this was not possible before because .disconnect would be called
twice.

lib/child_process.js
test/simple/test-child-process-disconnect.js

index 4af4d4f4139e7aca7dc64a35e1de393512d039ce..a1eac7744dd57450435328358d32ddb6d67c241c 100644 (file)
@@ -158,7 +158,9 @@ function setupChannel(target, channel) {
 
   target.connected = true;
   target.disconnect = function() {
-      if (!this.connected) return;
+      if (!this.connected) {
+        this.emit('error', new Error('IPC channel is already disconnected'));
+      }
 
       // do not allow messages to be written
       this.connected = false;
@@ -231,9 +233,6 @@ exports.fork = function(modulePath /*, args, options*/) {
 
   if (!options.thread) setupChannel(child, options.stdinStream);
 
-  // Disconnect when the child process exits.
-  child.once('exit', child.disconnect.bind(child));
-
   return child;
 };
 
index adb008d25fa22b279e486ecd96d860cfbcb066eb..e490c0848d29d3c445397f632480bc67e22c084d 100644 (file)
@@ -85,6 +85,7 @@ if (process.argv[2] === 'child') {
         // ready to be disconnected
         if (data === 'ready') {
           child.disconnect();
+          assert.throws(child.disconnect.bind(child), Error);
           return;
         }