Skip to content

Commit f2a45ca

Browse files
dohseJulien Gilli
authored and
Julien Gilli
committed
domains: fix stack clearing after error handled
caeb677 introduced a regression where the domains stack would not be cleared after an error had been handled by the top-level domain. This change clears the domains stack regardless of the position of the active domain in the stack. PR: nodejs#9364 PR-URL: nodejs/node-v0.x-archive#9364 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Julien Gilli <[email protected]>
1 parent 51fe319 commit f2a45ca

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

src/node.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@
216216
return startup._lazyConstants;
217217
};
218218

219+
function _clearDomainsStack() {
220+
var domainModule = NativeModule.require('domain');
221+
var domainStack = domainModule._stack;
222+
domainStack.length = 0;
223+
domainModule.active = null;
224+
process.domain = null;
225+
}
226+
219227
startup.processFatal = function() {
220228
// call into the active domain, or emit uncaughtException,
221229
// and exit if there are no listeners.
@@ -268,13 +276,6 @@
268276
// If caught is false after this, then there's no need to exit()
269277
// the domain, because we're going to crash the process anyway.
270278
caught = domain.emit('error', er);
271-
272-
// Exit all domains on the stack. Uncaught exceptions end the
273-
// current tick and no domains should be left on the stack
274-
// between ticks.
275-
var domainModule = NativeModule.require('domain');
276-
domainStack.length = 0;
277-
domainModule.active = process.domain = null;
278279
} catch (er2) {
279280
// The domain error handler threw! oh no!
280281
// See if another domain can catch THIS error,
@@ -291,6 +292,11 @@
291292
}
292293
}
293294
}
295+
296+
// Exit all domains on the stack. Uncaught exceptions end the
297+
// current tick and no domains should be left on the stack
298+
// between ticks.
299+
_clearDomainsStack();
294300
} else {
295301
caught = process.emit('uncaughtException', er);
296302
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 assert = require('assert');
23+
var domain = require('domain');
24+
25+
/*
26+
* Make sure that the domains stack is cleared after a top-level domain
27+
* error handler exited gracefully.
28+
*/
29+
var d = domain.create();
30+
31+
d.on('error', function() {
32+
process.nextTick(function() {
33+
if (domain._stack.length !== 1) {
34+
process.exit(1);
35+
}
36+
});
37+
});
38+
39+
d.run(function() {
40+
throw new Error('Error from domain');
41+
});

0 commit comments

Comments
 (0)