From 5883a59b21a97e8b7339f435c977155a2c29ba8d Mon Sep 17 00:00:00 2001 From: Oleg Elifantiev Date: Thu, 2 Apr 2015 23:27:27 +0300 Subject: [PATCH] cluster: disconnect event not emitted correctly Inside of a worker, disconnect event was not emitted on cluster.worker Fixes: https://github.com/iojs/io.js/issues/1304 PR-URL: https://github.com/iojs/io.js/pull/1386 Reviewed-By: Colin Ihrig --- lib/cluster.js | 1 + test/parallel/test-cluster-worker-disconnect.js | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/cluster.js b/lib/cluster.js index 10a55996f15cc8..9a7900bc92b04e 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -511,6 +511,7 @@ function workerInit() { }); cluster.worker = worker; process.once('disconnect', function() { + worker.emit('disconnect'); if (!worker.suicide) { // Unexpected disconnect, master exited, or some such nastiness, so // worker exits immediately. diff --git a/test/parallel/test-cluster-worker-disconnect.js b/test/parallel/test-cluster-worker-disconnect.js index 4bb9b163b64982..e7a0960cb1026f 100644 --- a/test/parallel/test-cluster-worker-disconnect.js +++ b/test/parallel/test-cluster-worker-disconnect.js @@ -8,6 +8,10 @@ if (cluster.isWorker) { }).listen(common.PORT, '127.0.0.1'); + cluster.worker.on('disconnect', function() { + process.exit(42); + }); + } else if (cluster.isMaster) { var checks = { @@ -18,6 +22,7 @@ if (cluster.isWorker) { }, worker: { emitDisconnect: false, + emitDisconnectInsideWorker: false, emitExit: false, state: false, suicideMode: false, @@ -59,9 +64,11 @@ if (cluster.isWorker) { }); // Check that the worker died - worker.once('exit', function() { + worker.once('exit', function(code) { checks.worker.emitExit = true; checks.worker.died = !alive(worker.process.pid); + checks.worker.emitDisconnectInsideWorker = code === 42; + process.nextTick(function() { process.exit(0); }); @@ -74,6 +81,7 @@ if (cluster.isWorker) { // events assert.ok(w.emitDisconnect, 'Disconnect event did not emit'); + assert.ok(w.emitDisconnectInsideWorker, 'Disconnect event did not emit inside worker'); assert.ok(c.emitDisconnect, 'Disconnect event did not emit'); assert.ok(w.emitExit, 'Exit event did not emit'); assert.ok(c.emitExit, 'Exit event did not emit');