Skip to content

Commit

Permalink
src: setup cluster workers before preloading
Browse files Browse the repository at this point in the history
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: #1269
PR-URL: #1314
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Fedor Indutny <[email protected]>
  • Loading branch information
ofrobots authored and bnoordhuis committed Apr 3, 2015
1 parent 65d4d25 commit b6e22c4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@
} 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');
Expand All @@ -87,16 +98,6 @@
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 &&
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/cluster-preload-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
3 changes: 3 additions & 0 deletions test/fixtures/cluster-preload.js
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

0 comments on commit b6e22c4

Please sign in to comment.