src: setup cluster workers before preloading

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>
This commit is contained in:
Ali Ijaz Sheikh 2015-03-30 10:54:59 -07:00 committed by Ben Noordhuis
parent 65d4d25f52
commit b6e22c4bd5
4 changed files with 29 additions and 10 deletions

View File

@ -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');
@ -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 &&

7
test/fixtures/cluster-preload-test.js vendored Normal file
View File

@ -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
test/fixtures/cluster-preload.js vendored Normal file
View File

@ -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

View 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));
});