Skip to content

Commit 81a4edc

Browse files
AndreasMadsenbnoordhuis
authored andcommitted
cluster: remove NODE_UNIQUE_ID from env on startup
In case a worker would spawn a new subprocess with process.env, NODE_UNIQUE_ID would have been a part of the env. Making the new subprocess believe it is a worker, this would result in some confusion if the subprocess where to listen to a port, since the server handle request would then be relayed to the worker. This patch removes the NODE_UNIQUE_ID flag from process.env on startup so any subprocess spawned by a worker is a normal process with no cluster stuff.
1 parent 968b49b commit 81a4edc

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

lib/cluster.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var debug;
3333
if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {
3434
debug = function(x) {
3535
var prefix = process.pid + ',' +
36-
(process.env.NODE_WORKER_ID ? 'Worker' : 'Master');
36+
(process.env.NODE_UNIQUE_ID ? 'Worker' : 'Master');
3737
console.error(prefix, x);
3838
};
3939
} else {

lib/net.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var stream = require('stream');
2424
var timers = require('timers');
2525
var util = require('util');
2626
var assert = require('assert');
27+
var cluster;
2728

2829
function noop() {}
2930

@@ -907,8 +908,9 @@ Server.prototype._listen2 = function(address, port, addressType, backlog) {
907908

908909

909910
function listen(self, address, port, addressType, backlog) {
910-
if (process.env.NODE_UNIQUE_ID) {
911-
var cluster = require('cluster');
911+
if (!cluster) cluster = require('cluster');
912+
913+
if (cluster.isWorker) {
912914
cluster._getServer(self, address, port, addressType, function(handle) {
913915
self._handle = handle;
914916
self._listen2(address, port, addressType, backlog);

src/node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
if (process.env.NODE_UNIQUE_ID) {
7979
var cluster = NativeModule.require('cluster');
8080
cluster._setupWorker();
81+
82+
// Make sure it's not accidentally inherited by child processes.
83+
delete process.env.NODE_UNIQUE_ID;
8184
}
8285

8386
var Module = NativeModule.require('module');

test/simple/test-cluster-basic.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ var common = require('../common');
2424
var assert = require('assert');
2525
var cluster = require('cluster');
2626

27+
assert.equal('NODE_UNIQUE_ID' in process.env, false,
28+
'NODE_UNIQUE_ID should be removed on startup');
29+
2730
function forEach(obj, fn) {
2831
Object.keys(obj).forEach(function(name, index) {
2932
fn(obj[name], name, index);
@@ -40,9 +43,6 @@ if (cluster.isWorker) {
4043

4144
else if (cluster.isMaster) {
4245

43-
assert.equal('NODE_UNIQUE_ID' in process.env, false,
44-
'cluster.isMaster should not be true when NODE_UNIQUE_ID is set');
45-
4646
var checks = {
4747
cluster: {
4848
events: {

0 commit comments

Comments
 (0)