mirror of https://github.com/lukechilds/node.git
Browse Source
Currently, cluster workers can be removed from the workers list in three different places: - In the exit event handler for the worker process. - In the disconnect event handler of the worker process. - In the disconnect event handler of the cluster master. However, handles for a given worker are cleaned up only in one of these places: in the cluster master's disconnect event handler. Because these events happen asynchronously, it is possible that the workers list is empty before we even clean up one handle. This makes the assert that makes sure that no handle is left when the workers list is empty fail. This commit removes the worker from the cluster.workers list only when the worker is dead _and_ disconnected, at which point we're sure that its associated handles are cleaned up. Fixes #8191 and #8192. Reviewed-By: Fedor Indutny <fedor@indutny.com>v0.11.14-release
committed by
Fedor Indutny
5 changed files with 134 additions and 19 deletions
@ -0,0 +1,37 @@ |
|||
var cluster = require('cluster'); |
|||
var assert = require('assert'); |
|||
var util = require('util'); |
|||
|
|||
if (cluster.isMaster) { |
|||
var worker = cluster.fork(); |
|||
|
|||
assert.ok(worker.isConnected(), |
|||
"isConnected() should return true as soon as the worker has " + |
|||
"been created."); |
|||
|
|||
worker.on('disconnect', function() { |
|||
assert.ok(!worker.isConnected(), |
|||
"After a disconnect event has been emitted, " + |
|||
"isConncted should return false"); |
|||
}); |
|||
|
|||
worker.on('message', function(msg) { |
|||
if (msg === 'readyToDisconnect') { |
|||
worker.disconnect(); |
|||
} |
|||
}) |
|||
|
|||
} else { |
|||
assert.ok(cluster.worker.isConnected(), |
|||
"isConnected() should return true from within a worker at all " + |
|||
"times."); |
|||
|
|||
cluster.worker.process.on('disconnect', function() { |
|||
assert.ok(!cluster.worker.isConnected(), |
|||
"isConnected() should return false from within a worker " + |
|||
"after its underlying process has been disconnected from " + |
|||
"the master"); |
|||
}) |
|||
|
|||
process.send('readyToDisconnect'); |
|||
} |
@ -0,0 +1,27 @@ |
|||
var cluster = require('cluster'); |
|||
var assert = require('assert'); |
|||
var net = require('net'); |
|||
|
|||
if (cluster.isMaster) { |
|||
var worker = cluster.fork(); |
|||
assert.ok(!worker.isDead(), |
|||
"isDead() should return false right after the worker has been " + |
|||
"created."); |
|||
|
|||
worker.on('exit', function() { |
|||
assert.ok(!worker.isConnected(), |
|||
"After an event has been emitted, " + |
|||
"isDead should return true"); |
|||
}) |
|||
|
|||
worker.on('message', function(msg) { |
|||
if (msg === 'readyToDie') { |
|||
worker.kill(); |
|||
} |
|||
}); |
|||
|
|||
} else if (cluster.isWorker) { |
|||
assert.ok(!cluster.worker.isDead(), |
|||
"isDead() should return false when called from within a worker"); |
|||
process.send('readyToDie'); |
|||
} |
Loading…
Reference in new issue