Skip to content

Commit 14c911d

Browse files
cxregisaacs
authored andcommitted
domain: empty stack on all exceptions
Due to the nature of asyncronous programming, it's impossible to know what will run on the next tick. Because of this, it's not correct to maintain domain stack state between ticks Since the _fatalException handler is only invoked after the stack is unwound, once it exits the tick will end. The only reasonable thing to do in that case is to exit *all* domains.
1 parent 33d2242 commit 14c911d

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/node.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,14 @@
243243
// If caught is false after this, then there's no need to exit()
244244
// the domain, because we're going to crash the process anyway.
245245
caught = domain.emit('error', er);
246-
domain.exit();
246+
247+
// Exit all domains on the stack. Uncaught exceptions end the
248+
// current tick and no domains should be left on the stack between
249+
// ticks. Since a domain exists, this require will not be loading
250+
// it for the first time and should be safe.
251+
var domainModule = NativeModule.require('domain');
252+
domainModule._stack.length = 0;
253+
domainModule.active = process.domain = null;
247254
} catch (er2) {
248255
caught = false;
249256
}

test/simple/test-domain-nested.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
23+
// Make sure that the nested domains don't cause the domain stack to grow
24+
25+
var assert = require('assert');
26+
var domain = require('domain');
27+
28+
process.on('exit', function(c) {
29+
assert.equal(domain._stack.length, 0);
30+
});
31+
32+
domain.create().run(function() {
33+
domain.create().run(function() {
34+
domain.create().run(function() {
35+
domain.create().on("error", function(e) {
36+
// Don't need to do anything here
37+
}).run(function() {
38+
throw new Error("died")
39+
})
40+
})
41+
})
42+
})

0 commit comments

Comments
 (0)