Skip to content

Commit f56eb2a

Browse files
AndreasMadsenevanlucas
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: #18079 PR-URL: #17273 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2a4f849 commit f56eb2a

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

lib/internal/async_hooks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ function getDefaultTriggerAsyncId() {
249249
var defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
250250
// Reset value after it's been called so the next constructor doesn't
251251
// inherit it by accident.
252-
async_id_fields[kDefaultTriggerAsyncId] = 0;
252+
async_id_fields[kDefaultTriggerAsyncId] = -1;
253253
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
254-
if (defaultTriggerAsyncId <= 0)
254+
if (defaultTriggerAsyncId < 0)
255255
defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId];
256256
return defaultTriggerAsyncId;
257257
}
@@ -285,7 +285,7 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
285285
} else {
286286
// If a triggerAsyncId was passed, any kDefaultTriggerAsyncId still must be
287287
// null'd.
288-
async_id_fields[kDefaultTriggerAsyncId] = 0;
288+
async_id_fields[kDefaultTriggerAsyncId] = -1;
289289
}
290290

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

lib/internal/bootstrap_node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@
375375

376376
// It's possible that kDefaultTriggerAsyncId was set for a constructor
377377
// call that threw and was never cleared. So clear it now.
378-
async_id_fields[kDefaultTriggerAsyncId] = 0;
378+
async_id_fields[kDefaultTriggerAsyncId] = -1;
379379

380380
if (exceptionHandlerState.captureFn !== null) {
381381
exceptionHandlerState.captureFn(er);

src/env-inl.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ inline Environment::AsyncHooks::AsyncHooks(v8::Isolate* isolate)
6666
// and flag changes won't be included.
6767
fields_[kCheck] = 1;
6868

69+
// kDefaultTriggerAsyncId should be -1, this indicates that there is no
70+
// specified default value and it should fallback to the executionAsyncId.
71+
// 0 is not used as the magic value, because that indicates a missing context
72+
// which is different from a default context.
73+
async_id_fields_[AsyncHooks::kDefaultTriggerAsyncId] = -1;
74+
6975
// kAsyncIdCounter should start at 1 because that'll be the id the execution
7076
// context during bootstrap (code that runs before entering uv_run()).
7177
async_id_fields_[AsyncHooks::kAsyncIdCounter] = 1;
@@ -443,9 +449,9 @@ inline double Environment::trigger_async_id() {
443449
inline double Environment::get_default_trigger_async_id() {
444450
double default_trigger_async_id =
445451
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
446-
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = 0;
452+
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = -1;
447453
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
448-
if (default_trigger_async_id <= 0)
454+
if (default_trigger_async_id < 0)
449455
default_trigger_async_id = execution_async_id();
450456
return default_trigger_async_id;
451457
}

0 commit comments

Comments
 (0)