async_hooks: clean up usage in internal code
Instead of exposing internals of async_hooks & async_wrap throughout the code base, create necessary helper methods within the internal async_hooks that allows easy usage by Node.js internals. This stops every single internal user of async_hooks from importing a ton of functions, constants and internal Aliased Buffers from C++ async_wrap. Adds functions initHooksExist, afterHooksExist, and destroyHooksExist to determine whether the related emit methods need to be triggered. Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side as an alternative to always calling C++. Moves async_id_symbol and trigger_async_id_symbol to internal async_hooks as they are never used in C++. Renames newUid to newAsyncId for added clarity of its purpose. Adjusts usage throughout the codebase, as well as in a couple of tests. PR-URL: https://github.com/nodejs/node/pull/18720 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
7748865cd3
commit
e9ac80bb39
@ -25,7 +25,7 @@ const net = require('net');
|
|||||||
const util = require('util');
|
const util = require('util');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const debug = util.debuglog('http');
|
const debug = util.debuglog('http');
|
||||||
const { async_id_symbol } = process.binding('async_wrap');
|
const { async_id_symbol } = require('internal/async_hooks').symbols;
|
||||||
const { nextTick } = require('internal/process/next_tick');
|
const { nextTick } = require('internal/process/next_tick');
|
||||||
|
|
||||||
// New Agent code.
|
// New Agent code.
|
||||||
|
@ -31,7 +31,7 @@ const common = require('_http_common');
|
|||||||
const checkIsHttpToken = common._checkIsHttpToken;
|
const checkIsHttpToken = common._checkIsHttpToken;
|
||||||
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
|
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
|
||||||
const { outHeadersKey } = require('internal/http');
|
const { outHeadersKey } = require('internal/http');
|
||||||
const { async_id_symbol } = process.binding('async_wrap');
|
const { async_id_symbol } = require('internal/async_hooks').symbols;
|
||||||
const { nextTick } = require('internal/process/next_tick');
|
const { nextTick } = require('internal/process/next_tick');
|
||||||
const errors = require('internal/errors');
|
const errors = require('internal/errors');
|
||||||
|
|
||||||
|
@ -9,12 +9,14 @@ const internal_async_hooks = require('internal/async_hooks');
|
|||||||
// resource gets gced.
|
// resource gets gced.
|
||||||
const { registerDestroyHook } = async_wrap;
|
const { registerDestroyHook } = async_wrap;
|
||||||
const {
|
const {
|
||||||
|
executionAsyncId,
|
||||||
|
triggerAsyncId,
|
||||||
// Private API
|
// Private API
|
||||||
getHookArrays,
|
getHookArrays,
|
||||||
enableHooks,
|
enableHooks,
|
||||||
disableHooks,
|
disableHooks,
|
||||||
// Internal Embedder API
|
// Internal Embedder API
|
||||||
newUid,
|
newAsyncId,
|
||||||
getDefaultTriggerAsyncId,
|
getDefaultTriggerAsyncId,
|
||||||
emitInit,
|
emitInit,
|
||||||
emitBefore,
|
emitBefore,
|
||||||
@ -22,21 +24,16 @@ const {
|
|||||||
emitDestroy,
|
emitDestroy,
|
||||||
} = internal_async_hooks;
|
} = internal_async_hooks;
|
||||||
|
|
||||||
// Get fields
|
|
||||||
const { async_id_fields } = async_wrap;
|
|
||||||
|
|
||||||
// Get symbols
|
// Get symbols
|
||||||
const {
|
const {
|
||||||
|
async_id_symbol, trigger_async_id_symbol,
|
||||||
init_symbol, before_symbol, after_symbol, destroy_symbol,
|
init_symbol, before_symbol, after_symbol, destroy_symbol,
|
||||||
promise_resolve_symbol
|
promise_resolve_symbol
|
||||||
} = internal_async_hooks.symbols;
|
} = internal_async_hooks.symbols;
|
||||||
|
|
||||||
const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
|
|
||||||
|
|
||||||
// Get constants
|
// Get constants
|
||||||
const {
|
const {
|
||||||
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
|
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
|
||||||
kExecutionAsyncId, kTriggerAsyncId
|
|
||||||
} = async_wrap.constants;
|
} = async_wrap.constants;
|
||||||
|
|
||||||
// Listener API //
|
// Listener API //
|
||||||
@ -125,16 +122,6 @@ function createHook(fns) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function executionAsyncId() {
|
|
||||||
return async_id_fields[kExecutionAsyncId];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function triggerAsyncId() {
|
|
||||||
return async_id_fields[kTriggerAsyncId];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Embedder API //
|
// Embedder API //
|
||||||
|
|
||||||
const destroyedSymbol = Symbol('destroyed');
|
const destroyedSymbol = Symbol('destroyed');
|
||||||
@ -170,7 +157,7 @@ class AsyncResource {
|
|||||||
triggerAsyncId);
|
triggerAsyncId);
|
||||||
}
|
}
|
||||||
|
|
||||||
this[async_id_symbol] = newUid();
|
this[async_id_symbol] = newAsyncId();
|
||||||
this[trigger_async_id_symbol] = triggerAsyncId;
|
this[trigger_async_id_symbol] = triggerAsyncId;
|
||||||
// this prop name (destroyed) has to be synchronized with C++
|
// this prop name (destroyed) has to be synchronized with C++
|
||||||
this[destroyedSymbol] = { destroyed: false };
|
this[destroyedSymbol] = { destroyed: false };
|
||||||
|
@ -28,9 +28,11 @@ const dns = require('dns');
|
|||||||
const util = require('util');
|
const util = require('util');
|
||||||
const { isUint8Array } = require('internal/util/types');
|
const { isUint8Array } = require('internal/util/types');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
|
const {
|
||||||
|
defaultTriggerAsyncIdScope,
|
||||||
|
symbols: { async_id_symbol }
|
||||||
|
} = require('internal/async_hooks');
|
||||||
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
|
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
|
||||||
const { async_id_symbol } = process.binding('async_wrap');
|
|
||||||
const { nextTick } = require('internal/process/next_tick');
|
const { nextTick } = require('internal/process/next_tick');
|
||||||
|
|
||||||
const { UDP, SendWrap } = process.binding('udp_wrap');
|
const { UDP, SendWrap } = process.binding('udp_wrap');
|
||||||
|
@ -27,7 +27,7 @@ const async_wrap = process.binding('async_wrap');
|
|||||||
* It has a fixed size, so if that is exceeded, calls to the native
|
* It has a fixed size, so if that is exceeded, calls to the native
|
||||||
* side are used instead in pushAsyncIds() and popAsyncIds().
|
* side are used instead in pushAsyncIds() and popAsyncIds().
|
||||||
*/
|
*/
|
||||||
const { async_id_symbol, async_hook_fields, async_id_fields } = async_wrap;
|
const { async_hook_fields, async_id_fields } = async_wrap;
|
||||||
// Store the pair executionAsyncId and triggerAsyncId in a std::stack on
|
// Store the pair executionAsyncId and triggerAsyncId in a std::stack on
|
||||||
// Environment::AsyncHooks::async_ids_stack_ tracks the resource responsible for
|
// Environment::AsyncHooks::async_ids_stack_ tracks the resource responsible for
|
||||||
// the current execution stack. This is unwound as each resource exits. In the
|
// the current execution stack. This is unwound as each resource exits. In the
|
||||||
@ -71,6 +71,8 @@ const { kInit, kBefore, kAfter, kDestroy, kPromiseResolve,
|
|||||||
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
|
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
|
||||||
|
|
||||||
// Used in AsyncHook and AsyncResource.
|
// Used in AsyncHook and AsyncResource.
|
||||||
|
const async_id_symbol = Symbol('asyncId');
|
||||||
|
const trigger_async_id_symbol = Symbol('triggerAsyncId');
|
||||||
const init_symbol = Symbol('init');
|
const init_symbol = Symbol('init');
|
||||||
const before_symbol = Symbol('before');
|
const before_symbol = Symbol('before');
|
||||||
const after_symbol = Symbol('after');
|
const after_symbol = Symbol('after');
|
||||||
@ -245,7 +247,7 @@ function disableHooks() {
|
|||||||
// Increment the internal id counter and return the value. Important that the
|
// Increment the internal id counter and return the value. Important that the
|
||||||
// counter increment first. Since it's done the same way in
|
// counter increment first. Since it's done the same way in
|
||||||
// Environment::new_async_uid()
|
// Environment::new_async_uid()
|
||||||
function newUid() {
|
function newAsyncId() {
|
||||||
return ++async_id_fields[kAsyncIdCounter];
|
return ++async_id_fields[kAsyncIdCounter];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -254,7 +256,7 @@ function getOrSetAsyncId(object) {
|
|||||||
return object[async_id_symbol];
|
return object[async_id_symbol];
|
||||||
}
|
}
|
||||||
|
|
||||||
return object[async_id_symbol] = newUid();
|
return object[async_id_symbol] = newAsyncId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -270,6 +272,11 @@ function getDefaultTriggerAsyncId() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function clearDefaultTriggerAsyncId() {
|
||||||
|
async_id_fields[kDefaultTriggerAsyncId] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
|
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
|
||||||
// CHECK(Number.isSafeInteger(triggerAsyncId))
|
// CHECK(Number.isSafeInteger(triggerAsyncId))
|
||||||
// CHECK(triggerAsyncId > 0)
|
// CHECK(triggerAsyncId > 0)
|
||||||
@ -287,6 +294,19 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function initHooksExist() {
|
||||||
|
return async_hook_fields[kInit] > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function afterHooksExist() {
|
||||||
|
return async_hook_fields[kAfter] > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function destroyHooksExist() {
|
||||||
|
return async_hook_fields[kDestroy] > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function emitInitScript(asyncId, type, triggerAsyncId, resource) {
|
function emitInitScript(asyncId, type, triggerAsyncId, resource) {
|
||||||
validateAsyncId(asyncId, 'asyncId');
|
validateAsyncId(asyncId, 'asyncId');
|
||||||
if (triggerAsyncId !== null)
|
if (triggerAsyncId !== null)
|
||||||
@ -345,6 +365,20 @@ function emitDestroyScript(asyncId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Keep in sync with Environment::AsyncHooks::clear_async_id_stack
|
||||||
|
// in src/env-inl.h.
|
||||||
|
function clearAsyncIdStack() {
|
||||||
|
async_id_fields[kExecutionAsyncId] = 0;
|
||||||
|
async_id_fields[kTriggerAsyncId] = 0;
|
||||||
|
async_hook_fields[kStackLength] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function hasAsyncIdStack() {
|
||||||
|
return async_hook_fields[kStackLength] > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// This is the equivalent of the native push_async_ids() call.
|
// This is the equivalent of the native push_async_ids() call.
|
||||||
function pushAsyncIds(asyncId, triggerAsyncId) {
|
function pushAsyncIds(asyncId, triggerAsyncId) {
|
||||||
const offset = async_hook_fields[kStackLength];
|
const offset = async_hook_fields[kStackLength];
|
||||||
@ -377,20 +411,38 @@ function popAsyncIds(asyncId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function executionAsyncId() {
|
||||||
|
return async_id_fields[kExecutionAsyncId];
|
||||||
|
}
|
||||||
|
|
||||||
|
function triggerAsyncId() {
|
||||||
|
return async_id_fields[kTriggerAsyncId];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
executionAsyncId,
|
||||||
|
triggerAsyncId,
|
||||||
// Private API
|
// Private API
|
||||||
getHookArrays,
|
getHookArrays,
|
||||||
symbols: {
|
symbols: {
|
||||||
|
async_id_symbol, trigger_async_id_symbol,
|
||||||
init_symbol, before_symbol, after_symbol, destroy_symbol,
|
init_symbol, before_symbol, after_symbol, destroy_symbol,
|
||||||
promise_resolve_symbol
|
promise_resolve_symbol
|
||||||
},
|
},
|
||||||
enableHooks,
|
enableHooks,
|
||||||
disableHooks,
|
disableHooks,
|
||||||
|
clearDefaultTriggerAsyncId,
|
||||||
|
clearAsyncIdStack,
|
||||||
|
hasAsyncIdStack,
|
||||||
// Internal Embedder API
|
// Internal Embedder API
|
||||||
newUid,
|
newAsyncId,
|
||||||
getOrSetAsyncId,
|
getOrSetAsyncId,
|
||||||
getDefaultTriggerAsyncId,
|
getDefaultTriggerAsyncId,
|
||||||
defaultTriggerAsyncIdScope,
|
defaultTriggerAsyncIdScope,
|
||||||
|
initHooksExist,
|
||||||
|
afterHooksExist,
|
||||||
|
destroyHooksExist,
|
||||||
emitInit: emitInitScript,
|
emitInit: emitInitScript,
|
||||||
emitBefore: emitBeforeScript,
|
emitBefore: emitBeforeScript,
|
||||||
emitAfter: emitAfterScript,
|
emitAfter: emitAfterScript,
|
||||||
|
26
lib/internal/bootstrap_node.js
vendored
26
lib/internal/bootstrap_node.js
vendored
@ -431,18 +431,19 @@
|
|||||||
function noop() {}
|
function noop() {}
|
||||||
|
|
||||||
function setupProcessFatal() {
|
function setupProcessFatal() {
|
||||||
const async_wrap = process.binding('async_wrap');
|
const {
|
||||||
// Arrays containing hook flags and ids for async_hook calls.
|
executionAsyncId,
|
||||||
const { async_hook_fields, async_id_fields } = async_wrap;
|
clearDefaultTriggerAsyncId,
|
||||||
// Internal functions needed to manipulate the stack.
|
clearAsyncIdStack,
|
||||||
const { clearAsyncIdStack } = async_wrap;
|
hasAsyncIdStack,
|
||||||
const { kAfter, kExecutionAsyncId,
|
afterHooksExist,
|
||||||
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
|
emitAfter
|
||||||
|
} = NativeModule.require('internal/async_hooks');
|
||||||
|
|
||||||
process._fatalException = function(er) {
|
process._fatalException = function(er) {
|
||||||
// It's possible that kDefaultTriggerAsyncId was set for a constructor
|
// It's possible that defaultTriggerAsyncId was set for a constructor
|
||||||
// call that threw and was never cleared. So clear it now.
|
// call that threw and was never cleared. So clear it now.
|
||||||
async_id_fields[kDefaultTriggerAsyncId] = -1;
|
clearDefaultTriggerAsyncId();
|
||||||
|
|
||||||
if (exceptionHandlerState.captureFn !== null) {
|
if (exceptionHandlerState.captureFn !== null) {
|
||||||
exceptionHandlerState.captureFn(er);
|
exceptionHandlerState.captureFn(er);
|
||||||
@ -465,11 +466,10 @@
|
|||||||
NativeModule.require('timers').setImmediate(noop);
|
NativeModule.require('timers').setImmediate(noop);
|
||||||
|
|
||||||
// Emit the after() hooks now that the exception has been handled.
|
// Emit the after() hooks now that the exception has been handled.
|
||||||
if (async_hook_fields[kAfter] > 0) {
|
if (afterHooksExist()) {
|
||||||
const { emitAfter } = NativeModule.require('internal/async_hooks');
|
|
||||||
do {
|
do {
|
||||||
emitAfter(async_id_fields[kExecutionAsyncId]);
|
emitAfter(executionAsyncId());
|
||||||
} while (async_hook_fields[kStackLength] > 0);
|
} while (hasAsyncIdStack());
|
||||||
// Or completely empty the id stack.
|
// Or completely empty the id stack.
|
||||||
} else {
|
} else {
|
||||||
clearAsyncIdStack();
|
clearAsyncIdStack();
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
require('internal/util').assertCrypto();
|
require('internal/util').assertCrypto();
|
||||||
|
|
||||||
const { async_id_symbol } = process.binding('async_wrap');
|
const { async_id_symbol } = require('internal/async_hooks').symbols;
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const binding = process.binding('http2');
|
const binding = process.binding('http2');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
@ -5,19 +5,20 @@ exports.setup = setupNextTick;
|
|||||||
exports.nextTick = null;
|
exports.nextTick = null;
|
||||||
|
|
||||||
function setupNextTick() {
|
function setupNextTick() {
|
||||||
const async_wrap = process.binding('async_wrap');
|
const {
|
||||||
const async_hooks = require('internal/async_hooks');
|
getDefaultTriggerAsyncId,
|
||||||
|
newAsyncId,
|
||||||
|
initHooksExist,
|
||||||
|
destroyHooksExist,
|
||||||
|
emitInit,
|
||||||
|
emitBefore,
|
||||||
|
emitAfter,
|
||||||
|
emitDestroy,
|
||||||
|
symbols: { async_id_symbol, trigger_async_id_symbol }
|
||||||
|
} = require('internal/async_hooks');
|
||||||
const promises = require('internal/process/promises');
|
const promises = require('internal/process/promises');
|
||||||
const errors = require('internal/errors');
|
const errors = require('internal/errors');
|
||||||
const { emitPromiseRejectionWarnings } = promises;
|
const { emitPromiseRejectionWarnings } = promises;
|
||||||
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.
|
|
||||||
const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
|
|
||||||
// Grab the constants necessary for working with internal arrays.
|
|
||||||
const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
|
|
||||||
const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
|
|
||||||
|
|
||||||
// tickInfo is used so that the C++ code in src/node.cc can
|
// tickInfo is used so that the C++ code in src/node.cc can
|
||||||
// have easy access to our nextTick state, and avoid unnecessary
|
// have easy access to our nextTick state, and avoid unnecessary
|
||||||
@ -104,7 +105,7 @@ function setupNextTick() {
|
|||||||
// that nextTick() doesn't allow the event loop to proceed, but if
|
// that nextTick() doesn't allow the event loop to proceed, but if
|
||||||
// any async hooks are enabled during the callback's execution then
|
// any async hooks are enabled during the callback's execution then
|
||||||
// this tock's after hook will be called, but not its destroy hook.
|
// this tock's after hook will be called, but not its destroy hook.
|
||||||
if (async_hook_fields[kDestroy] > 0)
|
if (destroyHooksExist())
|
||||||
emitDestroy(asyncId);
|
emitDestroy(asyncId);
|
||||||
|
|
||||||
const callback = tock.callback;
|
const callback = tock.callback;
|
||||||
@ -128,11 +129,11 @@ function setupNextTick() {
|
|||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
this.args = args;
|
this.args = args;
|
||||||
|
|
||||||
const asyncId = ++async_id_fields[kAsyncIdCounter];
|
const asyncId = newAsyncId();
|
||||||
this[async_id_symbol] = asyncId;
|
this[async_id_symbol] = asyncId;
|
||||||
this[trigger_async_id_symbol] = triggerAsyncId;
|
this[trigger_async_id_symbol] = triggerAsyncId;
|
||||||
|
|
||||||
if (async_hook_fields[kInit] > 0) {
|
if (initHooksExist()) {
|
||||||
emitInit(asyncId,
|
emitInit(asyncId,
|
||||||
'TickObject',
|
'TickObject',
|
||||||
triggerAsyncId,
|
triggerAsyncId,
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const async_wrap = process.binding('async_wrap');
|
|
||||||
// Two arrays that share state between C++ and JS.
|
|
||||||
const { async_hook_fields, async_id_fields } = async_wrap;
|
|
||||||
const {
|
const {
|
||||||
getDefaultTriggerAsyncId,
|
getDefaultTriggerAsyncId,
|
||||||
// The needed emit*() functions.
|
newAsyncId,
|
||||||
|
initHooksExist,
|
||||||
emitInit
|
emitInit
|
||||||
} = require('internal/async_hooks');
|
} = require('internal/async_hooks');
|
||||||
// Grab the constants necessary for working with internal arrays.
|
|
||||||
const { kInit, kAsyncIdCounter } = async_wrap.constants;
|
|
||||||
// Symbols for storing async id state.
|
// Symbols for storing async id state.
|
||||||
const async_id_symbol = Symbol('asyncId');
|
const async_id_symbol = Symbol('asyncId');
|
||||||
const trigger_async_id_symbol = Symbol('triggerId');
|
const trigger_async_id_symbol = Symbol('triggerId');
|
||||||
@ -70,9 +66,9 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {
|
|||||||
|
|
||||||
this[unrefedSymbol] = isUnrefed;
|
this[unrefedSymbol] = isUnrefed;
|
||||||
|
|
||||||
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
|
this[async_id_symbol] = newAsyncId();
|
||||||
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
|
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
|
||||||
if (async_hook_fields[kInit] > 0) {
|
if (initHooksExist()) {
|
||||||
emitInit(this[async_id_symbol],
|
emitInit(this[async_id_symbol],
|
||||||
'Timeout',
|
'Timeout',
|
||||||
this[trigger_async_id_symbol],
|
this[trigger_async_id_symbol],
|
||||||
|
@ -47,8 +47,11 @@ const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
|
|||||||
const { TCPConnectWrap } = process.binding('tcp_wrap');
|
const { TCPConnectWrap } = process.binding('tcp_wrap');
|
||||||
const { PipeConnectWrap } = process.binding('pipe_wrap');
|
const { PipeConnectWrap } = process.binding('pipe_wrap');
|
||||||
const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
|
const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
|
||||||
const { async_id_symbol } = process.binding('async_wrap');
|
const {
|
||||||
const { newUid, defaultTriggerAsyncIdScope } = require('internal/async_hooks');
|
newAsyncId,
|
||||||
|
defaultTriggerAsyncIdScope,
|
||||||
|
symbols: { async_id_symbol }
|
||||||
|
} = require('internal/async_hooks');
|
||||||
const { nextTick } = require('internal/process/next_tick');
|
const { nextTick } = require('internal/process/next_tick');
|
||||||
const errors = require('internal/errors');
|
const errors = require('internal/errors');
|
||||||
const dns = require('dns');
|
const dns = require('dns');
|
||||||
@ -91,7 +94,7 @@ function createHandle(fd, is_server) {
|
|||||||
|
|
||||||
function getNewAsyncId(handle) {
|
function getNewAsyncId(handle) {
|
||||||
return (!handle || typeof handle.getAsyncId !== 'function') ?
|
return (!handle || typeof handle.getAsyncId !== 'function') ?
|
||||||
newUid() : handle.getAsyncId();
|
newAsyncId() : handle.getAsyncId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,34 +21,34 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const async_wrap = process.binding('async_wrap');
|
|
||||||
const {
|
const {
|
||||||
Timer: TimerWrap,
|
Timer: TimerWrap,
|
||||||
setupTimers,
|
setupTimers,
|
||||||
} = process.binding('timer_wrap');
|
} = process.binding('timer_wrap');
|
||||||
const L = require('internal/linkedlist');
|
const L = require('internal/linkedlist');
|
||||||
const timerInternals = require('internal/timers');
|
const {
|
||||||
|
async_id_symbol,
|
||||||
|
trigger_async_id_symbol,
|
||||||
|
Timeout,
|
||||||
|
validateTimerDuration
|
||||||
|
} = require('internal/timers');
|
||||||
const internalUtil = require('internal/util');
|
const internalUtil = require('internal/util');
|
||||||
const { createPromise, promiseResolve } = process.binding('util');
|
const { createPromise, promiseResolve } = process.binding('util');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const errors = require('internal/errors');
|
const errors = require('internal/errors');
|
||||||
const debug = util.debuglog('timer');
|
const debug = util.debuglog('timer');
|
||||||
// Two arrays that share state between C++ and JS.
|
|
||||||
const { async_hook_fields, async_id_fields } = async_wrap;
|
|
||||||
const {
|
const {
|
||||||
getDefaultTriggerAsyncId,
|
getDefaultTriggerAsyncId,
|
||||||
|
newAsyncId,
|
||||||
|
initHooksExist,
|
||||||
|
destroyHooksExist,
|
||||||
// The needed emit*() functions.
|
// The needed emit*() functions.
|
||||||
emitInit,
|
emitInit,
|
||||||
emitBefore,
|
emitBefore,
|
||||||
emitAfter,
|
emitAfter,
|
||||||
emitDestroy
|
emitDestroy
|
||||||
} = require('internal/async_hooks');
|
} = require('internal/async_hooks');
|
||||||
// Grab the constants necessary for working with internal arrays.
|
|
||||||
const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
|
|
||||||
// Symbols for storing async id state.
|
|
||||||
const async_id_symbol = timerInternals.async_id_symbol;
|
|
||||||
const trigger_async_id_symbol = timerInternals.trigger_async_id_symbol;
|
|
||||||
|
|
||||||
// *Must* match Environment::ImmediateInfo::Fields in src/env.h.
|
// *Must* match Environment::ImmediateInfo::Fields in src/env.h.
|
||||||
const kCount = 0;
|
const kCount = 0;
|
||||||
@ -60,10 +60,6 @@ const [immediateInfo, toggleImmediateRef] =
|
|||||||
|
|
||||||
const kRefed = Symbol('refed');
|
const kRefed = Symbol('refed');
|
||||||
|
|
||||||
// The Timeout class
|
|
||||||
const Timeout = timerInternals.Timeout;
|
|
||||||
|
|
||||||
|
|
||||||
// HOW and WHY the timers implementation works the way it does.
|
// HOW and WHY the timers implementation works the way it does.
|
||||||
//
|
//
|
||||||
// Timers are crucial to Node.js. Internally, any TCP I/O connection creates a
|
// Timers are crucial to Node.js. Internally, any TCP I/O connection creates a
|
||||||
@ -192,9 +188,9 @@ function insert(item, unrefed, start) {
|
|||||||
|
|
||||||
if (!item[async_id_symbol] || item._destroyed) {
|
if (!item[async_id_symbol] || item._destroyed) {
|
||||||
item._destroyed = false;
|
item._destroyed = false;
|
||||||
item[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
|
item[async_id_symbol] = newAsyncId();
|
||||||
item[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
|
item[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
|
||||||
if (async_hook_fields[kInit] > 0) {
|
if (initHooksExist()) {
|
||||||
emitInit(item[async_id_symbol],
|
emitInit(item[async_id_symbol],
|
||||||
'Timeout',
|
'Timeout',
|
||||||
item[trigger_async_id_symbol],
|
item[trigger_async_id_symbol],
|
||||||
@ -255,7 +251,7 @@ function listOnTimeout(handle, now) {
|
|||||||
assert(timer !== L.peek(list));
|
assert(timer !== L.peek(list));
|
||||||
|
|
||||||
if (!timer._onTimeout) {
|
if (!timer._onTimeout) {
|
||||||
if (async_hook_fields[kDestroy] > 0 && !timer._destroyed &&
|
if (destroyHooksExist() && !timer._destroyed &&
|
||||||
typeof timer[async_id_symbol] === 'number') {
|
typeof timer[async_id_symbol] === 'number') {
|
||||||
emitDestroy(timer[async_id_symbol]);
|
emitDestroy(timer[async_id_symbol]);
|
||||||
timer._destroyed = true;
|
timer._destroyed = true;
|
||||||
@ -306,7 +302,7 @@ function tryOnTimeout(timer, start) {
|
|||||||
if (timerAsyncId !== null) {
|
if (timerAsyncId !== null) {
|
||||||
if (!threw)
|
if (!threw)
|
||||||
emitAfter(timerAsyncId);
|
emitAfter(timerAsyncId);
|
||||||
if (!timer._repeat && async_hook_fields[kDestroy] > 0 &&
|
if (!timer._repeat && destroyHooksExist() &&
|
||||||
!timer._destroyed) {
|
!timer._destroyed) {
|
||||||
emitDestroy(timerAsyncId);
|
emitDestroy(timerAsyncId);
|
||||||
timer._destroyed = true;
|
timer._destroyed = true;
|
||||||
@ -341,8 +337,7 @@ function reuse(item) {
|
|||||||
// Remove a timer. Cancels the timeout and resets the relevant timer properties.
|
// Remove a timer. Cancels the timeout and resets the relevant timer properties.
|
||||||
function unenroll(item) {
|
function unenroll(item) {
|
||||||
// Fewer checks may be possible, but these cover everything.
|
// Fewer checks may be possible, but these cover everything.
|
||||||
if (async_hook_fields[kDestroy] > 0 &&
|
if (destroyHooksExist() &&
|
||||||
item &&
|
|
||||||
typeof item[async_id_symbol] === 'number' &&
|
typeof item[async_id_symbol] === 'number' &&
|
||||||
!item._destroyed) {
|
!item._destroyed) {
|
||||||
emitDestroy(item[async_id_symbol]);
|
emitDestroy(item[async_id_symbol]);
|
||||||
@ -368,7 +363,7 @@ exports.unenroll = util.deprecate(unenroll,
|
|||||||
// This function does not start the timer, see `active()`.
|
// This function does not start the timer, see `active()`.
|
||||||
// Using existing objects as timers slightly reduces object overhead.
|
// Using existing objects as timers slightly reduces object overhead.
|
||||||
function enroll(item, msecs) {
|
function enroll(item, msecs) {
|
||||||
item._idleTimeout = timerInternals.validateTimerDuration(msecs);
|
item._idleTimeout = validateTimerDuration(msecs);
|
||||||
|
|
||||||
// if this item was already in a list somewhere
|
// if this item was already in a list somewhere
|
||||||
// then we should unenroll it from that
|
// then we should unenroll it from that
|
||||||
@ -574,7 +569,7 @@ Timeout.prototype.ref = function() {
|
|||||||
Timeout.prototype.close = function() {
|
Timeout.prototype.close = function() {
|
||||||
this._onTimeout = null;
|
this._onTimeout = null;
|
||||||
if (this._handle) {
|
if (this._handle) {
|
||||||
if (async_hook_fields[kDestroy] > 0 &&
|
if (destroyHooksExist() &&
|
||||||
typeof this[async_id_symbol] === 'number' &&
|
typeof this[async_id_symbol] === 'number' &&
|
||||||
!this._destroyed) {
|
!this._destroyed) {
|
||||||
emitDestroy(this[async_id_symbol]);
|
emitDestroy(this[async_id_symbol]);
|
||||||
@ -684,7 +679,7 @@ function tryOnImmediate(immediate, oldTail, count, refCount) {
|
|||||||
} finally {
|
} finally {
|
||||||
immediate._onImmediate = null;
|
immediate._onImmediate = null;
|
||||||
|
|
||||||
if (async_hook_fields[kDestroy] > 0) {
|
if (destroyHooksExist()) {
|
||||||
emitDestroy(immediate[async_id_symbol]);
|
emitDestroy(immediate[async_id_symbol]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -725,9 +720,9 @@ const Immediate = class Immediate {
|
|||||||
this._destroyed = false;
|
this._destroyed = false;
|
||||||
this[kRefed] = false;
|
this[kRefed] = false;
|
||||||
|
|
||||||
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
|
this[async_id_symbol] = newAsyncId();
|
||||||
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
|
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
|
||||||
if (async_hook_fields[kInit] > 0) {
|
if (initHooksExist()) {
|
||||||
emitInit(this[async_id_symbol],
|
emitInit(this[async_id_symbol],
|
||||||
'Immediate',
|
'Immediate',
|
||||||
this[trigger_async_id_symbol],
|
this[trigger_async_id_symbol],
|
||||||
@ -807,7 +802,7 @@ exports.clearImmediate = function(immediate) {
|
|||||||
toggleImmediateRef(false);
|
toggleImmediateRef(false);
|
||||||
immediate[kRefed] = undefined;
|
immediate[kRefed] = undefined;
|
||||||
|
|
||||||
if (async_hook_fields[kDestroy] > 0) {
|
if (destroyHooksExist()) {
|
||||||
emitDestroy(immediate[async_id_symbol]);
|
emitDestroy(immediate[async_id_symbol]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,6 @@ using v8::PromiseHookType;
|
|||||||
using v8::PropertyCallbackInfo;
|
using v8::PropertyCallbackInfo;
|
||||||
using v8::RetainedObjectInfo;
|
using v8::RetainedObjectInfo;
|
||||||
using v8::String;
|
using v8::String;
|
||||||
using v8::Symbol;
|
|
||||||
using v8::TryCatch;
|
using v8::TryCatch;
|
||||||
using v8::Undefined;
|
using v8::Undefined;
|
||||||
using v8::Value;
|
using v8::Value;
|
||||||
@ -472,12 +471,6 @@ void AsyncWrap::PopAsyncIds(const FunctionCallbackInfo<Value>& args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AsyncWrap::ClearAsyncIdStack(const FunctionCallbackInfo<Value>& args) {
|
|
||||||
Environment* env = Environment::GetCurrent(args);
|
|
||||||
env->async_hooks()->clear_async_id_stack();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
|
void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
|
||||||
AsyncWrap* wrap;
|
AsyncWrap* wrap;
|
||||||
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
|
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
|
||||||
@ -512,7 +505,6 @@ void AsyncWrap::Initialize(Local<Object> target,
|
|||||||
env->SetMethod(target, "setupHooks", SetupHooks);
|
env->SetMethod(target, "setupHooks", SetupHooks);
|
||||||
env->SetMethod(target, "pushAsyncIds", PushAsyncIds);
|
env->SetMethod(target, "pushAsyncIds", PushAsyncIds);
|
||||||
env->SetMethod(target, "popAsyncIds", PopAsyncIds);
|
env->SetMethod(target, "popAsyncIds", PopAsyncIds);
|
||||||
env->SetMethod(target, "clearAsyncIdStack", ClearAsyncIdStack);
|
|
||||||
env->SetMethod(target, "queueDestroyAsyncId", QueueDestroyAsyncId);
|
env->SetMethod(target, "queueDestroyAsyncId", QueueDestroyAsyncId);
|
||||||
env->SetMethod(target, "enablePromiseHook", EnablePromiseHook);
|
env->SetMethod(target, "enablePromiseHook", EnablePromiseHook);
|
||||||
env->SetMethod(target, "disablePromiseHook", DisablePromiseHook);
|
env->SetMethod(target, "disablePromiseHook", DisablePromiseHook);
|
||||||
@ -581,17 +573,6 @@ void AsyncWrap::Initialize(Local<Object> target,
|
|||||||
#undef V
|
#undef V
|
||||||
FORCE_SET_TARGET_FIELD(target, "Providers", async_providers);
|
FORCE_SET_TARGET_FIELD(target, "Providers", async_providers);
|
||||||
|
|
||||||
// These Symbols are used throughout node so the stored values on each object
|
|
||||||
// can be accessed easily across files.
|
|
||||||
FORCE_SET_TARGET_FIELD(
|
|
||||||
target,
|
|
||||||
"async_id_symbol",
|
|
||||||
Symbol::New(isolate, FIXED_ONE_BYTE_STRING(isolate, "asyncId")));
|
|
||||||
FORCE_SET_TARGET_FIELD(
|
|
||||||
target,
|
|
||||||
"trigger_async_id_symbol",
|
|
||||||
Symbol::New(isolate, FIXED_ONE_BYTE_STRING(isolate, "triggerAsyncId")));
|
|
||||||
|
|
||||||
#undef FORCE_SET_TARGET_FIELD
|
#undef FORCE_SET_TARGET_FIELD
|
||||||
|
|
||||||
env->set_async_hooks_init_function(Local<Function>());
|
env->set_async_hooks_init_function(Local<Function>());
|
||||||
|
@ -125,8 +125,6 @@ class AsyncWrap : public BaseObject {
|
|||||||
static void GetAsyncId(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void GetAsyncId(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void PushAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void PushAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void PopAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void PopAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void ClearAsyncIdStack(
|
|
||||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
static void AsyncReset(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void AsyncReset(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void QueueDestroyAsyncId(
|
static void QueueDestroyAsyncId(
|
||||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
|
@ -172,6 +172,7 @@ inline bool Environment::AsyncHooks::pop_async_id(double async_id) {
|
|||||||
return fields_[kStackLength] > 0;
|
return fields_[kStackLength] > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keep in sync with clearAsyncIdStack in lib/internal/async_hooks.js.
|
||||||
inline void Environment::AsyncHooks::clear_async_id_stack() {
|
inline void Environment::AsyncHooks::clear_async_id_stack() {
|
||||||
async_id_fields_[kExecutionAsyncId] = 0;
|
async_id_fields_[kExecutionAsyncId] = 0;
|
||||||
async_id_fields_[kTriggerAsyncId] = 0;
|
async_id_fields_[kTriggerAsyncId] = 0;
|
||||||
|
@ -34,8 +34,8 @@ assert.strictEqual(
|
|||||||
'RangeError [ERR_INVALID_ASYNC_ID]: Invalid triggerAsyncId value: -2');
|
'RangeError [ERR_INVALID_ASYNC_ID]: Invalid triggerAsyncId value: -2');
|
||||||
assert.strictEqual(c2.status, 1);
|
assert.strictEqual(c2.status, 1);
|
||||||
|
|
||||||
const expectedId = async_hooks.newUid();
|
const expectedId = async_hooks.newAsyncId();
|
||||||
const expectedTriggerId = async_hooks.newUid();
|
const expectedTriggerId = async_hooks.newAsyncId();
|
||||||
const expectedType = 'test_emit_before_after_type';
|
const expectedType = 'test_emit_before_after_type';
|
||||||
|
|
||||||
// Verify that if there is no registered hook, then nothing will happen.
|
// Verify that if there is no registered hook, then nothing will happen.
|
||||||
|
@ -7,8 +7,8 @@ const spawnSync = require('child_process').spawnSync;
|
|||||||
const async_hooks = require('internal/async_hooks');
|
const async_hooks = require('internal/async_hooks');
|
||||||
const initHooks = require('./init-hooks');
|
const initHooks = require('./init-hooks');
|
||||||
|
|
||||||
const expectedId = async_hooks.newUid();
|
const expectedId = async_hooks.newAsyncId();
|
||||||
const expectedTriggerId = async_hooks.newUid();
|
const expectedTriggerId = async_hooks.newAsyncId();
|
||||||
const expectedType = 'test_emit_init_type';
|
const expectedType = 'test_emit_init_type';
|
||||||
const expectedResource = { key: 'test_emit_init_resource' };
|
const expectedResource = { key: 'test_emit_init_resource' };
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
// Flags: --expose-internals
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
|
const { async_id_symbol } = require('internal/async_hooks').symbols;
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
|
|
||||||
// Regression test for https://github.com/nodejs/node/issues/13325
|
// Regression test for https://github.com/nodejs/node/issues/13325
|
||||||
|
@ -9,8 +9,10 @@ const assert = require('assert');
|
|||||||
const net = require('net');
|
const net = require('net');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const uv = process.binding('uv');
|
const uv = process.binding('uv');
|
||||||
const { async_id_symbol } = process.binding('async_wrap');
|
const {
|
||||||
const { newUid } = require('internal/async_hooks');
|
newAsyncId,
|
||||||
|
symbols: { async_id_symbol }
|
||||||
|
} = require('internal/async_hooks');
|
||||||
|
|
||||||
const agent = new http.Agent();
|
const agent = new http.Agent();
|
||||||
agent.createConnection = common.mustCall((cfg) => {
|
agent.createConnection = common.mustCall((cfg) => {
|
||||||
@ -26,7 +28,7 @@ agent.createConnection = common.mustCall((cfg) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Simulate just enough socket handle initialization
|
// Simulate just enough socket handle initialization
|
||||||
sock[async_id_symbol] = newUid();
|
sock[async_id_symbol] = newAsyncId();
|
||||||
|
|
||||||
sock.connect(cfg);
|
sock.connect(cfg);
|
||||||
return sock;
|
return sock;
|
||||||
|
@ -44,5 +44,5 @@ assert.notStrictEqual(newgid, oldgid);
|
|||||||
|
|
||||||
const olduid = process.geteuid();
|
const olduid = process.geteuid();
|
||||||
process.seteuid('nobody');
|
process.seteuid('nobody');
|
||||||
const newuid = process.geteuid();
|
const newAsyncId = process.geteuid();
|
||||||
assert.notStrictEqual(newuid, olduid);
|
assert.notStrictEqual(newAsyncId, olduid);
|
||||||
|
@ -63,5 +63,5 @@ assert.notStrictEqual(newgid, oldgid);
|
|||||||
|
|
||||||
const olduid = process.getuid();
|
const olduid = process.getuid();
|
||||||
process.setuid('nobody');
|
process.setuid('nobody');
|
||||||
const newuid = process.getuid();
|
const newAsyncId = process.getuid();
|
||||||
assert.notStrictEqual(newuid, olduid);
|
assert.notStrictEqual(newAsyncId, olduid);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user