forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster: centralize removal from workers list.
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 nodejs#8191 and nodejs#8192. Reviewed-By: Fedor Indutny <[email protected]>
- Loading branch information
Showing
5 changed files
with
134 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} |