-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Conversation
lib/async_hooks.js
Outdated
function hookFactory(symbol) { | ||
// Called from native. The asyncId stack handling is taken care of there | ||
// before this is called. | ||
return function emitAfterN(asyncId) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this still called emitAfterN
?
lib/async_hooks.js
Outdated
@@ -325,7 +328,7 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) { | |||
triggerAsyncId = initTriggerId(); | |||
} | |||
|
|||
// I'd prefer allowing these checks to not exist, or only throw in a debug | |||
// TODO(trevnorris): I'd prefer allowing these checks to not exist, or only throw in a debug |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is over 80 characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor change request, but other than that LGTM
lib/async_hooks.js
Outdated
@@ -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); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trevnorris PTAL...
@@ -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 |
There was a problem hiding this comment.
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
.
lib/async_hooks.js
Outdated
function hookFactory(symbol) { | ||
// Called from native. The asyncId stack handling is taken care of there | ||
// before this is called. | ||
return function(asyncId) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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')
There was a problem hiding this comment.
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)
PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
1 question
lib/async_hooks.js
Outdated
@@ -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 = emitHookFactory(before_symbol); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: is there a need to declare these? Stack beauty?
Otherwise could just be used in L#61
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the emitDestroyN
hook is used only once, the other two are used in more than one spot. I made them all const for consistency.
} | ||
} catch (e) { | ||
fatalError(e); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx
Comment addressed |
lib/async_hooks.js
Outdated
@@ -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 = emitHookFactory(before_symbol, 'emitBeforeN'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you change the N
to Native
while you are at it? That short naming convention really annoys me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess in that case the the S
should also be renamed but I can't think of what it stands for right now. Does it stand for Script
?
lib/async_hooks.js
Outdated
restoreTmpHooks(); | ||
} | ||
}; | ||
Object.defineProperty(fn, 'name', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind leaving a comment that explains this sets the anonymous function name such it looks good in stack traces.
@trevnorris why is
@BridgeAR If the above is true, please also rename |
@AndreasMadsen I'm not sure if we can factorize init as it calls the hooks with |
Haha, completly forgot about that, never mind :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
landed in 5c6c029 |
PR-URL: #13755 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
PR-URL: #13755 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
This fixes an error that could occure by nesting async_hooks calls PR-URL: #14054 Ref: #13755 (comment) Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
PR-URL: #13755 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
This fixes an error that could occure by nesting async_hooks calls PR-URL: #14054 Ref: #13755 (comment) Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
The difference between the hooks is only a single variable and it can be initialized in the beginning. So I think it's nicer to stick to DRY.
The
init
function is also very similar but that would require an additional check, so I kept it as it is.I also added a
TODO
entry as the comment somewhat confused me (ping @trevnorris ).Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
async_hooks