Title: AsyncLocalStore looses context in MakeCallback · Issue #43038 · nodejs/node · GitHub
Open Graph Title: AsyncLocalStore looses context in MakeCallback · Issue #43038 · nodejs/node
X Title: AsyncLocalStore looses context in MakeCallback · Issue #43038 · nodejs/node
Description: Version 18.1.0, 14.19.1 (and most likely a lot others) Platform Tested on Microsoft Windows NT 10.0.22000.0 x64 but very likely platform independent. Subsystem async_hooks What steps will reproduce the bug? Use node::MakeCallback() with ...
Open Graph Description: Version 18.1.0, 14.19.1 (and most likely a lot others) Platform Tested on Microsoft Windows NT 10.0.22000.0 x64 but very likely platform independent. Subsystem async_hooks What steps will reproduce...
X Description: Version 18.1.0, 14.19.1 (and most likely a lot others) Platform Tested on Microsoft Windows NT 10.0.22000.0 x64 but very likely platform independent. Subsystem async_hooks What steps will reproduce...
Opengraph URL: https://github.com/nodejs/node/issues/43038
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"AsyncLocalStore looses context in MakeCallback","articleBody":"### Version\r\n\r\n18.1.0, 14.19.1 (and most likely a lot others)\r\n\r\n### Platform\r\n\r\nTested on `Microsoft Windows NT 10.0.22000.0 x64` but very likely platform independent.\r\n\r\n### Subsystem\r\n\r\nasync_hooks\r\n\r\n### What steps will reproduce the bug?\r\n\r\nUse `node::MakeCallback()` with a receiver different to the `AsyncResource`.\r\n\r\nSee e.g. [snappy 6.x](https://github.com/kesla/node-snappy/blob/53bdf54e6e84e3d898b2f6cd00baeb9f16e9fb43/src/binding.cc#L13-L43) which uses [`Nan::AsyncWorker`](https://github.com/nodejs/nan/blob/16fa32231e2ccd89d2804b3f765319128b20c4ac/nan.h#L1909-L2026).\r\n\r\nIn this setup the complete callback is not called with the `AsyncResource` as target and this results in setting the wrong object as current resource in [`MakeCallback`](https://github.com/nodejs/node/blob/76499898763102272a47d642f905e47c0cc41df0/src/api/callback.cc#L282).\r\n\r\nCalling callbacks with undefined/null/global as target is not that uncommon.\r\n\r\nThe before/after callbacks are called with the correct asyncId but `AsyncLocalStore` relies on the current resource.\r\n\r\n```js\r\nconst { executionAsyncId, AsyncLocalStorage } = require(\"async_hooks\")\r\nconst snappy = require(\"snappy\")\r\nconst als = new AsyncLocalStorage()\r\n\r\nals.run(15, () => {\r\n snappy.compress(\"Hello World!\", () => {\r\n const cbId = executionAsyncId()\r\n console.log(`compressed: ${cbId}, als: ${als.getStore()}`)\r\n })\r\n})\r\n```\r\n\r\n### How often does it reproduce? Is there a required condition?\r\n\r\nAlways if `MakeCallback` is used with a receiver different then the `AsyncResource` object.\r\n\r\n### What is the expected behavior?\r\n\r\nno context loss\r\n\r\n### What do you see instead?\r\n\r\ncontext loss \r\n\r\n### Additional information\r\n\r\n_No response_","author":{"url":"https://github.com/Flarna","@type":"Person","name":"Flarna"},"datePublished":"2022-05-10T14:19:27Z","url":"https://github.com/nodejs/node/issues/43038"}
| 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:d12b0f1d-0df0-1c12-a9ae-fbd756f58b70 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 935C:E530F:61B7A3:81D9A0:6AED1DE9 |
| html-safe-nonce | da4ecb37aad2be9cef3c47ffbd18bdac417ed82c49be94c938d94a561f2a38e1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzVDOkU1MzBGOjYxQjdBMzo4MUQ5QTA6NkFFRDFERTkiLCJ2aXNpdG9yX2lkIjoiODQ3NzUxNTk5Mzc3OTYwOTA2NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 256ea8eba75900387a1002fd9a6ccbee6304fefb6b3fc05264b0847322b6d143 |
| hovercard-subject-tag | issue:1231242152 |
| 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/43038/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1aba303fdc08d0037e07f55d72c8baf35966dfac7a9c222a7db42fa169266eab/nodejs/node/issues/43038 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1aba303fdc08d0037e07f55d72c8baf35966dfac7a9c222a7db42fa169266eab/nodejs/node/issues/43038 |
| og:image:alt | Version 18.1.0, 14.19.1 (and most likely a lot others) Platform Tested on Microsoft Windows NT 10.0.22000.0 x64 but very likely platform independent. Subsystem async_hooks What steps will reproduce... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Flarna |
| 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