child_process: emit 'disconnect' asynchronously
authorBen Noordhuis <info@bnoordhuis.nl>
Thu, 20 Jun 2013 21:13:28 +0000 (23:13 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Thu, 20 Jun 2013 21:24:55 +0000 (23:24 +0200)
Deferring I/O-triggered events to the next event loop tick is not just
a good idea, IT'S THE LAW!

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

index f731221..4b37350 100644 (file)
@@ -498,7 +498,7 @@ function setupChannel(target, channel) {
       return;
     }
 
-    finish();
+    process.nextTick(finish);
   };
 
   channel.readStart();
index 162e7dd..301034c 100644 (file)
@@ -27,6 +27,16 @@ var net = require('net');
 // child
 if (process.argv[2] === 'child') {
 
+  // Check that the 'disconnect' event is deferred to the next event loop tick.
+  var disconnect = process.disconnect;
+  process.disconnect = function() {
+    disconnect.apply(this, arguments);
+    // If the event is emitted synchronously, we're too late by now.
+    process.once('disconnect', common.mustCall(disconnectIsNotAsync));
+    // The funky function name makes it show up legible in mustCall errors.
+    function disconnectIsNotAsync() {}
+  };
+
   var server = net.createServer();
 
   server.on('connection', function(socket) {