|
| 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