Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_hooks: reduce code duplication by using a factory #13755

Closed
wants to merge 7 commits into from
Closed
Changes from 2 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
83 changes: 24 additions & 59 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const init_symbol = Symbol('init');
const before_symbol = Symbol('before');
const after_symbol = Symbol('after');
const destroy_symbol = Symbol('destroy');
const emitBeforeN = hookFactory(before_symbol);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: change this to hookFactoryEmit or emitHookFactory.

Copy link
Contributor

Choose a reason for hiding this comment

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

@trevnorris PTAL...

const emitAfterN = hookFactory(after_symbol);
const emitDestroyN = hookFactory(destroy_symbol);

// Setup the callbacks that node::AsyncWrap will call when there are hooks to
// process. They use the same functions as the JS embedder API. These callbacks
Expand Down Expand Up @@ -325,8 +328,8 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) {
triggerAsyncId = initTriggerId();
}

// I'd prefer allowing these checks to not exist, or only throw in a debug
// build, in order to improve performance.
// TODO(trevnorris): I'd prefer allowing these checks to not exist, or only
Copy link
Contributor

Choose a reason for hiding this comment

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

good call changing this to a TODO.

// throw in a debug build, in order to improve performance.
if (!Number.isSafeInteger(asyncId) || asyncId < 0)
throw new RangeError('asyncId must be an unsigned integer');
if (typeof type !== 'string' || type.length <= 0)
Expand All @@ -342,24 +345,28 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) {
}
}


function emitBeforeN(asyncId) {
processing_hook = true;
// Use a single try/catch for all hook to avoid setting up one per iteration.
try {
for (var i = 0; i < active_hooks_array.length; i++) {
if (typeof active_hooks_array[i][before_symbol] === 'function') {
active_hooks_array[i][before_symbol](asyncId);
function hookFactory(symbol) {
// Called from native. The asyncId stack handling is taken care of there
// before this is called.
return function(asyncId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Have we verified how this'll change the stack trace from any of these? If V8 can properly infer the name based on the variable name then great.

Copy link
Member Author

Choose a reason for hiding this comment

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

It does that on the newer v8 versions:

> const a = { foo: function () {}, bar() {}, baz: () => {} }
undefined
> a.foo.name
'foo'
> a.bar.name
'bar'
> a.baz.name
'baz'

But I am not certain if there might be a context in which it is not inferred. I am also not sure where to trigger a stack trace from one of these functions as I didn't look deep into the async_hooks code so far. Can you point me out to something or give me a small example?

I could enforce setting the name though.

function hookFactory(symbol, name) {
  const fn = function () {}
  Object.defineProperty(fn, 'name', {
    value: name
  })
  return fn
}

const hook = hookFactory(Symbol(), 'myName')

Copy link
Member Author

Choose a reason for hiding this comment

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

The stack trace was changed so I now explicitly set the name and it's now looking good again.

Error
    at AsyncHook.before (repl:2:26)
    at emitBeforeN (async_hooks.js:358:40)
    at emitBeforeS (async_hooks.js:393:3)
    at nextTickEmitBefore (internal/process/next_tick.js:158:7)
    at process._tickDomainCallback (internal/process/next_tick.js:230:9)

processing_hook = true;
// Use a single try/catch for all hook to avoid setting up one per
// iteration.
try {
for (var i = 0; i < active_hooks_array.length; i++) {
if (typeof active_hooks_array[i][symbol] === 'function') {
active_hooks_array[i][symbol](asyncId);
}
}
} catch (e) {
fatalError(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

You asked how to reach this fatalError call. The following will do it:

require('async_hooks').createHook({
  before() { throw new Error('ah crud') },
}).enable();
setTimeout(() => {}, 10);

Copy link
Member Author

Choose a reason for hiding this comment

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

Thx

}
} catch (e) {
fatalError(e);
}
processing_hook = false;
processing_hook = false;

if (tmp_active_hooks_array !== null) {
restoreTmpHooks();
}
if (tmp_active_hooks_array !== null) {
restoreTmpHooks();
}
};
}


Expand All @@ -383,28 +390,6 @@ function emitBeforeS(asyncId, triggerAsyncId = asyncId) {
}


// Called from native. The asyncId stack handling is taken care of there before
// this is called.
function emitAfterN(asyncId) {
processing_hook = true;
// Use a single try/catch for all hook to avoid setting up one per iteration.
try {
for (var i = 0; i < active_hooks_array.length; i++) {
if (typeof active_hooks_array[i][after_symbol] === 'function') {
active_hooks_array[i][after_symbol](asyncId);
}
}
} catch (e) {
fatalError(e);
}
processing_hook = false;

if (tmp_active_hooks_array !== null) {
restoreTmpHooks();
}
}


// TODO(trevnorris): Calling emitBefore/emitAfter from native can't adjust the
// kIdStackIndex. But what happens if the user doesn't have both before and
// after callbacks.
Expand All @@ -425,26 +410,6 @@ function emitDestroyS(asyncId) {
}


function emitDestroyN(asyncId) {
processing_hook = true;
// Use a single try/catch for all hook to avoid setting up one per iteration.
try {
for (var i = 0; i < active_hooks_array.length; i++) {
if (typeof active_hooks_array[i][destroy_symbol] === 'function') {
active_hooks_array[i][destroy_symbol](asyncId);
}
}
} catch (e) {
fatalError(e);
}
processing_hook = false;

if (tmp_active_hooks_array !== null) {
restoreTmpHooks();
}
}


// Emit callbacks for native calls. Since some state can be setup directly from
// C++ there's no need to perform all the work here.

Expand Down