Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

domain: fix clearing domains stack #4659

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ Domain.prototype._errorHandler = function errorHandler(er) {
var caught = false;
var self = this;

function emitError() {
var handled = self.emit('error', er);

// Exit all domains on the stack. Uncaught exceptions end the
// current tick and no domains should be left on the stack
// between ticks.
stack.length = 0;
exports.active = process.domain = null;

return handled;
}

// ignore errors on disposed domains.
//
// XXX This is a bit stupid. We should probably get rid of
Expand Down Expand Up @@ -107,7 +95,7 @@ Domain.prototype._errorHandler = function errorHandler(er) {
// if technically the top-level domain is still active, it would
// be ok to abort on an uncaught exception at this point
process._emittingTopLevelDomainError = true;
caught = emitError();
caught = self.emit('error', er);
} finally {
process._emittingTopLevelDomainError = false;
}
Expand All @@ -123,7 +111,7 @@ Domain.prototype._errorHandler = function errorHandler(er) {
//
// If caught is false after this, then there's no need to exit()
// the domain, because we're going to crash the process anyway.
caught = emitError();
caught = self.emit('error', er);
} catch (er2) {
// The domain error handler threw! oh no!
// See if another domain can catch THIS error,
Expand All @@ -138,9 +126,15 @@ Domain.prototype._errorHandler = function errorHandler(er) {
} else {
caught = false;
}
return caught;
}
}

// Exit all domains on the stack. Uncaught exceptions end the
// current tick and no domains should be left on the stack
// between ticks.
stack.length = 0;
exports.active = process.domain = null;

return caught;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const common = require('../common');
const domain = require('domain');
const assert = require('assert');

const d = domain.create();

process.on('uncaughtException', common.mustCall(function onUncaught() {
assert.equal(process.domain, null,
'domains stack should be empty in uncaughtException handler');
}));

process.on('beforeExit', common.mustCall(function onBeforeExit() {
assert.equal(process.domain, null,
'domains stack should be empty in beforeExit handler');
}));

d.run(function() {
throw new Error('boom');
});