From 2a4f849c39c89adf540b0314adefe068b3ade9d4 Mon Sep 17 00:00:00 2001 From: Andreas Madsen Date: Wed, 22 Nov 2017 13:54:38 +0100 Subject: [PATCH] async_hooks: rename initTriggerId rename initTriggerId to defaultTriggerAsyncId such it matches the rest of our naming. 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/_http_client.js | 2 +- lib/async_hooks.js | 6 +++--- lib/dgram.js | 4 ++-- lib/internal/async_hooks.js | 36 +++++++++++++++---------------- lib/internal/bootstrap_node.js | 8 +++---- lib/internal/process/next_tick.js | 6 +++--- lib/net.js | 10 ++++----- lib/timers.js | 8 +++---- src/async_wrap.cc | 13 +++++------ src/connection_wrap.cc | 2 +- src/env-inl.h | 23 ++++++++++---------- src/env.h | 6 +++--- src/stream_base.cc | 8 +++---- src/tcp_wrap.cc | 4 ++-- src/udp_wrap.cc | 2 +- 15 files changed, 70 insertions(+), 68 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 09925b22791972..b7311639f80db3 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -579,7 +579,7 @@ function responseKeepAlive(res, req) { socket.removeListener('error', socketErrorListener); socket.once('error', freeSocketErrorListener); // There are cases where _handle === null. Avoid those. Passing null to - // nextTick() will call initTriggerId() to retrieve the id. + // nextTick() will call getDefaultTriggerAsyncId() to retrieve the id. const asyncId = socket._handle ? socket._handle.getAsyncId() : null; // Mark this socket as available, AFTER user-added end // handlers have a chance to run. diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 78dff7218c13ea..01b620208b65f3 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -19,7 +19,7 @@ const { disableHooks, // Sensitive Embedder API newUid, - initTriggerId, + getDefaultTriggerAsyncId, setInitTriggerId, emitInit, emitBefore, @@ -152,7 +152,7 @@ class AsyncResource { if (typeof opts === 'number') { opts = { triggerAsyncId: opts, requireManualDestroy: false }; } else if (opts.triggerAsyncId === undefined) { - opts.triggerAsyncId = initTriggerId(); + opts.triggerAsyncId = getDefaultTriggerAsyncId(); } // Unlike emitInitScript, AsyncResource doesn't supports null as the @@ -245,7 +245,7 @@ Object.defineProperty(module.exports, 'newUid', { Object.defineProperty(module.exports, 'initTriggerId', { get: internalUtil.deprecate(function() { - return initTriggerId; + return getDefaultTriggerAsyncId; }, 'async_hooks.initTriggerId is deprecated. ' + 'Use the AsyncResource default instead.', 'DEP0085') }); diff --git a/lib/dgram.js b/lib/dgram.js index bfd024bfee6c48..c7143b6876c2a5 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -28,7 +28,7 @@ const dns = require('dns'); const util = require('util'); const { isUint8Array } = require('internal/util/types'); const EventEmitter = require('events'); -const { setInitTriggerId } = require('internal/async_hooks'); +const { setDefaultTriggerAsyncId } = require('internal/async_hooks'); const { UV_UDP_REUSEADDR } = process.binding('constants').os; const { async_id_symbol } = process.binding('async_wrap'); const { nextTick } = require('internal/process/next_tick'); @@ -479,7 +479,7 @@ function doSend(ex, self, ip, list, address, port, callback) { // node::SendWrap isn't instantiated and attached to the JS instance of // SendWrap above until send() is called. So don't set the init trigger id // until now. - setInitTriggerId(self[async_id_symbol]); + setDefaultTriggerAsyncId(self[async_id_symbol]); var err = self._handle.send(req, list, list.length, diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 46cc1806b94a8d..ba08b5e3838468 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -14,10 +14,10 @@ const async_wrap = process.binding('async_wrap'); * kTriggerAsyncId: The trigger_async_id of the resource responsible for * the current execution stack. * kAsyncIdCounter: Incremental counter tracking the next assigned async_id. - * kInitTriggerAsyncId: Written immediately before a resource's constructor + * kDefaultTriggerAsyncId: Written immediately before a resource's constructor * that sets the value of the init()'s triggerAsyncId. The order of * retrieving the triggerAsyncId value is passing directly to the - * constructor -> value set in kInitTriggerAsyncId -> executionAsyncId of + * constructor -> value set in kDefaultTriggerAsyncId -> executionAsyncId of * the current resource. */ const { async_hook_fields, async_id_fields } = async_wrap; @@ -61,7 +61,7 @@ const active_hooks = { // for a given step, that step can bail out early. const { kInit, kBefore, kAfter, kDestroy, kPromiseResolve, kCheck, kExecutionAsyncId, kAsyncIdCounter, - kInitTriggerAsyncId } = async_wrap.constants; + kDefaultTriggerAsyncId } = async_wrap.constants; // Used in AsyncHook and AsyncResource. const init_symbol = Symbol('init'); @@ -242,25 +242,25 @@ function newUid() { return ++async_id_fields[kAsyncIdCounter]; } - // Return the triggerAsyncId meant for the constructor calling it. It's up to // the user to safeguard this call and make sure it's zero'd out when the // constructor is complete. -function initTriggerId() { - var triggerAsyncId = async_id_fields[kInitTriggerAsyncId]; +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[kInitTriggerAsyncId] = 0; - if (triggerAsyncId <= 0) - triggerAsyncId = async_id_fields[kExecutionAsyncId]; - return triggerAsyncId; + async_id_fields[kDefaultTriggerAsyncId] = 0; + // If defaultTriggerAsyncId isn't set, use the executionAsyncId + if (defaultTriggerAsyncId <= 0) + defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId]; + return defaultTriggerAsyncId; } -function setInitTriggerId(triggerAsyncId) { +function setDefaultTriggerAsyncId(triggerAsyncId) { // CHECK(Number.isSafeInteger(triggerAsyncId)) // CHECK(triggerAsyncId > 0) - async_id_fields[kInitTriggerAsyncId] = triggerAsyncId; + async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId; } @@ -279,13 +279,13 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) { return; // This can run after the early return check b/c running this function - // manually means that the embedder must have used initTriggerId(). + // manually means that the embedder must have used getDefaultTriggerAsyncId(). if (triggerAsyncId === null) { - triggerAsyncId = initTriggerId(); + triggerAsyncId = getDefaultTriggerAsyncId(); } else { - // If a triggerAsyncId was passed, any kInitTriggerAsyncId still must be + // If a triggerAsyncId was passed, any kDefaultTriggerAsyncId still must be // null'd. - async_id_fields[kInitTriggerAsyncId] = 0; + async_id_fields[kDefaultTriggerAsyncId] = 0; } emitInitNative(asyncId, type, triggerAsyncId, resource); @@ -337,8 +337,8 @@ module.exports = { disableHooks, // Sensitive Embedder API newUid, - initTriggerId, - setInitTriggerId, + getDefaultTriggerAsyncId, + setDefaultTriggerAsyncId, emitInit: emitInitScript, emitBefore: emitBeforeScript, emitAfter: emitAfterScript, diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 72101cb6408cc9..8d86630c888e25 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -368,14 +368,14 @@ // Internal functions needed to manipulate the stack. const { clearAsyncIdStack, asyncIdStackSize } = async_wrap; const { kAfter, kExecutionAsyncId, - kInitTriggerAsyncId } = async_wrap.constants; + kDefaultTriggerAsyncId } = async_wrap.constants; process._fatalException = function(er) { var caught; - // It's possible that kInitTriggerAsyncId was set for a constructor call - // that threw and was never cleared. So clear it now. - async_id_fields[kInitTriggerAsyncId] = 0; + // 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; if (exceptionHandlerState.captureFn !== null) { exceptionHandlerState.captureFn(er); diff --git a/lib/internal/process/next_tick.js b/lib/internal/process/next_tick.js index 260aa70b431b86..bf7d0bc94dc4ce 100644 --- a/lib/internal/process/next_tick.js +++ b/lib/internal/process/next_tick.js @@ -48,7 +48,7 @@ function setupNextTick() { const promises = require('internal/process/promises'); const errors = require('internal/errors'); const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks); - const initTriggerId = async_hooks.initTriggerId; + const getDefaultTriggerAsyncId = async_hooks.getDefaultTriggerAsyncId; // Two arrays that share state between C++ and JS. const { async_hook_fields, async_id_fields } = async_wrap; // Used to change the state of the async id stack. @@ -210,7 +210,7 @@ function setupNextTick() { nextTickQueue.push(new TickObject(callback, args, ++async_id_fields[kAsyncIdCounter], - initTriggerId())); + getDefaultTriggerAsyncId())); } // `internalNextTick()` will not enqueue any callback when the process is @@ -237,7 +237,7 @@ function setupNextTick() { } if (triggerAsyncId === null) - triggerAsyncId = initTriggerId(); + triggerAsyncId = getDefaultTriggerAsyncId(); // In V8 6.2, moving tickInfo & async_id_fields[kAsyncIdCounter] into the // TickObject incurs a significant performance penalty in the // next-tick-breadth-args benchmark (revisit later) diff --git a/lib/net.js b/lib/net.js index 540de753641e88..785ec236bd2199 100644 --- a/lib/net.js +++ b/lib/net.js @@ -43,7 +43,7 @@ const { TCPConnectWrap } = process.binding('tcp_wrap'); const { PipeConnectWrap } = process.binding('pipe_wrap'); const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap'); const { async_id_symbol } = process.binding('async_wrap'); -const { newUid, setInitTriggerId } = require('internal/async_hooks'); +const { newUid, setDefaultTriggerAsyncId } = require('internal/async_hooks'); const { nextTick } = require('internal/process/next_tick'); const errors = require('internal/errors'); const dns = require('dns'); @@ -301,7 +301,7 @@ function onSocketFinish() { // node::ShutdownWrap isn't instantiated and attached to the JS instance of // ShutdownWrap above until shutdown() is called. So don't set the init // trigger id until now. - setInitTriggerId(this[async_id_symbol]); + setDefaultTriggerAsyncId(this[async_id_symbol]); var err = this._handle.shutdown(req); if (err) @@ -939,7 +939,7 @@ function internalConnect( // node::TCPConnectWrap isn't instantiated and attached to the JS instance // of TCPConnectWrap above until connect() is called. So don't set the init // trigger id until now. - setInitTriggerId(self[async_id_symbol]); + setDefaultTriggerAsyncId(self[async_id_symbol]); if (addressType === 4) err = self._handle.connect(req, address, port); else @@ -952,7 +952,7 @@ function internalConnect( // node::PipeConnectWrap isn't instantiated and attached to the JS instance // of PipeConnectWrap above until connect() is called. So don't set the // init trigger id until now. - setInitTriggerId(self[async_id_symbol]); + setDefaultTriggerAsyncId(self[async_id_symbol]); err = self._handle.connect(req, address, afterConnect); } @@ -1091,7 +1091,7 @@ function lookupAndConnect(self, options) { debug('connect: dns options', dnsopts); self._host = host; var lookup = options.lookup || dns.lookup; - setInitTriggerId(self[async_id_symbol]); + setDefaultTriggerAsyncId(self[async_id_symbol]); lookup(host, dnsopts, function emitLookup(err, ip, addressType) { self.emit('lookup', err, ip, addressType, host); diff --git a/lib/timers.js b/lib/timers.js index 1560215fe0ad5d..c2ce399234c124 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -34,7 +34,7 @@ const kOnTimeout = TimerWrap.kOnTimeout | 0; // Two arrays that share state between C++ and JS. const { async_hook_fields, async_id_fields } = async_wrap; const { - initTriggerId, + getDefaultTriggerAsyncId, // The needed emit*() functions. emitInit, emitBefore, @@ -181,7 +181,7 @@ function insert(item, unrefed) { if (!item[async_id_symbol] || item._destroyed) { item._destroyed = false; item[async_id_symbol] = ++async_id_fields[kAsyncIdCounter]; - item[trigger_async_id_symbol] = initTriggerId(); + item[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); if (async_hook_fields[kInit] > 0) { emitInit(item[async_id_symbol], 'Timeout', @@ -552,7 +552,7 @@ function Timeout(callback, after, args, isRepeat) { this._destroyed = false; this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter]; - this[trigger_async_id_symbol] = initTriggerId(); + this[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); if (async_hook_fields[kInit] > 0) { emitInit(this[async_id_symbol], 'Timeout', @@ -769,7 +769,7 @@ function Immediate(callback, args) { this._destroyed = false; this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter]; - this[trigger_async_id_symbol] = initTriggerId(); + this[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); if (async_hook_fields[kInit] > 0) { emitInit(this[async_id_symbol], 'Immediate', diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 236c5670472980..875d6d81cfda98 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -310,7 +310,7 @@ static void PromiseHook(PromiseHookType type, Local promise, } // get id from parentWrap double trigger_async_id = parent_wrap->get_async_id(); - env->set_init_trigger_async_id(trigger_async_id); + env->set_default_trigger_async_id(trigger_async_id); } wrap = PromiseWrap::New(env, promise, parent_wrap, silent); @@ -541,9 +541,10 @@ void AsyncWrap::Initialize(Local target, // // kAsyncUid: Maintains the state of the next unique id to be assigned. // - // kInitTriggerAsyncId: Write the id of the resource responsible for a + // kDefaultTriggerAsyncId: Write the id of the resource responsible for a // handle's creation just before calling the new handle's constructor. - // After the new handle is constructed kInitTriggerAsyncId is set back to 0. + // After the new handle is constructed kDefaultTriggerAsyncId is set back + // to 0. FORCE_SET_TARGET_FIELD(target, "async_id_fields", env->async_hooks()->async_id_fields().GetJSArray()); @@ -563,7 +564,7 @@ void AsyncWrap::Initialize(Local target, SET_HOOKS_CONSTANT(kExecutionAsyncId); SET_HOOKS_CONSTANT(kTriggerAsyncId); SET_HOOKS_CONSTANT(kAsyncIdCounter); - SET_HOOKS_CONSTANT(kInitTriggerAsyncId); + SET_HOOKS_CONSTANT(kDefaultTriggerAsyncId); #undef SET_HOOKS_CONSTANT FORCE_SET_TARGET_FIELD(target, "constants", constants); @@ -676,7 +677,7 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) { void AsyncWrap::AsyncReset(double execution_async_id, bool silent) { async_id_ = execution_async_id == -1 ? env()->new_async_id() : execution_async_id; - trigger_async_id_ = env()->get_init_trigger_async_id(); + trigger_async_id_ = env()->get_default_trigger_async_id(); switch (provider_type()) { #define V(PROVIDER) \ @@ -777,7 +778,7 @@ async_context EmitAsyncInit(Isolate* isolate, // Initialize async context struct if (trigger_async_id == -1) - trigger_async_id = env->get_init_trigger_async_id(); + trigger_async_id = env->get_default_trigger_async_id(); async_context context = { env->new_async_id(), // async_id_ diff --git a/src/connection_wrap.cc b/src/connection_wrap.cc index d82e7195d76579..b7c1949e11e404 100644 --- a/src/connection_wrap.cc +++ b/src/connection_wrap.cc @@ -49,7 +49,7 @@ void ConnectionWrap::OnConnection(uv_stream_t* handle, }; if (status == 0) { - env->set_init_trigger_async_id(wrap_data->get_async_id()); + env->set_default_trigger_async_id(wrap_data->get_async_id()); // Instantiate the client javascript object and handle. Local client_obj = WrapType::Instantiate(env, wrap_data, diff --git a/src/env-inl.h b/src/env-inl.h index 955cf2688f88fa..2981f0f17eb1e5 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -440,17 +440,18 @@ inline double Environment::trigger_async_id() { return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId]; } -inline double Environment::get_init_trigger_async_id() { - AliasedBuffer& async_id_fields = - async_hooks()->async_id_fields(); - double tid = async_id_fields[AsyncHooks::kInitTriggerAsyncId]; - async_id_fields[AsyncHooks::kInitTriggerAsyncId] = 0; - if (tid <= 0) tid = execution_async_id(); - return tid; -} - -inline void Environment::set_init_trigger_async_id(const double id) { - async_hooks()->async_id_fields()[AsyncHooks::kInitTriggerAsyncId] = 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; + // If defaultTriggerAsyncId isn't set, use the executionAsyncId + if (default_trigger_async_id <= 0) + default_trigger_async_id = execution_async_id(); + return default_trigger_async_id; +} + +inline void Environment::set_default_trigger_async_id(const double id) { + async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = id; } inline double* Environment::heap_statistics_buffer() const { diff --git a/src/env.h b/src/env.h index 79ef42a5c0159e..23cfeae39d0cac 100644 --- a/src/env.h +++ b/src/env.h @@ -389,7 +389,7 @@ class Environment { kExecutionAsyncId, kTriggerAsyncId, kAsyncIdCounter, - kInitTriggerAsyncId, + kDefaultTriggerAsyncId, kUidFieldsCount, }; @@ -565,8 +565,8 @@ class Environment { inline double new_async_id(); inline double execution_async_id(); inline double trigger_async_id(); - inline double get_init_trigger_async_id(); - inline void set_init_trigger_async_id(const double id); + inline double get_default_trigger_async_id(); + inline void set_default_trigger_async_id(const double id); // List of id's that have been destroyed and need the destroy() cb called. inline std::vector* destroy_async_id_list(); diff --git a/src/stream_base.cc b/src/stream_base.cc index bb25fc1cff0e9c..021f5417c12687 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -52,7 +52,7 @@ int StreamBase::Shutdown(const FunctionCallbackInfo& args) { AsyncWrap* wrap = GetAsyncWrap(); CHECK_NE(wrap, nullptr); - env->set_init_trigger_async_id(wrap->get_async_id()); + env->set_default_trigger_async_id(wrap->get_async_id()); ShutdownWrap* req_wrap = new ShutdownWrap(env, req_wrap_obj, this); @@ -155,7 +155,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { wrap = GetAsyncWrap(); CHECK_NE(wrap, nullptr); - env->set_init_trigger_async_id(wrap->get_async_id()); + env->set_default_trigger_async_id(wrap->get_async_id()); req_wrap = WriteWrap::New(env, req_wrap_obj, this, storage_size); offset = 0; @@ -244,7 +244,7 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo& args) { wrap = GetAsyncWrap(); if (wrap != nullptr) - env->set_init_trigger_async_id(wrap->get_async_id()); + env->set_default_trigger_async_id(wrap->get_async_id()); // Allocate, or write rest req_wrap = WriteWrap::New(env, req_wrap_obj, this); @@ -329,7 +329,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo& args) { wrap = GetAsyncWrap(); if (wrap != nullptr) - env->set_init_trigger_async_id(wrap->get_async_id()); + env->set_default_trigger_async_id(wrap->get_async_id()); req_wrap = WriteWrap::New(env, req_wrap_obj, this, storage_size); data = req_wrap->Extra(); diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 8dd14e2e16c18b..00853df9baf5c8 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -293,7 +293,7 @@ void TCPWrap::Connect(const FunctionCallbackInfo& args) { int err = uv_ip4_addr(*ip_address, port, &addr); if (err == 0) { - env->set_init_trigger_async_id(wrap->get_async_id()); + env->set_default_trigger_async_id(wrap->get_async_id()); ConnectWrap* req_wrap = new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP); err = uv_tcp_connect(req_wrap->req(), @@ -329,7 +329,7 @@ void TCPWrap::Connect6(const FunctionCallbackInfo& args) { int err = uv_ip6_addr(*ip_address, port, &addr); if (err == 0) { - env->set_init_trigger_async_id(wrap->get_async_id()); + env->set_default_trigger_async_id(wrap->get_async_id()); ConnectWrap* req_wrap = new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP); err = uv_tcp_connect(req_wrap->req(), diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index ff6dd73041ee80..49d32a49fe564a 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -353,7 +353,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo& args, int family) { node::Utf8Value address(env->isolate(), args[4]); const bool have_callback = args[5]->IsTrue(); - env->set_init_trigger_async_id(wrap->get_async_id()); + env->set_default_trigger_async_id(wrap->get_async_id()); SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback); size_t msg_size = 0;