Skip to content

Commit 2a4f849

Browse files
AndreasMadsenevanlucas
authored andcommitted
async_hooks: rename initTriggerId
rename initTriggerId to defaultTriggerAsyncId such it matches the rest of our naming. Backport-PR-URL: #18079 PR-URL: #17273 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 506d85b commit 2a4f849

15 files changed

+70
-68
lines changed

lib/_http_client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ function responseKeepAlive(res, req) {
579579
socket.removeListener('error', socketErrorListener);
580580
socket.once('error', freeSocketErrorListener);
581581
// There are cases where _handle === null. Avoid those. Passing null to
582-
// nextTick() will call initTriggerId() to retrieve the id.
582+
// nextTick() will call getDefaultTriggerAsyncId() to retrieve the id.
583583
const asyncId = socket._handle ? socket._handle.getAsyncId() : null;
584584
// Mark this socket as available, AFTER user-added end
585585
// handlers have a chance to run.

lib/async_hooks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {
1919
disableHooks,
2020
// Sensitive Embedder API
2121
newUid,
22-
initTriggerId,
22+
getDefaultTriggerAsyncId,
2323
setInitTriggerId,
2424
emitInit,
2525
emitBefore,
@@ -152,7 +152,7 @@ class AsyncResource {
152152
if (typeof opts === 'number') {
153153
opts = { triggerAsyncId: opts, requireManualDestroy: false };
154154
} else if (opts.triggerAsyncId === undefined) {
155-
opts.triggerAsyncId = initTriggerId();
155+
opts.triggerAsyncId = getDefaultTriggerAsyncId();
156156
}
157157

158158
// Unlike emitInitScript, AsyncResource doesn't supports null as the
@@ -245,7 +245,7 @@ Object.defineProperty(module.exports, 'newUid', {
245245

246246
Object.defineProperty(module.exports, 'initTriggerId', {
247247
get: internalUtil.deprecate(function() {
248-
return initTriggerId;
248+
return getDefaultTriggerAsyncId;
249249
}, 'async_hooks.initTriggerId is deprecated. ' +
250250
'Use the AsyncResource default instead.', 'DEP0085')
251251
});

lib/dgram.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const dns = require('dns');
2828
const util = require('util');
2929
const { isUint8Array } = require('internal/util/types');
3030
const EventEmitter = require('events');
31-
const { setInitTriggerId } = require('internal/async_hooks');
31+
const { setDefaultTriggerAsyncId } = require('internal/async_hooks');
3232
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
3333
const { async_id_symbol } = process.binding('async_wrap');
3434
const { nextTick } = require('internal/process/next_tick');
@@ -479,7 +479,7 @@ function doSend(ex, self, ip, list, address, port, callback) {
479479
// node::SendWrap isn't instantiated and attached to the JS instance of
480480
// SendWrap above until send() is called. So don't set the init trigger id
481481
// until now.
482-
setInitTriggerId(self[async_id_symbol]);
482+
setDefaultTriggerAsyncId(self[async_id_symbol]);
483483
var err = self._handle.send(req,
484484
list,
485485
list.length,

lib/internal/async_hooks.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const async_wrap = process.binding('async_wrap');
1414
* kTriggerAsyncId: The trigger_async_id of the resource responsible for
1515
* the current execution stack.
1616
* kAsyncIdCounter: Incremental counter tracking the next assigned async_id.
17-
* kInitTriggerAsyncId: Written immediately before a resource's constructor
17+
* kDefaultTriggerAsyncId: Written immediately before a resource's constructor
1818
* that sets the value of the init()'s triggerAsyncId. The order of
1919
* retrieving the triggerAsyncId value is passing directly to the
20-
* constructor -> value set in kInitTriggerAsyncId -> executionAsyncId of
20+
* constructor -> value set in kDefaultTriggerAsyncId -> executionAsyncId of
2121
* the current resource.
2222
*/
2323
const { async_hook_fields, async_id_fields } = async_wrap;
@@ -61,7 +61,7 @@ const active_hooks = {
6161
// for a given step, that step can bail out early.
6262
const { kInit, kBefore, kAfter, kDestroy, kPromiseResolve,
6363
kCheck, kExecutionAsyncId, kAsyncIdCounter,
64-
kInitTriggerAsyncId } = async_wrap.constants;
64+
kDefaultTriggerAsyncId } = async_wrap.constants;
6565

6666
// Used in AsyncHook and AsyncResource.
6767
const init_symbol = Symbol('init');
@@ -242,25 +242,25 @@ function newUid() {
242242
return ++async_id_fields[kAsyncIdCounter];
243243
}
244244

245-
246245
// Return the triggerAsyncId meant for the constructor calling it. It's up to
247246
// the user to safeguard this call and make sure it's zero'd out when the
248247
// constructor is complete.
249-
function initTriggerId() {
250-
var triggerAsyncId = async_id_fields[kInitTriggerAsyncId];
248+
function getDefaultTriggerAsyncId() {
249+
var defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
251250
// Reset value after it's been called so the next constructor doesn't
252251
// inherit it by accident.
253-
async_id_fields[kInitTriggerAsyncId] = 0;
254-
if (triggerAsyncId <= 0)
255-
triggerAsyncId = async_id_fields[kExecutionAsyncId];
256-
return triggerAsyncId;
252+
async_id_fields[kDefaultTriggerAsyncId] = 0;
253+
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
254+
if (defaultTriggerAsyncId <= 0)
255+
defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId];
256+
return defaultTriggerAsyncId;
257257
}
258258

259259

260-
function setInitTriggerId(triggerAsyncId) {
260+
function setDefaultTriggerAsyncId(triggerAsyncId) {
261261
// CHECK(Number.isSafeInteger(triggerAsyncId))
262262
// CHECK(triggerAsyncId > 0)
263-
async_id_fields[kInitTriggerAsyncId] = triggerAsyncId;
263+
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
264264
}
265265

266266

@@ -279,13 +279,13 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
279279
return;
280280

281281
// This can run after the early return check b/c running this function
282-
// manually means that the embedder must have used initTriggerId().
282+
// manually means that the embedder must have used getDefaultTriggerAsyncId().
283283
if (triggerAsyncId === null) {
284-
triggerAsyncId = initTriggerId();
284+
triggerAsyncId = getDefaultTriggerAsyncId();
285285
} else {
286-
// If a triggerAsyncId was passed, any kInitTriggerAsyncId still must be
286+
// If a triggerAsyncId was passed, any kDefaultTriggerAsyncId still must be
287287
// null'd.
288-
async_id_fields[kInitTriggerAsyncId] = 0;
288+
async_id_fields[kDefaultTriggerAsyncId] = 0;
289289
}
290290

291291
emitInitNative(asyncId, type, triggerAsyncId, resource);
@@ -337,8 +337,8 @@ module.exports = {
337337
disableHooks,
338338
// Sensitive Embedder API
339339
newUid,
340-
initTriggerId,
341-
setInitTriggerId,
340+
getDefaultTriggerAsyncId,
341+
setDefaultTriggerAsyncId,
342342
emitInit: emitInitScript,
343343
emitBefore: emitBeforeScript,
344344
emitAfter: emitAfterScript,

lib/internal/bootstrap_node.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,14 @@
368368
// Internal functions needed to manipulate the stack.
369369
const { clearAsyncIdStack, asyncIdStackSize } = async_wrap;
370370
const { kAfter, kExecutionAsyncId,
371-
kInitTriggerAsyncId } = async_wrap.constants;
371+
kDefaultTriggerAsyncId } = async_wrap.constants;
372372

373373
process._fatalException = function(er) {
374374
var caught;
375375

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

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

lib/internal/process/next_tick.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function setupNextTick() {
4848
const promises = require('internal/process/promises');
4949
const errors = require('internal/errors');
5050
const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks);
51-
const initTriggerId = async_hooks.initTriggerId;
51+
const getDefaultTriggerAsyncId = async_hooks.getDefaultTriggerAsyncId;
5252
// Two arrays that share state between C++ and JS.
5353
const { async_hook_fields, async_id_fields } = async_wrap;
5454
// Used to change the state of the async id stack.
@@ -210,7 +210,7 @@ function setupNextTick() {
210210
nextTickQueue.push(new TickObject(callback,
211211
args,
212212
++async_id_fields[kAsyncIdCounter],
213-
initTriggerId()));
213+
getDefaultTriggerAsyncId()));
214214
}
215215

216216
// `internalNextTick()` will not enqueue any callback when the process is
@@ -237,7 +237,7 @@ function setupNextTick() {
237237
}
238238

239239
if (triggerAsyncId === null)
240-
triggerAsyncId = initTriggerId();
240+
triggerAsyncId = getDefaultTriggerAsyncId();
241241
// In V8 6.2, moving tickInfo & async_id_fields[kAsyncIdCounter] into the
242242
// TickObject incurs a significant performance penalty in the
243243
// next-tick-breadth-args benchmark (revisit later)

lib/net.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const { TCPConnectWrap } = process.binding('tcp_wrap');
4343
const { PipeConnectWrap } = process.binding('pipe_wrap');
4444
const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
4545
const { async_id_symbol } = process.binding('async_wrap');
46-
const { newUid, setInitTriggerId } = require('internal/async_hooks');
46+
const { newUid, setDefaultTriggerAsyncId } = require('internal/async_hooks');
4747
const { nextTick } = require('internal/process/next_tick');
4848
const errors = require('internal/errors');
4949
const dns = require('dns');
@@ -301,7 +301,7 @@ function onSocketFinish() {
301301
// node::ShutdownWrap isn't instantiated and attached to the JS instance of
302302
// ShutdownWrap above until shutdown() is called. So don't set the init
303303
// trigger id until now.
304-
setInitTriggerId(this[async_id_symbol]);
304+
setDefaultTriggerAsyncId(this[async_id_symbol]);
305305
var err = this._handle.shutdown(req);
306306

307307
if (err)
@@ -939,7 +939,7 @@ function internalConnect(
939939
// node::TCPConnectWrap isn't instantiated and attached to the JS instance
940940
// of TCPConnectWrap above until connect() is called. So don't set the init
941941
// trigger id until now.
942-
setInitTriggerId(self[async_id_symbol]);
942+
setDefaultTriggerAsyncId(self[async_id_symbol]);
943943
if (addressType === 4)
944944
err = self._handle.connect(req, address, port);
945945
else
@@ -952,7 +952,7 @@ function internalConnect(
952952
// node::PipeConnectWrap isn't instantiated and attached to the JS instance
953953
// of PipeConnectWrap above until connect() is called. So don't set the
954954
// init trigger id until now.
955-
setInitTriggerId(self[async_id_symbol]);
955+
setDefaultTriggerAsyncId(self[async_id_symbol]);
956956
err = self._handle.connect(req, address, afterConnect);
957957
}
958958

@@ -1091,7 +1091,7 @@ function lookupAndConnect(self, options) {
10911091
debug('connect: dns options', dnsopts);
10921092
self._host = host;
10931093
var lookup = options.lookup || dns.lookup;
1094-
setInitTriggerId(self[async_id_symbol]);
1094+
setDefaultTriggerAsyncId(self[async_id_symbol]);
10951095
lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
10961096
self.emit('lookup', err, ip, addressType, host);
10971097

lib/timers.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const kOnTimeout = TimerWrap.kOnTimeout | 0;
3434
// Two arrays that share state between C++ and JS.
3535
const { async_hook_fields, async_id_fields } = async_wrap;
3636
const {
37-
initTriggerId,
37+
getDefaultTriggerAsyncId,
3838
// The needed emit*() functions.
3939
emitInit,
4040
emitBefore,
@@ -181,7 +181,7 @@ function insert(item, unrefed) {
181181
if (!item[async_id_symbol] || item._destroyed) {
182182
item._destroyed = false;
183183
item[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
184-
item[trigger_async_id_symbol] = initTriggerId();
184+
item[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
185185
if (async_hook_fields[kInit] > 0) {
186186
emitInit(item[async_id_symbol],
187187
'Timeout',
@@ -552,7 +552,7 @@ function Timeout(callback, after, args, isRepeat) {
552552
this._destroyed = false;
553553

554554
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
555-
this[trigger_async_id_symbol] = initTriggerId();
555+
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
556556
if (async_hook_fields[kInit] > 0) {
557557
emitInit(this[async_id_symbol],
558558
'Timeout',
@@ -769,7 +769,7 @@ function Immediate(callback, args) {
769769
this._destroyed = false;
770770

771771
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
772-
this[trigger_async_id_symbol] = initTriggerId();
772+
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
773773
if (async_hook_fields[kInit] > 0) {
774774
emitInit(this[async_id_symbol],
775775
'Immediate',

src/async_wrap.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
310310
}
311311
// get id from parentWrap
312312
double trigger_async_id = parent_wrap->get_async_id();
313-
env->set_init_trigger_async_id(trigger_async_id);
313+
env->set_default_trigger_async_id(trigger_async_id);
314314
}
315315

316316
wrap = PromiseWrap::New(env, promise, parent_wrap, silent);
@@ -541,9 +541,10 @@ void AsyncWrap::Initialize(Local<Object> target,
541541
//
542542
// kAsyncUid: Maintains the state of the next unique id to be assigned.
543543
//
544-
// kInitTriggerAsyncId: Write the id of the resource responsible for a
544+
// kDefaultTriggerAsyncId: Write the id of the resource responsible for a
545545
// handle's creation just before calling the new handle's constructor.
546-
// After the new handle is constructed kInitTriggerAsyncId is set back to 0.
546+
// After the new handle is constructed kDefaultTriggerAsyncId is set back
547+
// to 0.
547548
FORCE_SET_TARGET_FIELD(target,
548549
"async_id_fields",
549550
env->async_hooks()->async_id_fields().GetJSArray());
@@ -563,7 +564,7 @@ void AsyncWrap::Initialize(Local<Object> target,
563564
SET_HOOKS_CONSTANT(kExecutionAsyncId);
564565
SET_HOOKS_CONSTANT(kTriggerAsyncId);
565566
SET_HOOKS_CONSTANT(kAsyncIdCounter);
566-
SET_HOOKS_CONSTANT(kInitTriggerAsyncId);
567+
SET_HOOKS_CONSTANT(kDefaultTriggerAsyncId);
567568
#undef SET_HOOKS_CONSTANT
568569
FORCE_SET_TARGET_FIELD(target, "constants", constants);
569570

@@ -676,7 +677,7 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
676677
void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
677678
async_id_ =
678679
execution_async_id == -1 ? env()->new_async_id() : execution_async_id;
679-
trigger_async_id_ = env()->get_init_trigger_async_id();
680+
trigger_async_id_ = env()->get_default_trigger_async_id();
680681

681682
switch (provider_type()) {
682683
#define V(PROVIDER) \
@@ -777,7 +778,7 @@ async_context EmitAsyncInit(Isolate* isolate,
777778

778779
// Initialize async context struct
779780
if (trigger_async_id == -1)
780-
trigger_async_id = env->get_init_trigger_async_id();
781+
trigger_async_id = env->get_default_trigger_async_id();
781782

782783
async_context context = {
783784
env->new_async_id(), // async_id_

src/connection_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void ConnectionWrap<WrapType, UVType>::OnConnection(uv_stream_t* handle,
4949
};
5050

5151
if (status == 0) {
52-
env->set_init_trigger_async_id(wrap_data->get_async_id());
52+
env->set_default_trigger_async_id(wrap_data->get_async_id());
5353
// Instantiate the client javascript object and handle.
5454
Local<Object> client_obj = WrapType::Instantiate(env,
5555
wrap_data,

src/env-inl.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,18 @@ inline double Environment::trigger_async_id() {
440440
return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
441441
}
442442

443-
inline double Environment::get_init_trigger_async_id() {
444-
AliasedBuffer<double, v8::Float64Array>& async_id_fields =
445-
async_hooks()->async_id_fields();
446-
double tid = async_id_fields[AsyncHooks::kInitTriggerAsyncId];
447-
async_id_fields[AsyncHooks::kInitTriggerAsyncId] = 0;
448-
if (tid <= 0) tid = execution_async_id();
449-
return tid;
450-
}
451-
452-
inline void Environment::set_init_trigger_async_id(const double id) {
453-
async_hooks()->async_id_fields()[AsyncHooks::kInitTriggerAsyncId] = id;
443+
inline double Environment::get_default_trigger_async_id() {
444+
double default_trigger_async_id =
445+
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
446+
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = 0;
447+
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
448+
if (default_trigger_async_id <= 0)
449+
default_trigger_async_id = execution_async_id();
450+
return default_trigger_async_id;
451+
}
452+
453+
inline void Environment::set_default_trigger_async_id(const double id) {
454+
async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = id;
454455
}
455456

456457
inline double* Environment::heap_statistics_buffer() const {

src/env.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class Environment {
389389
kExecutionAsyncId,
390390
kTriggerAsyncId,
391391
kAsyncIdCounter,
392-
kInitTriggerAsyncId,
392+
kDefaultTriggerAsyncId,
393393
kUidFieldsCount,
394394
};
395395

@@ -565,8 +565,8 @@ class Environment {
565565
inline double new_async_id();
566566
inline double execution_async_id();
567567
inline double trigger_async_id();
568-
inline double get_init_trigger_async_id();
569-
inline void set_init_trigger_async_id(const double id);
568+
inline double get_default_trigger_async_id();
569+
inline void set_default_trigger_async_id(const double id);
570570

571571
// List of id's that have been destroyed and need the destroy() cb called.
572572
inline std::vector<double>* destroy_async_id_list();

0 commit comments

Comments
 (0)