From f299d870dcfb71c0b0ab77b20d43d6a1d59d04c0 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 9 Nov 2015 15:08:50 -0500 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/3238 PR-URL: https://github.com/nodejs/node/pull/3720 Reviewed-By: James M Snell --- lib/cluster.js | 22 ++++++++++------------ test/parallel/test-regress-GH-3238.js | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 test/parallel/test-regress-GH-3238.js diff --git a/lib/cluster.js b/lib/cluster.js index 550a17bed9..f202f25cdd 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -654,26 +654,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() { diff --git a/test/parallel/test-regress-GH-3238.js b/test/parallel/test-regress-GH-3238.js new file mode 100644 index 0000000000..a92a09db5f --- /dev/null +++ b/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(); +}