Skip to content
Closed
Changes from 1 commit
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
Next Next commit
async_hooks: reduce duplication with factory
  • Loading branch information
BridgeAR committed Jun 19, 2017
commit 58f011ffdd2535312c7e3c2b93892cfea6afcc86
79 changes: 22 additions & 57 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
Copy Markdown
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
Copy Markdown
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 @@ -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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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