Skip to content

Commit 47f3735

Browse files
cjihrigFishrock123
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 9ef81ff commit 47f3735

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

lib/cluster.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -654,26 +654,24 @@ function workerInit() {
654654

655655
Worker.prototype.disconnect = function() {
656656
this.suicide = true;
657-
var waitingHandles = 0;
657+
let waitingCount = 1;
658658

659-
function checkRemainingHandles() {
660-
waitingHandles--;
661-
if (waitingHandles === 0) {
659+
function checkWaitingCount() {
660+
waitingCount--;
661+
if (waitingCount === 0) {
662+
send({ act: 'suicide' });
662663
process.disconnect();
663664
}
664665
}
665666

666-
for (var key in handles) {
667-
var handle = handles[key];
667+
for (const key in handles) {
668+
const handle = handles[key];
668669
delete handles[key];
669-
waitingHandles++;
670-
handle.owner.close(checkRemainingHandles);
671-
}
672-
673-
if (waitingHandles === 0) {
674-
process.disconnect();
670+
waitingCount++;
671+
handle.owner.close(checkWaitingCount);
675672
}
676673

674+
checkWaitingCount();
677675
};
678676

679677
Worker.prototype.destroy = function() {

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

Lines changed: 21 additions & 0 deletions
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)