Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const net = require('net');
const util = require('util');
const EventEmitter = require('events');
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');

// New Agent code.
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const common = require('_http_common');
const checkIsHttpToken = common._checkIsHttpToken;
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
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 errors = require('internal/errors');

Expand Down
23 changes: 5 additions & 18 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,31 @@ const internal_async_hooks = require('internal/async_hooks');
// resource gets gced.
const { registerDestroyHook } = async_wrap;
const {
executionAsyncId,
triggerAsyncId,
// Private API
getHookArrays,
enableHooks,
disableHooks,
// Internal Embedder API
newUid,
newAsyncId,
getDefaultTriggerAsyncId,
emitInit,
emitBefore,
emitAfter,
emitDestroy,
} = internal_async_hooks;

// Get fields
const { async_id_fields } = async_wrap;

// Get symbols
const {
async_id_symbol, trigger_async_id_symbol,
init_symbol, before_symbol, after_symbol, destroy_symbol,
promise_resolve_symbol
} = internal_async_hooks.symbols;

const { async_id_symbol, trigger_async_id_symbol } = async_wrap;

// Get constants
const {
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
kExecutionAsyncId, kTriggerAsyncId
} = async_wrap.constants;

// Listener API //
Expand Down Expand Up @@ -125,16 +122,6 @@ function createHook(fns) {
}


function executionAsyncId() {
return async_id_fields[kExecutionAsyncId];
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: executionAsyncId and triggerAsyncId should probably stay together. Either both move or neither.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me. I'll move both. (I wanted to avoid requiring both files during bootstrap.)

function triggerAsyncId() {
return async_id_fields[kTriggerAsyncId];
}


// Embedder API //

const destroyedSymbol = Symbol('destroyed');
Expand Down Expand Up @@ -170,7 +157,7 @@ class AsyncResource {
triggerAsyncId);
}

this[async_id_symbol] = newUid();
this[async_id_symbol] = newAsyncId();
this[trigger_async_id_symbol] = triggerAsyncId;
// this prop name (destroyed) has to be synchronized with C++
this[destroyedSymbol] = { destroyed: false };
Expand Down
6 changes: 4 additions & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ const dns = require('dns');
const util = require('util');
const { isUint8Array } = require('internal/util/types');
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 { async_id_symbol } = process.binding('async_wrap');
const { nextTick } = require('internal/process/next_tick');

const { UDP, SendWrap } = process.binding('udp_wrap');
Expand Down
60 changes: 56 additions & 4 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* 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
// Environment::AsyncHooks::async_ids_stack_ tracks the resource responsible for
// the current execution stack. This is unwound as each resource exits. In the
Expand Down Expand Up @@ -71,6 +71,8 @@ const { kInit, kBefore, kAfter, kDestroy, kPromiseResolve,
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;

// Used in AsyncHook and AsyncResource.
const async_id_symbol = Symbol('asyncId');
const trigger_async_id_symbol = Symbol('triggerAsyncId');
const init_symbol = Symbol('init');
const before_symbol = Symbol('before');
const after_symbol = Symbol('after');
Expand Down Expand Up @@ -245,7 +247,7 @@ function disableHooks() {
// Increment the internal id counter and return the value. Important that the
// counter increment first. Since it's done the same way in
// Environment::new_async_uid()
function newUid() {
function newAsyncId() {
return ++async_id_fields[kAsyncIdCounter];
}

Expand All @@ -254,7 +256,7 @@ function getOrSetAsyncId(object) {
return object[async_id_symbol];
}

return object[async_id_symbol] = newUid();
return object[async_id_symbol] = newAsyncId();
}


Expand All @@ -270,6 +272,11 @@ function getDefaultTriggerAsyncId() {
}


function clearDefaultTriggerAsyncId() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It might be worth moving the copy of this operation in env-inl.h into a standalone create_default_trigger_async_id function so that we can attach a comment in both places to keep the copies in sync.

Aside: I've wanted to cleanup the magical -1 and 0 hard-coded constants for async id, but that should probably happen in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it makes sense to move it off into a function in C++ since it seems to only be done from the constructor. Perhaps a constant makes the most sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment that says that this should never be used except for exception handling?

async_id_fields[kDefaultTriggerAsyncId] = -1;
}


function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
// CHECK(Number.isSafeInteger(triggerAsyncId))
// CHECK(triggerAsyncId > 0)
Expand All @@ -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) {
validateAsyncId(asyncId, 'asyncId');
if (triggerAsyncId !== null)
Expand Down Expand Up @@ -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.
function pushAsyncIds(asyncId, triggerAsyncId) {
const offset = async_hook_fields[kStackLength];
Expand Down Expand Up @@ -377,20 +411,38 @@ function popAsyncIds(asyncId) {
}


function executionAsyncId() {
return async_id_fields[kExecutionAsyncId];
}

function triggerAsyncId() {
return async_id_fields[kTriggerAsyncId];
}


module.exports = {
executionAsyncId,
triggerAsyncId,
// Private API
getHookArrays,
symbols: {
async_id_symbol, trigger_async_id_symbol,
init_symbol, before_symbol, after_symbol, destroy_symbol,
promise_resolve_symbol
},
enableHooks,
disableHooks,
clearDefaultTriggerAsyncId,
clearAsyncIdStack,
hasAsyncIdStack,
// Internal Embedder API
newUid,
newAsyncId,
getOrSetAsyncId,
getDefaultTriggerAsyncId,
defaultTriggerAsyncIdScope,
initHooksExist,
afterHooksExist,
destroyHooksExist,
emitInit: emitInitScript,
emitBefore: emitBeforeScript,
emitAfter: emitAfterScript,
Expand Down
26 changes: 13 additions & 13 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,18 +424,19 @@
function noop() {}

function setupProcessFatal() {
const async_wrap = process.binding('async_wrap');
// Arrays containing hook flags and ids for async_hook calls.
const { async_hook_fields, async_id_fields } = async_wrap;
// Internal functions needed to manipulate the stack.
const { clearAsyncIdStack } = async_wrap;
const { kAfter, kExecutionAsyncId,
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
const {
executionAsyncId,
clearDefaultTriggerAsyncId,
clearAsyncIdStack,
hasAsyncIdStack,
afterHooksExist,
emitAfter
} = NativeModule.require('internal/async_hooks');

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.
async_id_fields[kDefaultTriggerAsyncId] = -1;
clearDefaultTriggerAsyncId();

if (exceptionHandlerState.captureFn !== null) {
exceptionHandlerState.captureFn(er);
Expand All @@ -458,11 +459,10 @@
NativeModule.require('timers').setImmediate(noop);

// Emit the after() hooks now that the exception has been handled.
if (async_hook_fields[kAfter] > 0) {
const { emitAfter } = NativeModule.require('internal/async_hooks');
if (afterHooksExist()) {
do {
emitAfter(async_id_fields[kExecutionAsyncId]);
} while (async_hook_fields[kStackLength] > 0);
emitAfter(executionAsyncId());
} while (hasAsyncIdStack());
// Or completely empty the id stack.
} else {
clearAsyncIdStack();
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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 binding = process.binding('http2');
const assert = require('assert');
Expand Down
27 changes: 14 additions & 13 deletions lib/internal/process/next_tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ exports.setup = setupNextTick;
exports.nextTick = null;

function setupNextTick() {
const async_wrap = process.binding('async_wrap');
const async_hooks = require('internal/async_hooks');
const {
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 errors = require('internal/errors');
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
// have easy access to our nextTick state, and avoid unnecessary
Expand Down Expand Up @@ -104,7 +105,7 @@ function setupNextTick() {
// that nextTick() doesn't allow the event loop to proceed, but if
// any async hooks are enabled during the callback's execution then
// this tock's after hook will be called, but not its destroy hook.
if (async_hook_fields[kDestroy] > 0)
if (destroyHooksExist())
emitDestroy(asyncId);

const callback = tock.callback;
Expand All @@ -128,11 +129,11 @@ function setupNextTick() {
this.callback = callback;
this.args = args;

const asyncId = ++async_id_fields[kAsyncIdCounter];
const asyncId = newAsyncId();
this[async_id_symbol] = asyncId;
this[trigger_async_id_symbol] = triggerAsyncId;

if (async_hook_fields[kInit] > 0) {
if (initHooksExist()) {
emitInit(asyncId,
'TickObject',
triggerAsyncId,
Expand Down
12 changes: 4 additions & 8 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
'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 {
getDefaultTriggerAsyncId,
// The needed emit*() functions.
newAsyncId,
initHooksExist,
emitInit
} = 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.
const async_id_symbol = Symbol('asyncId');
Copy link
Member

@AndreasMadsen AndreasMadsen Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have separate symbols for this? 😂 I think they should just be async_id_symbol and trigger_async_id_symbol from async_hooks.

Copy link
Contributor Author

@apapirovski apapirovski Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny enough, it's actually necessary because of enroll and unenroll. Since they could use an object that already has an asyncId associated, it would overwrite it or misrepresent it in emit.

const trigger_async_id_symbol = Symbol('triggerId');
Expand Down Expand Up @@ -70,9 +66,9 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {

this[unrefedSymbol] = isUnrefed;

this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
this[async_id_symbol] = newAsyncId();
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
if (async_hook_fields[kInit] > 0) {
if (initHooksExist()) {
emitInit(this[async_id_symbol],
'Timeout',
this[trigger_async_id_symbol],
Expand Down
9 changes: 6 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
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, defaultTriggerAsyncIdScope } = require('internal/async_hooks');
const {
newAsyncId,
defaultTriggerAsyncIdScope,
symbols: { async_id_symbol }
} = require('internal/async_hooks');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const dns = require('dns');
Expand Down Expand Up @@ -91,7 +94,7 @@ function createHandle(fd, is_server) {

function getNewAsyncId(handle) {
return (!handle || typeof handle.getAsyncId !== 'function') ?
newUid() : handle.getAsyncId();
newAsyncId() : handle.getAsyncId();
}


Expand Down
Loading