Skip to content

Commit 90204cc

Browse files
author
Julien Gilli
committed
domains: clear stack when no error handler
Clear domains stack __even if no domain error handler is set__ so that code running in the process' uncaughtException handler, or any code that may be executed when an error is thrown and not caught and that is not the domain's error handler, doesn't run in the context of the domain within which the error was thrown. PR: #4659 PR-URL: #4659 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent e98bcfa commit 90204cc

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

lib/domain.js

+9-15
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ Domain.prototype._errorHandler = function errorHandler(er) {
6262
var caught = false;
6363
var self = this;
6464

65-
function emitError() {
66-
var handled = self.emit('error', er);
67-
68-
// Exit all domains on the stack. Uncaught exceptions end the
69-
// current tick and no domains should be left on the stack
70-
// between ticks.
71-
stack.length = 0;
72-
exports.active = process.domain = null;
73-
74-
return handled;
75-
}
76-
7765
// ignore errors on disposed domains.
7866
//
7967
// XXX This is a bit stupid. We should probably get rid of
@@ -107,7 +95,7 @@ Domain.prototype._errorHandler = function errorHandler(er) {
10795
// if technically the top-level domain is still active, it would
10896
// be ok to abort on an uncaught exception at this point
10997
process._emittingTopLevelDomainError = true;
110-
caught = emitError();
98+
caught = self.emit('error', er);
11199
} finally {
112100
process._emittingTopLevelDomainError = false;
113101
}
@@ -123,7 +111,7 @@ Domain.prototype._errorHandler = function errorHandler(er) {
123111
//
124112
// If caught is false after this, then there's no need to exit()
125113
// the domain, because we're going to crash the process anyway.
126-
caught = emitError();
114+
caught = self.emit('error', er);
127115
} catch (er2) {
128116
// The domain error handler threw! oh no!
129117
// See if another domain can catch THIS error,
@@ -138,9 +126,15 @@ Domain.prototype._errorHandler = function errorHandler(er) {
138126
} else {
139127
caught = false;
140128
}
141-
return caught;
142129
}
143130
}
131+
132+
// Exit all domains on the stack. Uncaught exceptions end the
133+
// current tick and no domains should be left on the stack
134+
// between ticks.
135+
stack.length = 0;
136+
exports.active = process.domain = null;
137+
144138
return caught;
145139
};
146140

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const domain = require('domain');
5+
const assert = require('assert');
6+
7+
const d = domain.create();
8+
9+
process.on('uncaughtException', common.mustCall(function onUncaught() {
10+
assert.equal(process.domain, null,
11+
'domains stack should be empty in uncaughtException handler');
12+
}));
13+
14+
process.on('beforeExit', common.mustCall(function onBeforeExit() {
15+
assert.equal(process.domain, null,
16+
'domains stack should be empty in beforeExit handler');
17+
}));
18+
19+
d.run(function() {
20+
throw new Error('boom');
21+
});
22+

0 commit comments

Comments
 (0)