Skip to content

Commit 03873db

Browse files
AndreasMadsenMylesBorins
authored andcommitted
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: #18179 PR-URL: #17273 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent cce92cc commit 03873db

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

lib/internal/async_hooks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ function getDefaultTriggerAsyncId() {
252252
var defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
253253
// Reset value after it's been called so the next constructor doesn't
254254
// inherit it by accident.
255-
async_id_fields[kDefaultTriggerAsyncId] = 0;
255+
async_id_fields[kDefaultTriggerAsyncId] = -1;
256256
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
257-
if (defaultTriggerAsyncId <= 0)
257+
if (defaultTriggerAsyncId < 0)
258258
defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId];
259259
return defaultTriggerAsyncId;
260260
}
@@ -288,7 +288,7 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
288288
} else {
289289
// If a triggerAsyncId was passed, any kDefaultTriggerAsyncId still must be
290290
// null'd.
291-
async_id_fields[kDefaultTriggerAsyncId] = 0;
291+
async_id_fields[kDefaultTriggerAsyncId] = -1;
292292
}
293293

294294
emitInitNative(asyncId, type, triggerAsyncId, resource);

lib/internal/bootstrap_node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@
366366

367367
// It's possible that kDefaultTriggerAsyncId was set for a constructor
368368
// call that threw and was never cleared. So clear it now.
369-
async_id_fields[kDefaultTriggerAsyncId] = 0;
369+
async_id_fields[kDefaultTriggerAsyncId] = -1;
370370

371371
if (process.domain && process.domain._errorHandler)
372372
caught = process.domain._errorHandler(er);

src/env-inl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ inline Environment::AsyncHooks::AsyncHooks(v8::Isolate* isolate)
8787
async_id_fields_(isolate, kUidFieldsCount) {
8888
v8::HandleScope handle_scope(isolate_);
8989

90+
// kDefaultTriggerAsyncId should be -1, this indicates that there is no
91+
// specified default value and it should fallback to the executionAsyncId.
92+
// 0 is not used as the magic value, because that indicates a missing context
93+
// which is different from a default context.
94+
async_id_fields_[AsyncHooks::kDefaultTriggerAsyncId] = -1;
95+
9096
// kAsyncIdCounter should start at 1 because that'll be the id the execution
9197
// context during bootstrap (code that runs before entering uv_run()).
9298
async_id_fields_[AsyncHooks::kAsyncIdCounter] = 1;
@@ -467,9 +473,9 @@ inline double Environment::trigger_async_id() {
467473
inline double Environment::get_default_trigger_async_id() {
468474
double default_trigger_async_id =
469475
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
470-
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = 0;
476+
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = -1;
471477
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
472-
if (default_trigger_async_id <= 0)
478+
if (default_trigger_async_id < 0)
473479
default_trigger_async_id = execution_async_id();
474480
return default_trigger_async_id;
475481
}

0 commit comments

Comments
 (0)