Repository navigation
async_hooks.triggerAsyncId() don't return the expected value in context of the connection callback of net.Server #21078
Description
Activity
ping @nodejs/async_hooks — not sure what the correct outcome here is. I can see validity to both, depending on how one thinks about it. Either way, seems like the documentation doesn't match the reality so one of the two needs to be updated.
- addedasync_hooksIssues and PRs related to the async hooks subsystem.Issues and PRs related to the async hooks subsystem.
on Jun 5, 2018 This has broken cls-hooked since 8.10.0. It's been raised as an issue in
cls-hookedand it's probably affecting more people who are unaware of it.Just to confirm what @tingshao stated above, when I compare logs of 8.9.4 (left) to 8.10.0 (right), the change from
TCPWRAPtoTCPSERVERWRAPcan be seen.Reacted by Stanislav Poslavsky and Dmitrii Kanatnikov@nodejs/async_hooks @AndreasMadsen @addaleax What are your thoughts on this? It appears the documentation doesn't match the behavior or are we missing something?
If so, do we have more options than this?
- Update the documentation and support the current behavior going forward.
or - Change the behavior and then potentially back-port the fix to node 8?
Thank you!
- Update the documentation and support the current behavior going forward.
@AndreasMadsen @addaleax Looks like this was never addressed. I assume at this point it's too late to expect behaviour change, so probably simple documentation change would suffice?
@kibertoad Well, basically what @apapirovski said … I can see the validity of either point of view. I think what this issue is waiting for is for somebody to have a strong enough opinion to either change the docs or change the behavior here, but yeah, a documentation change would definitely suffice.
Reacted by Benjamin Gruenbaum and Igor Savingithub-actions commented
on Jun 27, 2026 on Jun 27, 2026 – with GitHub ActionsContributorMore actionsThis issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Jun 27, 2026 Still true on v26 — in the connection callback
triggerAsyncId()returns the server's own triggerAsyncId, not the connection's asyncId, because the callback runs in the server'sMakeCallback()scope. Opened #64583 with the doc change @addaleax signed off on: it fixes the one wrong comment, mirroring the siblingexecutionAsyncId()example, which has had it right all along.- removedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Jul 19, 2026 - added a commit that references this issue
on Aug 28, 2026 - added 2 commits that reference this issue
on Aug 29, 2026 - added a commit that references this issue
on Sep 9, 2026 - added 2 commits that reference this issue
on Sep 9, 2026

async_hooks.triggerAsyncId() is expected to return the async id of the connection in the
onconnectioncallback of server. This was specifically declared in the example of the async_hooks documentation which can be found here.However, after testing the example using both node 10.3.0 and 8.11.2, I found the result was different. Below is the code and output of my test.
my code:
if I connect the server with another terminal by
connect localhost 8000, then the output is:Please note the line
-- conn callback, triggerAsyncId: 1, executionAsyncId: 5. It means in the connection callback, the triggerAsyncId is 1 instead of 7 which is expected.I investigated the code, and found the root cause is:
When the callback is made, it was made on the TCPWrap instance of the server, and the server's triggerId (1) and asyncId (5) were pushed into the stack, thus lead to this result. While It seems that this mechanism is ok most of the time for normal callbacks, but for this new connection callback, I think we should add some logic to pass the triggerId of the new connection (actually a new TCPWrap instance) to push into the stack. I made a change based on that and verified the result, it could work.
So, I doubt is the issue due to document obsoleted or the code should be changed?
I'd like to make a PR if it is confirmed, thanks.