diff --git a/lib/child_process.js b/lib/child_process.js index 4af4d4f4139..a1eac7744dd 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -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; }; diff --git a/test/simple/test-child-process-disconnect.js b/test/simple/test-child-process-disconnect.js index adb008d25fa..e490c0848d2 100644 --- a/test/simple/test-child-process-disconnect.js +++ b/test/simple/test-child-process-disconnect.js @@ -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; }