From ee643bd0facfb75e77dd132260801f9e78a4c838 Mon Sep 17 00:00:00 2001 From: Oleg Elifantiev Date: Thu, 2 Apr 2015 23:27:27 +0300 Subject: [PATCH] cluster: disconnect event was not emitted correctly Fix for iojs/io.js#1304 Inside of a worker, disconnect event was not emitted on cluster.worker --- lib/cluster.js | 1 + .../parallel/test-cluster-worker-disconnect.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/cluster.js b/lib/cluster.js index 08230ad4aa882c..29c906b811d829 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -509,6 +509,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..431fe716759581 100644 --- a/test/parallel/test-cluster-worker-disconnect.js +++ b/test/parallel/test-cluster-worker-disconnect.js @@ -1,6 +1,8 @@ var common = require('../common'); var assert = require('assert'); var cluster = require('cluster'); +var fs = require('fs'); +var path = require('path'); if (cluster.isWorker) { var http = require('http'); @@ -8,6 +10,11 @@ if (cluster.isWorker) { }).listen(common.PORT, '127.0.0.1'); + // Save flag-file on disconnect + cluster.worker.on('disconnect', function(){ + fs.writeFileSync(path.join(__dirname, process.pid + ''), ''); + }); + } else if (cluster.isMaster) { var checks = { @@ -18,6 +25,7 @@ if (cluster.isWorker) { }, worker: { emitDisconnect: false, + emitDisconnectInsideWorker: false, emitExit: false, state: false, suicideMode: false, @@ -62,6 +70,15 @@ if (cluster.isWorker) { worker.once('exit', function() { checks.worker.emitExit = true; checks.worker.died = !alive(worker.process.pid); + + var flagFile = path.join(__dirname, worker.process.pid + ''); + checks.worker.emitDisconnectInsideWorker = fs.existsSync(flagFile); + + // cleanup + if (checks.worker.emitDisconnectInsideWorker) { + fs.unlinkSync(flagFile); + } + process.nextTick(function() { process.exit(0); }); @@ -74,6 +91,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');