Skip to content

Commit 6c80ef0

Browse files
committed
node: emit 'exit' when exiting with error
Fix nodejs#3555
1 parent 7550e31 commit 6c80ef0

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/node.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@
251251
caught = process.emit('uncaughtException', er);
252252
}
253253
// if someone handled it, then great. otherwise, die in C++ land
254+
// since that means that we'll exit the process, emit the 'exit' event
255+
if (!caught) {
256+
try {
257+
process.emit('exit', 1);
258+
} catch (er) {
259+
// nothing to be done about it at this point.
260+
}
261+
}
254262
return caught;
255263
};
256264
};

test/message/error_exit.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
var common = require('../common.js');
23+
var assert = require('assert');
24+
25+
process.on('exit', function(code) {
26+
console.error('Exiting with code=%d', code);
27+
});
28+
29+
assert.equal(1, 2);

test/message/error_exit.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Exiting with code=1
2+
3+
assert.js:*
4+
throw new assert.AssertionError({
5+
^
6+
AssertionError: 1 == 2
7+
at Object.<anonymous> (*test*message*error_exit.js:*:*)
8+
at Module._compile (module.js:*:*)
9+
at Object.Module._extensions..js (module.js:*:*)
10+
at Module.load (module.js:*:*)
11+
at Function.Module._load (module.js:*:*)
12+
at Module.runMain (module.js:*:*)
13+
at process._tickCallback (node.js:*:*)

test/message/undefined_reference_in_new_context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ common.error('before');
3030
var Script = process.binding('evals').NodeScript;
3131

3232
// undefined reference
33-
script = new Script('foo.bar = 5;');
33+
var script = new Script('foo.bar = 5;');
3434
script.runInNewContext();
3535

3636
common.error('after');

0 commit comments

Comments
 (0)