From f56eb2a41e12e009fda654694780bfe58ea87a37 Mon Sep 17 00:00:00 2001 From: Andreas Madsen Date: Wed, 22 Nov 2017 15:41:18 +0100 Subject: [PATCH] async_hooks: separate missing from default context When context is missing the executionAsyncId will be zero. For the default triggerAsyncId the zero value was used to default to the executionAsyncId. While this was not technically wrong because the functions are different themself, it poorly separated the two concepts. Backport-PR-URL: https://github.com/nodejs/node/pull/18079 PR-URL: https://github.com/nodejs/node/pull/17273 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/internal/async_hooks.js | 6 +++--- lib/internal/bootstrap_node.js | 2 +- src/env-inl.h | 10 ++++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index ba08b5e3838468..13e1669374df69 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -249,9 +249,9 @@ function getDefaultTriggerAsyncId() { var defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId]; // Reset value after it's been called so the next constructor doesn't // inherit it by accident. - async_id_fields[kDefaultTriggerAsyncId] = 0; + async_id_fields[kDefaultTriggerAsyncId] = -1; // If defaultTriggerAsyncId isn't set, use the executionAsyncId - if (defaultTriggerAsyncId <= 0) + if (defaultTriggerAsyncId < 0) defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId]; return defaultTriggerAsyncId; } @@ -285,7 +285,7 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) { } else { // If a triggerAsyncId was passed, any kDefaultTriggerAsyncId still must be // null'd. - async_id_fields[kDefaultTriggerAsyncId] = 0; + async_id_fields[kDefaultTriggerAsyncId] = -1; } emitInitNative(asyncId, type, triggerAsyncId, resource); diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 8d86630c888e25..31f95d1f417e2d 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -375,7 +375,7 @@ // It's possible that kDefaultTriggerAsyncId was set for a constructor // call that threw and was never cleared. So clear it now. - async_id_fields[kDefaultTriggerAsyncId] = 0; + async_id_fields[kDefaultTriggerAsyncId] = -1; if (exceptionHandlerState.captureFn !== null) { exceptionHandlerState.captureFn(er); diff --git a/src/env-inl.h b/src/env-inl.h index 2981f0f17eb1e5..3eb5a2175e22e3 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -66,6 +66,12 @@ inline Environment::AsyncHooks::AsyncHooks(v8::Isolate* isolate) // and flag changes won't be included. fields_[kCheck] = 1; + // kDefaultTriggerAsyncId should be -1, this indicates that there is no + // specified default value and it should fallback to the executionAsyncId. + // 0 is not used as the magic value, because that indicates a missing context + // which is different from a default context. + async_id_fields_[AsyncHooks::kDefaultTriggerAsyncId] = -1; + // kAsyncIdCounter should start at 1 because that'll be the id the execution // context during bootstrap (code that runs before entering uv_run()). async_id_fields_[AsyncHooks::kAsyncIdCounter] = 1; @@ -443,9 +449,9 @@ inline double Environment::trigger_async_id() { inline double Environment::get_default_trigger_async_id() { double default_trigger_async_id = async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId]; - async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = 0; + async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = -1; // If defaultTriggerAsyncId isn't set, use the executionAsyncId - if (default_trigger_async_id <= 0) + if (default_trigger_async_id < 0) default_trigger_async_id = execution_async_id(); return default_trigger_async_id; }