src: setup cluster workers before preloading
authorAli Ijaz Sheikh <ofrobots@google.com>
Mon, 30 Mar 2015 17:54:59 +0000 (10:54 -0700)
committerBen Noordhuis <info@bnoordhuis.nl>
Fri, 3 Apr 2015 22:53:45 +0000 (00:53 +0200)
We need to process cluster workers before any preload modules is
executed. Otherwise, the child processes are not correctly disovered
as clustered workers inside the preloaded modules.

Fixes: https://github.com/iojs/io.js/issues/1269
PR-URL: https://github.com/iojs/io.js/pull/1314
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
src/node.js
test/fixtures/cluster-preload-test.js [new file with mode: 0644]
test/fixtures/cluster-preload.js [new file with mode: 0644]
test/parallel/test-preload.js

index 6346529..1339fb3 100644 (file)
     } else {
       // There is user code to be run
 
+      // If this is a worker in cluster mode, start up the communication
+      // channel. This needs to be done before any user code gets executed
+      // (including preload modules).
+      if (process.argv[1] && process.env.NODE_UNIQUE_ID) {
+        var cluster = NativeModule.require('cluster');
+        cluster._setupWorker();
+
+        // Make sure it's not accidentally inherited by child processes.
+        delete process.env.NODE_UNIQUE_ID;
+      }
+
       // Load any preload modules
       if (process._preload_modules) {
         var Module = NativeModule.require('module');
         var path = NativeModule.require('path');
         process.argv[1] = path.resolve(process.argv[1]);
 
-        // If this is a worker in cluster mode, start up the communication
-        // channel.
-        if (process.env.NODE_UNIQUE_ID) {
-          var cluster = NativeModule.require('cluster');
-          cluster._setupWorker();
-
-          // Make sure it's not accidentally inherited by child processes.
-          delete process.env.NODE_UNIQUE_ID;
-        }
-
         var Module = NativeModule.require('module');
 
         if (global.v8debug &&
diff --git a/test/fixtures/cluster-preload-test.js b/test/fixtures/cluster-preload-test.js
new file mode 100644 (file)
index 0000000..43d3887
--- /dev/null
@@ -0,0 +1,7 @@
+var cluster = require('cluster');
+if (cluster.isMaster) {
+  cluster.fork(); // one child
+  cluster.on('exit', function(worker, code, signal) {
+    console.log('worker terminated with code ' + code);
+  });
+}
diff --git a/test/fixtures/cluster-preload.js b/test/fixtures/cluster-preload.js
new file mode 100644 (file)
index 0000000..651fc48
--- /dev/null
@@ -0,0 +1,3 @@
+var cluster = require('cluster');
+cluster.isMaster || process.exit(42 + cluster.worker.id); // +42 to distinguish
+// from exit(1) for other random reasons
index aef778b..643edfb 100644 (file)
@@ -72,3 +72,11 @@ child_process.exec(nodeBinary + ' '
     if (err) throw err;
     assert.equal(stdout, 'A\nB\nhello\n');
   });
+
+child_process.exec(nodeBinary + ' '
+  + '--require ' + fixture('cluster-preload.js') + ' '
+  + fixture('cluster-preload-test.js'),
+  function(err, stdout, stderr) {
+    if (err) throw err;
+    assert.ok(/worker terminated with code 43/.test(stdout));
+  });