diff options
author | Sam Roberts <sam@strongloop.com> | 2013-12-02 19:00:39 -0800 |
---|---|---|
committer | Timothy J Fontaine <tjfontaine@gmail.com> | 2013-12-31 11:43:43 -0800 |
commit | dce35146e0e52de32c3836d01ada10f3ebbe8792 (patch) | |
tree | 5567215aefdbb5ce1c042f4db740136f00195cc9 /lib | |
parent | 6f40abe2d445516de629d289afe21f458e7cc1e9 (diff) | |
download | nodejs-dce35146e0e52de32c3836d01ada10f3ebbe8792.tar.gz nodejs-dce35146e0e52de32c3836d01ada10f3ebbe8792.tar.bz2 nodejs-dce35146e0e52de32c3836d01ada10f3ebbe8792.zip |
cluster: only forcibly exit worker on unclean exit
Fix inadvertent v0.11 changes to the definition of suicide, particularly
the relationship between suicide state, the disconnect event, and when
exit should occur.
In v0.10, workers don't forcibly exit on disconnect, it doesn't give
them time to do a graceful finish of open client connections, they exit
under normal node rules - when there is nothing left to do. But on
unexpected disconnect they do exit so the workers aren't left around
after the master.
Note that a test as-written was invalid, it failed against the v0.10
cluster API, demonstrating that it was an undocumented API change.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cluster.js | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/cluster.js b/lib/cluster.js index 4e9ce2a8d..f75fda521 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -343,8 +343,8 @@ function masterInit() { signo = signo || 'SIGTERM'; var proc = this.process; if (proc.connected) { - proc.once('disconnect', proc.kill.bind(proc, signo)); - proc.disconnect(); + this.once('disconnect', proc.kill.bind(proc, signo)); + this.disconnect(); return; } proc.kill(signo); @@ -444,7 +444,13 @@ function workerInit() { worker.id = +process.env.NODE_UNIQUE_ID | 0; worker.state = 'online'; worker.process = process; - process.once('disconnect', process.exit.bind(null, 0)); + process.once('disconnect', function() { + if (!worker.suicide) { + // Unexpected disconnect, master exited, or some such nastiness, so + // worker exits immediately. + process.exit(0); + } + }); process.on('internalMessage', internal(worker, onmessage)); send({ act: 'online' }); function onmessage(message, handle) { @@ -554,6 +560,7 @@ function workerInit() { } Worker.prototype.disconnect = function() { + this.suicide = true; for (var key in handles) { var handle = handles[key]; delete handles[key]; @@ -563,6 +570,7 @@ function workerInit() { }; Worker.prototype.destroy = function() { + this.suicide = true; if (!process.connected) process.exit(0); var exit = process.exit.bind(null, 0); send({ act: 'suicide' }, exit); |