-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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: promise callbacks have no context unless init hook present #18520
Comments
/cc @nodejs/diagnostics @nodejs/async_hooks |
@ofrobots Yep, it is a known issue. It is because Unfortunately, we can't enable As I have suggested previously, it would be nice if |
Yep, got it. The overhead of promise hooks is quite a bummer indeed. I agree that we should not add the promise hook overhead to applications that are not interested in monitoring asynchronous flow. This was not clear in the docs, so I have gone ahead and opened #18540 to fix this. |
If we reorganize the PromiseHook API, we could teach V8 to assign these IDs for all promises, always (at least when running in Node). |
Sorry for not having the complete context here, but why is assigning an ID to a promise object too expensive? |
AsyncHooks have a few subtleties with being able to track promises. This commit adds a section to the docs that explains things the issues. PR-URL: nodejs#18540 Fixes: nodejs#18520 Reviewed-By: James M Snell <jasnell@gmail.com>
@gsathya assigning the id requires us to enable promise hooks; which are expensive. Basically we need the init hook to be called so that we can assign an id to the promise, and record the trigger (causal) context. |
Ah, so enabling promise hooks to know when to assign an ID is expensive. Assigning an ID isn't. |
AsyncHooks have a few subtleties with being able to track promises. This commit adds a section to the docs that explains things the issues. PR-URL: nodejs#18540 Fixes: nodejs#18520 Reviewed-By: James M Snell <jasnell@gmail.com>
While going through some examples I ran into the following buggy behavior in async_hooks. Promise callbacks don't seem to have context by default. Even more surprisingly Adding a dummy init makes the context appears as if by magic!
I intend to look at this in detail later, but opening the issue now in case someone else wants to take a peek.
Unlike timers, and other Node.js async APIs (e.g. fs.read), by default the
triggerAsyncId
is always 0 (i.e. missing context) inside thethen
callback. The behaviour is the same when no hooks are present or an empty set of hooks are present.❯ node p2.js 1 then callback ran with eid 1 tid 0
Attaching an empty init hook magically makes the context appear:
❯ node p2.js 2 then callback ran with eid 7 tid 6
Replacing the promise example with a
setTimeout
orfs.readFile
has the things working correctly in all scenarios.The text was updated successfully, but these errors were encountered: