src: implement native changes for async_hooks

Changes in the native code for the upcoming async_hooks module. These
have been separated to help with review and testing.

Changes include:

* Introduce an async id stack that tracks recursive calls into async
  execution contexts. For performance reasons the id stack is held as a
  double* and assigned to a Float64Array. If the stack grows too large
  it is then placed in it's own stack and replaced with a new double*.
  This should accommodate arbitrarily large stacks.

  I'm not especially happy with the complexity involved with this async
  id stack, but it's also the fastest and most full proof way of
  handling it that I have found.

* Add helper functions in Environment and AsyncWrap to work with the
  async id stack.

* Add AsyncWrap::Reset() to allow AsyncWrap instances that have been
  placed in a resource pool, instead of being released, to be
  reinitialized. AsyncWrap::AsyncWrap() also now uses Reset() for
  initialization.

* AsyncWrap* parent no longer needs to be passed via the constructor.

* Introduce Environment::AsyncHooks class to contain the needed native
  functionality. This includes the pointer to the async id stack, and
  array of v8::Eternal<v8::String>'s that hold the names of all
  providers, mechanisms for storing/retrieving the trigger id, etc.

* Introduce Environment::AsyncHooks::ExecScope as a way to track the
  current id and trigger id of function execution via RAII.

* If the user passes --abort-on-uncaught-exception then instead of
  throwing the application will print a stack trace and abort.

PR-URL: https://github.com/nodejs/node/pull/12892
Ref: https://github.com/nodejs/node/pull/11883
Ref: https://github.com/nodejs/node/pull/8531
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
Trevor Norris authored and Anna Henningsen committed 2017-05-10 22:22:20 +02:00
1 parent fe2df3b842
commit c0bde73f1b
23 files changed
+528 -295

No files matched your search

+31 -41
View File
@@ -154,6 +154,8 @@ using v8::Uint32Array;
using v8::V8;
using v8::Value;
using AsyncHooks = node::Environment::AsyncHooks;
static bool print_eval = false;
static bool force_repl = false;
static bool syntax_check_only = false;
@@ -174,6 +176,7 @@ static node_module* modlist_linked;
static node_module* modlist_addon;
static bool trace_enabled = false;
static std::string trace_enabled_categories; // NOLINT(runtime/string)
static bool abort_on_uncaught_exception = false;
#if defined(NODE_HAVE_I18N_SUPPORT)
// Path to ICU data (for i18n / Intl)
@@ -1302,21 +1305,13 @@ Local<Value> MakeCallback(Environment* env,
// If you hit this assertion, you forgot to enter the v8::Context first.
CHECK_EQ(env->context(), env->isolate()->GetCurrentContext());
Local<Function> pre_fn = env->async_hooks_pre_function();
Local<Function> post_fn = env->async_hooks_post_function();
Local<Object> object, domain;
bool ran_init_callback = false;
bool has_domain = false;
Environment::AsyncCallbackScope callback_scope(env);
// TODO(trevnorris): Adding "_asyncQueue" to the "this" in the init callback
// is a horrible way to detect usage. Rethink how detection should happen.
if (recv->IsObject()) {
object = recv.As<Object>();
Local<Value> async_queue_v = object->Get(env->async_queue_string());
if (async_queue_v->IsObject())
ran_init_callback = true;
}
if (env->using_domains()) {
@@ -1340,34 +1335,13 @@ Local<Value> MakeCallback(Environment* env,
}
}
if (ran_init_callback && !pre_fn.IsEmpty()) {
TryCatch try_catch(env->isolate());
MaybeLocal<Value> ar = pre_fn->Call(env->context(), object, 0, nullptr);
if (ar.IsEmpty()) {
ClearFatalExceptionHandlers(env);
FatalException(env->isolate(), try_catch);
return Local<Value>();
}
}
// TODO(trevnorris): Correct this once node::MakeCallback() support id and
// triggerId. Consider completely removing it until then so the async id can
// propagate through to the fatalException after hook calls.
AsyncHooks::ExecScope exec_scope(env, 0, 0);
Local<Value> ret = callback->Call(recv, argc, argv);
if (ran_init_callback && !post_fn.IsEmpty()) {
Local<Value> did_throw = Boolean::New(env->isolate(), ret.IsEmpty());
// Currently there's no way to retrieve an uid from node::MakeCallback().
// This needs to be fixed.
Local<Value> vals[] =
{ Undefined(env->isolate()).As<Value>(), did_throw };
TryCatch try_catch(env->isolate());
MaybeLocal<Value> ar =
post_fn->Call(env->context(), object, arraysize(vals), vals);
if (ar.IsEmpty()) {
ClearFatalExceptionHandlers(env);
FatalException(env->isolate(), try_catch);
return Local<Value>();
}
}
if (ret.IsEmpty()) {
// NOTE: For backwards compatibility with public API we return Undefined()
// if the top level call threw.
@@ -1375,6 +1349,8 @@ Local<Value> MakeCallback(Environment* env,
ret : Undefined(env->isolate()).As<Value>();
}
exec_scope.Dispose();
if (has_domain) {
Local<Value> exit_v = domain->Get(env->exit_string());
if (exit_v->IsFunction()) {
@@ -1395,6 +1371,11 @@ Local<Value> MakeCallback(Environment* env,
env->isolate()->RunMicrotasks();
}
// Make sure the stack unwound properly. If there are nested MakeCallback's
// then it should return early and not reach this code.
CHECK_EQ(env->current_async_id(), 0);
CHECK_EQ(env->trigger_id(), 0);
Local<Object> process = env->process_object();
if (tick_info->length() == 0) {
@@ -1411,10 +1392,10 @@ Local<Value> MakeCallback(Environment* env,
Local<Value> MakeCallback(Environment* env,
Local<Object> recv,
Local<String> symbol,
int argc,
Local<Value> argv[]) {
Local<Object> recv,
Local<String> symbol,
int argc,
Local<Value> argv[]) {
Local<Value> cb_v = recv->Get(symbol);
CHECK(cb_v->IsFunction());
return MakeCallback(env, recv.As<Value>(), cb_v.As<Function>(), argc, argv);
@@ -1422,10 +1403,10 @@ Local<Value> MakeCallback(Environment* env,
Local<Value> MakeCallback(Environment* env,
Local<Object> recv,
const char* method,
int argc,
Local<Value> argv[]) {
Local<Object> recv,
const char* method,
int argc,
Local<Value> argv[]) {
Local<String> method_string = OneByteString(env->isolate(), method);
return MakeCallback(env, recv, method_string, argc, argv);
}
@@ -3925,6 +3906,12 @@ static void ParseArgs(int* argc,
} else if (strcmp(arg, "--") == 0) {
index += 1;
break;
} else if (strcmp(arg, "--abort-on-uncaught-exception") ||
strcmp(arg, "--abort_on_uncaught_exception")) {
abort_on_uncaught_exception = true;
// Also a V8 option. Pass through as-is.
new_v8_argv[new_v8_argc] = arg;
new_v8_argc += 1;
} else {
// V8 option. Pass through as-is.
new_v8_argv[new_v8_argc] = arg;
@@ -4471,8 +4458,11 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
if (debug_options.inspector_enabled() && !debugger_running)
return 12; // Signal internal error.
env.set_abort_on_uncaught_exception(abort_on_uncaught_exception);
{
Environment::AsyncCallbackScope callback_scope(&env);
Environment::AsyncHooks::ExecScope exec_scope(&env, 1, 0);
LoadEnvironment(&env);
}