Skip to content

Commit d44ce97

Browse files
committed
cluster: don't always kill the master on uncaughtException
uncaughtException handlers installed by the user override the default one that the cluster module installs, the one that kills off the master process. Fixes nodejs#2556.
1 parent 091ab85 commit d44ce97

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

lib/cluster.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ function startMaster() {
7878
workerArgs = process.argv.slice(2);
7979

8080
process.on('uncaughtException', function(e) {
81+
// Did the user install a listener? If so, it overrides this one.
82+
if (process.listeners('uncaughtException').length > 1) return;
83+
8184
// Quickly try to kill all the workers.
8285
// TODO: be session leader - will cause auto SIGHUP to the children.
8386
eachWorker(function(worker) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
// Installing a custom uncaughtException handler should override the default
23+
// one that the cluster module installs.
24+
// https://github.com/joyent/node/issues/2556
25+
26+
var common = require('../common');
27+
var assert = require('assert');
28+
var cluster = require('cluster');
29+
var fork = require('child_process').fork;
30+
31+
var MAGIC_EXIT_CODE = 42;
32+
33+
var isTestRunner = process.argv[2] != 'child';
34+
35+
if (isTestRunner) {
36+
var exitCode = -1;
37+
38+
process.on('exit', function() {
39+
assert.equal(exitCode, MAGIC_EXIT_CODE);
40+
});
41+
42+
var master = fork(__filename, ['child']);
43+
master.on('exit', function(code) {
44+
exitCode = code;
45+
});
46+
}
47+
else if (cluster.isMaster) {
48+
process.on('uncaughtException', function() {
49+
process.nextTick(function() {
50+
process.exit(MAGIC_EXIT_CODE);
51+
});
52+
});
53+
54+
cluster.fork();
55+
throw new Error('kill master');
56+
}
57+
else { // worker
58+
process.exit();
59+
}

0 commit comments

Comments
 (0)