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

domains: emit uncaughtException when appropriate #3637

Closed
wants to merge 2 commits into from
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
11 changes: 6 additions & 5 deletions lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,20 @@ var endMethods = ['end', 'abort', 'destroy', 'destroySoon'];
// a few side effects.
events.usingDomains = true;

// it's possible to enter one domain while already inside
// another one. the stack is each entered domain.
var stack = [];
exports._stack = stack;

// let the process know we're using domains
process._usingDomains();
process._usingDomains(stack);

exports.Domain = Domain;

exports.create = exports.createDomain = function(cb) {
return new Domain(cb);
};

// it's possible to enter one domain while already inside
// another one. the stack is each entered domain.
var stack = [];
exports._stack = stack;
// the active domain is always the one that we're currently in.
exports.active = null;

Expand Down
81 changes: 74 additions & 7 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ Persistent<String> domain_symbol;
// declared in node_internals.h
Persistent<Object> process;

static Persistent<Array> domains_stack;

static Persistent<Function> process_tickFromSpinner;
static Persistent<Function> process_tickCallback;

Expand All @@ -126,6 +128,8 @@ static Persistent<String> exit_symbol;
static Persistent<String> disposed_symbol;

static Persistent<String> emitting_toplevel_domain_error_symbol;
static Persistent<String> _events_symbol;
static Persistent<String> error_symbol;

static bool print_eval = false;
static bool force_repl = false;
Expand Down Expand Up @@ -904,25 +908,82 @@ Handle<Value> FromConstructorTemplate(Persistent<FunctionTemplate> t,
return scope.Close(t->GetFunction()->NewInstance(argc, argv));
}

static bool IsDomainActive() {
if (domain_symbol.IsEmpty())
domain_symbol = NODE_PSYMBOL("domain");
static bool domainHasErrorHandler(const Local<Object>& domain) {
HandleScope scope;

if (_events_symbol.IsEmpty())
_events_symbol = NODE_PSYMBOL("_events");

Local<Value> domain_event_listeners_v = domain->Get(_events_symbol);
if (!domain_event_listeners_v->IsObject())
return false;

Local<Object> domain_event_listeners_o =
domain_event_listeners_v->ToObject();

if (domain_event_listeners_o->IsNull())
return false;

if (error_symbol.IsEmpty())
error_symbol = NODE_PSYMBOL("error");

Local<Value> domain_error_listeners_v =
domain_event_listeners_o->Get(error_symbol);

if (domain_error_listeners_v->IsFunction() ||
(domain_error_listeners_v->IsArray() &&
domain_error_listeners_v.As<Array>()->Length() > 0))
return true;

return false;
}

static bool domainsStackHasErrorHandler() {
HandleScope scope;

if (!using_domains)
return false;

Local<Value> domain_v = process->Get(domain_symbol);
Local<Array> domains_stack_array = Local<Array>::New(domains_stack);
if (domains_stack_array->Length() == 0)
return false;

return domain_v->IsObject();
uint32_t domains_stack_length = domains_stack_array->Length();
for (int i = domains_stack_length - 1; i >= 0; --i) {
Local<Value> domain_v = domains_stack_array->Get(i);
if (domain_v->IsNull())
return false;

Local<Object> domain = domain_v->ToObject();
if (domain->IsNull())
return false;

if (domainHasErrorHandler(domain))
return true;
}

return false;
}

bool ShouldAbortOnUncaughtException() {
Local<Value> emitting_toplevel_domain_error_v =
process->Get(emitting_toplevel_domain_error_symbol);
return !IsDomainActive() || emitting_toplevel_domain_error_v->BooleanValue();

return emitting_toplevel_domain_error_v->BooleanValue() ||
!domainsStackHasErrorHandler();
}

Handle<Value> UsingDomains(const Arguments& args) {
HandleScope scope;

if (using_domains)
return scope.Close(Undefined());

if (!args[0]->IsArray()) {
fprintf(stderr, "domains stack must be an array\n");
abort();
}

using_domains = true;
Local<Value> tdc_v = process->Get(String::New("_tickDomainCallback"));
Local<Value> ndt_v = process->Get(String::New("_nextDomainTick"));
Expand All @@ -934,6 +995,9 @@ Handle<Value> UsingDomains(const Arguments& args) {
fprintf(stderr, "process._nextDomainTick assigned to non-function\n");
abort();
}

domains_stack = Persistent<Array>::New(args[0].As<Array>());

Local<Function> tdc = tdc_v.As<Function>();
Local<Function> ndt = ndt_v.As<Function>();
process->Set(String::New("_tickCallback"), tdc);
Expand Down Expand Up @@ -2449,7 +2513,10 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {
process->Set(String::NewSymbol("_tickInfoBox"), info_box);

// pre-set _events object for faster emit checks
process->Set(String::NewSymbol("_events"), Object::New());
if (_events_symbol.IsEmpty())
_events_symbol = NODE_PSYMBOL("_events");

process->Set(_events_symbol, Object::New());

if (emitting_toplevel_domain_error_symbol.IsEmpty())
emitting_toplevel_domain_error_symbol =
Expand Down
21 changes: 15 additions & 6 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,17 @@
// aborting in these cases.
if (domainStack.length === 1) {
try {
// Set the _emittingTopLevelDomainError so that we know that, even
// 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 = domain.emit('error', er);
// If there's no error handler, do not emit an 'error' event
// as this would throw an error, make the process exit, and thus
// prevent the process 'uncaughtException' event from being emitted
// if a listener is set.
if (domain.listeners('error').length > 0) {
// Set the _emittingTopLevelDomainError so that we know that, even
// 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 = domain.emit('error', er);
}
} finally {
process._emittingTopLevelDomainError = false;
}
Expand Down Expand Up @@ -297,9 +303,12 @@
// current tick and no domains should be left on the stack
// between ticks.
_clearDomainsStack();
} else {
}

if (!caught) {
caught = process.emit('uncaughtException', er);
}

// if someone handled it, then great. otherwise, die in C++ land
// since that means that we'll exit the process, emit the 'exit' event
if (!caught) {
Expand Down
Loading