Skip to content

Commit bf41a97

Browse files
cjihrigrvagg
authored andcommitted
cluster: send suicide message on disconnect
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 <[email protected]>
1 parent 365760e commit bf41a97

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

lib/cluster.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -651,26 +651,24 @@ function workerInit() {
651651

652652
Worker.prototype.disconnect = function() {
653653
this.suicide = true;
654-
var waitingHandles = 0;
654+
let waitingCount = 1;
655655

656-
function checkRemainingHandles() {
657-
waitingHandles--;
658-
if (waitingHandles === 0) {
656+
function checkWaitingCount() {
657+
waitingCount--;
658+
if (waitingCount === 0) {
659+
send({ act: 'suicide' });
659660
process.disconnect();
660661
}
661662
}
662663

663-
for (var key in handles) {
664-
var handle = handles[key];
664+
for (const key in handles) {
665+
const handle = handles[key];
665666
delete handles[key];
666-
waitingHandles++;
667-
handle.owner.close(checkRemainingHandles);
668-
}
669-
670-
if (waitingHandles === 0) {
671-
process.disconnect();
667+
waitingCount++;
668+
handle.owner.close(checkWaitingCount);
672669
}
673670

671+
checkWaitingCount();
674672
};
675673

676674
Worker.prototype.destroy = function() {

test/parallel/test-regress-GH-3238.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
5+
6+
if (cluster.isMaster) {
7+
const worker = cluster.fork();
8+
let disconnected = false;
9+
10+
worker.on('disconnect', common.mustCall(function() {
11+
assert.strictEqual(worker.suicide, true);
12+
disconnected = true;
13+
}));
14+
15+
worker.on('exit', common.mustCall(function() {
16+
assert.strictEqual(worker.suicide, true);
17+
assert.strictEqual(disconnected, true);
18+
}));
19+
} else {
20+
cluster.worker.disconnect();
21+
}

0 commit comments

Comments
 (0)