Title: CLS store gets reset on the first callback in http scenario · Issue #32060 · nodejs/node · GitHub
Open Graph Title: CLS store gets reset on the first callback in http scenario · Issue #32060 · nodejs/node
X Title: CLS store gets reset on the first callback in http scenario · Issue #32060 · nodejs/node
Description: Version: master Platform: Linux Subsystem: async_hooks What steps will reproduce the bug? I have been trying to write some new scenarios for AsyncLocalStorage class, as mentioned in #31978, and came across this issue: const { AsyncLocalS...
Open Graph Description: Version: master Platform: Linux Subsystem: async_hooks What steps will reproduce the bug? I have been trying to write some new scenarios for AsyncLocalStorage class, as mentioned in #31978, and cam...
X Description: Version: master Platform: Linux Subsystem: async_hooks What steps will reproduce the bug? I have been trying to write some new scenarios for AsyncLocalStorage class, as mentioned in #31978, and cam...
Opengraph URL: https://github.com/nodejs/node/issues/32060
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"CLS store gets reset on the first callback in http scenario","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--\u003e\r\n\r\n* **Version**: master\r\n* **Platform**: Linux\r\n* **Subsystem**: async_hooks\r\n\r\n### What steps will reproduce the bug?\r\n\r\nI have been trying to write some new scenarios for AsyncLocalStorage class, as mentioned in https://github.com/nodejs/node/issues/31978, and came across this issue:\r\n\r\n\r\n```js\r\nconst { AsyncLocalStorage } = require('async_hooks');\r\nconst http = require('http')\r\nconst cls = new AsyncLocalStorage();\r\n\r\nconst server = http.createServer((req, res) =\u003e {\r\n res.write('hello')\r\n setTimeout(() =\u003e {\r\n res.end(' world!')\r\n }, 1000)\r\n})\r\n\r\nserver.listen(12000, () =\u003e {\r\n cls.run(() =\u003e {\r\n const req = http.get('http://localhost:12000', (res) =\u003e {\r\n const store = cls.getStore()\r\n store.set('foo', '')\r\n res.on('data', (d) =\u003e { console.log(cls.getStore()) });\r\n })\r\n req.end()\r\n })\r\n})\r\n```\r\n\r\nIn this simple client-server program, the server is sending two chunks of data, forcing the `ondata` handler to be invoked twice.\r\n\r\n### How often does it reproduce? Is there a required condition?\r\n\r\nevery time\r\n\r\n### What is the expected behavior?\r\n\r\nI get an empty Map every time in the ondata callback\r\n\r\n\u003c!--\r\nIf possible please provide textual output instead of screenshots.\r\n--\u003e\r\n\r\n### What do you see instead?\r\n\r\nUnfortunately, the store is `undefined` after the first invocation:\r\n\r\n```trace\r\n$ node foo.js\r\nMap(1) { 'foo' =\u003e '' }\r\nundefined\r\n^C\r\n$\r\n```\r\n\r\n\u003c!--\r\nIf possible please provide textual output instead of screenshots.\r\n--\u003e\r\n\r\n### Additional information\r\n\r\nIf I replace this with a simple timer code, this logic works fine:\r\n\r\n```js\r\nconst { AsyncLocalStorage } = require('async_hooks');\r\nconst cls = new AsyncLocalStorage();\r\n\r\ncls.run(() =\u003e {\r\n const store = cls.getStore()\r\n store.set('foo', 'bar')\r\n setInterval(() =\u003e {\r\n console.log(cls.getStore().get('foo'))\r\n }, 1000)\r\n}) \r\n```\r\n\r\n$ node timer.js\r\n```trace\r\nbar\r\nbar\r\nbar\r\nbar\r\n^C\r\n```\r\n\r\n\r\nAm I missing something?\r\n\r\nPing @vdeturckheim \r\n\u003c!--\r\nTell us anything else you think we should know.\r\n--\u003e\r\n","author":{"url":"https://github.com/HarshithaKP","@type":"Person","name":"HarshithaKP"},"datePublished":"2020-03-03T07:23:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/32060/node/issues/32060"}
| 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:63d5400d-1ede-0d54-a978-e28bff709876 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E46E:3F60C6:A0574E:D8365D:6A4ECF63 |
| html-safe-nonce | c7d7832dd6d7ed8f0ca875806d6ac28e906307b79aa276637be337769d035a24 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNDZFOjNGNjBDNjpBMDU3NEU6RDgzNjVEOjZBNEVDRjYzIiwidmlzaXRvcl9pZCI6Ijg0NDE0MTAyOTEwNjU4NjgxMzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7eaa6f6be0b443539651527b0640165c8e87a34c493ee523495eeee121066429 |
| hovercard-subject-tag | issue:574467544 |
| 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/32060/issue_layout |
| twitter:image | https://opengraph.githubassets.com/228e6d38530abbb72863b47ac096920960c4317ef64ff411e1130c4808501e79/nodejs/node/issues/32060 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/228e6d38530abbb72863b47ac096920960c4317ef64ff411e1130c4808501e79/nodejs/node/issues/32060 |
| og:image:alt | Version: master Platform: Linux Subsystem: async_hooks What steps will reproduce the bug? I have been trying to write some new scenarios for AsyncLocalStorage class, as mentioned in #31978, and cam... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | HarshithaKP |
| hostname | github.com |
| expected-hostname | github.com |
| None | 41b6ab3ba6d20a71766ac245b5a4a94c6fc672a9cd4da7d44c1b33ab8bf6a21c |
| 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 | e6a744804e8e70f97b4d5a18a94dcc63db22f97a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width