Skip to content

Commit

Permalink
cluster: disconnect event was not emitted correctly
Browse files Browse the repository at this point in the history
Fix for nodejs#1304
Inside of a worker, disconnect event was not emitted on cluster.worker
  • Loading branch information
Olegas committed Apr 9, 2015
1 parent 1219e74 commit ee643bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-cluster-worker-disconnect.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
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');
http.Server(function() {

}).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 = {
Expand All @@ -18,6 +25,7 @@ if (cluster.isWorker) {
},
worker: {
emitDisconnect: false,
emitDisconnectInsideWorker: false,
emitExit: false,
state: false,
suicideMode: false,
Expand Down Expand Up @@ -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);
});
Expand All @@ -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');
Expand Down

0 comments on commit ee643bd

Please sign in to comment.