Skip to content

Commit

Permalink
cluster: send suicide message on disconnect
Browse files Browse the repository at this point in the history
This commit causes Worker.prototype.disconnect() to send a
suicide message to the cluster master. The function is also
restructured to eliminate redundant code.

Fixes: #3238
PR-URL: #3720
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig authored and jasnell committed Dec 23, 2015
1 parent ed32b9a commit 160702c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
22 changes: 10 additions & 12 deletions lib/cluster.js
Expand Up @@ -651,26 +651,24 @@ function workerInit() {

Worker.prototype.disconnect = function() {
this.suicide = true;
var waitingHandles = 0;
let waitingCount = 1;

function checkRemainingHandles() {
waitingHandles--;
if (waitingHandles === 0) {
function checkWaitingCount() {
waitingCount--;
if (waitingCount === 0) {
send({ act: 'suicide' });
process.disconnect();
}
}

for (var key in handles) {
var handle = handles[key];
for (const key in handles) {
const handle = handles[key];
delete handles[key];
waitingHandles++;
handle.owner.close(checkRemainingHandles);
}

if (waitingHandles === 0) {
process.disconnect();
waitingCount++;
handle.owner.close(checkWaitingCount);
}

checkWaitingCount();
};

Worker.prototype.destroy = function() {
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-regress-GH-3238.js
@@ -0,0 +1,21 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');

if (cluster.isMaster) {
const worker = cluster.fork();
let disconnected = false;

worker.on('disconnect', common.mustCall(function() {
assert.strictEqual(worker.suicide, true);
disconnected = true;
}));

worker.on('exit', common.mustCall(function() {
assert.strictEqual(worker.suicide, true);
assert.strictEqual(disconnected, true);
}));
} else {
cluster.worker.disconnect();
}

0 comments on commit 160702c

Please sign in to comment.