Title: async_hooks.triggerAsyncId() don't return the expected value in context of the connection callback of net.Server · Issue #21078 · nodejs/node · GitHub
Open Graph Title: async_hooks.triggerAsyncId() don't return the expected value in context of the connection callback of net.Server · Issue #21078 · nodejs/node
X Title: async_hooks.triggerAsyncId() don't return the expected value in context of the connection callback of net.Server · Issue #21078 · nodejs/node
Description: Version: 8.x and 10.x Platform: all supported Subsystem: async_hooks.triggerAsyncId() is expected to return the async id of the connection in the onconnection callback of server. This was specifically declared in the example of the async...
Open Graph Description: Version: 8.x and 10.x Platform: all supported Subsystem: async_hooks.triggerAsyncId() is expected to return the async id of the connection in the onconnection callback of server. This was specifica...
X Description: Version: 8.x and 10.x Platform: all supported Subsystem: async_hooks.triggerAsyncId() is expected to return the async id of the connection in the onconnection callback of server. This was specifica...
Opengraph URL: https://github.com/nodejs/node/issues/21078
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"async_hooks.triggerAsyncId() don't return the expected value in context of the connection callback of net.Server","articleBody":"\u003c!--\r\nThank you for reporting an issue.\r\n\r\nThis issue tracker is for bugs and issues found within Node.js core.\r\nIf you require more general support please file an issue on our help\r\nrepo. https://github.com/nodejs/help\r\n\r\n\r\nPlease fill in as much of the template below as you're able.\r\n\r\nVersion: output of `node -v`\r\nPlatform: output of `uname -a` (UNIX), or version and 32 or 64-bit (Windows)\r\nSubsystem: if known, please specify affected core module name\r\n\r\nIf possible, please provide code that demonstrates the problem, keeping it as\r\nsimple and free of external dependencies as you are able.\r\n-->\r\n\r\n* **Version**: 8.x and 10.x\r\n* **Platform**: all supported\r\n* **Subsystem**:\r\n\r\n\u003c!-- Enter your issue details below this comment. -->\r\nasync_hooks.triggerAsyncId() is expected to return the async id of the connection in the `onconnection` callback of server. This was specifically declared in the example of the async_hooks documentation which can be found [here](https://nodejs.org/dist/latest-v10.x/docs/api/async_hooks.html#async_hooks_async_hooks_triggerasyncid). \r\n\r\nHowever, 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.\r\nmy code:\r\n```\r\nlet net = require('net');\r\nlet async_hooks = require('async_hooks');\r\nlet fs = require('fs');\r\n\r\nasync_hooks.createHook({\r\n init(asyncId, type, triggerAsyncId) {\r\n \tfs.writeSync(1, `init hook: ${type}, asyncID: ${asyncId}, triggerID: ${triggerAsyncId}\\n`);\r\n },\r\n before(asyncId) {\r\n fs.writeSync(1, `before hook: asyncID: ${asyncId}\\n`);\r\n },\r\n after(asyncId) {\r\n fs.writeSync(1, `after hook: asyncId: ${asyncId}\\n`);\r\n },\r\n}).enable();\r\nconst server = net.createServer((conn) => {\r\n // The resource that caused (or triggered) this callback to be called\r\n // was that of the new connection. Thus the return value of triggerAsyncId()\r\n // is the asyncId of \"conn\".\r\n // fs.wriateSync(1, '-- conn callback, triggerAsyncId:', async_hooks.triggerAsyncId());\r\n let tid = async_hooks.triggerAsyncId();\r\n let eid = async_hooks.executionAsyncId();\r\n fs.writeSync(1, `-- conn callback, triggerAsyncId: ${tid}, executionAsyncId: ${eid}\\n`);\r\n}).listen(8000, () => {\r\n // Even though all callbacks passed to .listen() are wrapped in a nextTick()\r\n // the callback itself exists because the call to the server's .listen()\r\n // was made. So the return value would be the ID of the server.\r\n let tid = async_hooks.triggerAsyncId();\r\n let eid = async_hooks.executionAsyncId();\r\n fs.writeSync(1, `-- listen callback, triggerAsyncId: ${tid}, executionAsyncId: ${eid}\\n`);\r\n});\r\n```\r\n\r\nif I connect the server with another terminal by `connect localhost 8000`, then the output is:\r\n```\r\ninit hook: TCPSERVERWRAP, asyncID: 5, triggerID: 1\r\ninit hook: TickObject, asyncID: 6, triggerID: 5\r\nbefore hook: asyncID: 6\r\n-- listen callback, triggerAsyncId: 5, executionAsyncId: 6\r\nafter hook: asyncId: 6\r\ninit hook: TCPWRAP, asyncID: 7, triggerID: 5\r\nbefore hook: asyncID: 5\r\n-- conn callback, triggerAsyncId: 1, executionAsyncId: 5\r\nafter hook: asyncId: 5\r\nbefore hook: asyncID: 7\r\ninit hook: TickObject, asyncID: 8, triggerID: 7\r\nafter hook: asyncId: 7\r\nbefore hook: asyncID: 8\r\ninit hook: TickObject, asyncID: 9, triggerID: 8\r\ninit hook: TickObject, asyncID: 10, triggerID: 8\r\nafter hook: asyncId: 8\r\nbefore hook: asyncID: 9\r\nafter hook: asyncId: 9\r\nbefore hook: asyncID: 10\r\nafter hook: asyncId: 10\r\nbefore hook: asyncID: 7\r\nafter hook: asyncId: 7\r\n\r\n```\r\n\r\nPlease 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.\r\n\r\nI investigated the code, and found the root cause is:\r\nWhen 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.\r\n\r\nSo, I doubt is the issue due to document obsoleted or the code should be changed?\r\nI'd like to make a PR if it is confirmed, thanks.","author":{"url":"https://github.com/tingshao","@type":"Person","name":"tingshao"},"datePublished":"2018-06-01T08:20:21Z","url":"https://github.com/nodejs/node/issues/21078"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7a87d993-f7f3-9237-3511-2b2dc046b4d1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8524:2B3418:345449:442A2A:6AED1DD9 |
| html-safe-nonce | f0e6ec0e677ea41ce65666d713adcafc55ab9a2661f50f2c3199ed4954be5542 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTI0OjJCMzQxODozNDU0NDk6NDQyQTJBOjZBRUQxREQ5IiwidmlzaXRvcl9pZCI6IjM3NTUwNzE4MjI1Nzk0NDExMTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 87b733499d266b181f82f6b220a522dd81ba2ca70378cdc773ba1b04a0c10eea |
| hovercard-subject-tag | issue:328424186 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/21078/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7a4ec0ecd124f4822fd8235e15552becd329c53273f7b9561c1571ced3fdb3e5/nodejs/node/issues/21078 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7a4ec0ecd124f4822fd8235e15552becd329c53273f7b9561c1571ced3fdb3e5/nodejs/node/issues/21078 |
| og:image:alt | Version: 8.x and 10.x Platform: all supported Subsystem: async_hooks.triggerAsyncId() is expected to return the async id of the connection in the onconnection callback of server. This was specifica... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tingshao |
| hostname | github.com |
| expected-hostname | github.com |
| None | e3e0f7f63e50c6fe879198a93a0a023a648ecc0a5e9bc3b5e033ac57172237e6 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 707a24b346ac8a82878b9c084efaa38a997660cb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width