Title: Better scope management for async code · Issue #3751 · getsentry/sentry-javascript · GitHub
Open Graph Title: Better scope management for async code · Issue #3751 · getsentry/sentry-javascript
X Title: Better scope management for async code · Issue #3751 · getsentry/sentry-javascript
Description: Currently, the @sentry/browser SDK has no mechanism to automatically clone the Hub (and, consequently, Scope), unlike @sentry/node that automatically clones a new hub per Node Domain -- and we use that to isolate state for Node server ha...
Open Graph Description: Currently, the @sentry/browser SDK has no mechanism to automatically clone the Hub (and, consequently, Scope), unlike @sentry/node that automatically clones a new hub per Node Domain -- and we use ...
X Description: Currently, the @sentry/browser SDK has no mechanism to automatically clone the Hub (and, consequently, Scope), unlike @sentry/node that automatically clones a new hub per Node Domain -- and we use ...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/3751
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Better scope management for async code","articleBody":"Currently, the `@sentry/browser` SDK has no mechanism to automatically clone the `Hub` (and, consequently, `Scope`), unlike `@sentry/node` that automatically clones a new hub per Node Domain -- and we use that to isolate state for Node server handling concurrent requests.\r\n\r\nThis means that, in particular for `@sentry/browser`, any kind of async code may end up accidentally mixing up state unless hubs are manually cloned and used per unit of concurrency.\r\n\r\nThis issue tracks improving automatic hub/scope management/propagation in `@sentry/browser`. As a possible solution we may use (an optional support for) Zone.js.\r\n\r\nFor example:\r\n\r\n```js\r\nsetTag('a', 'tag');\r\nwithScope((scope) => {\r\n setTimeout(() => {\r\n captureMessage('test'); // no tags\r\n }, 0);\r\n});\r\ngetCurrentHub().getScope().clear();\r\n```\r\n\r\n```js\r\nhub.withScope(async scope => {\r\n console.log('scope1', scope);\r\n scope.setTag('me', 'you');\r\n hub.withScope(async scope => {\r\n // will have tag me: you\r\n console.log('scope2', scope);\r\n await asyncOperation();\r\n hub.withScope(async scope => {\r\n // won't have tag me: you\r\n console.log('scope3', scope);\r\n await asyncOperation();\r\n });\r\n });\r\n});\r\n```\r\n\r\nThe problem is apparent when some code needs to get something from the current scope, for example here is our `fetch` instrumentation:\r\n\r\nhttps://github.com/getsentry/sentry-javascript/blob/61eda62ed5df5654f93e34a4848fc9ae3fcac0f7/packages/tracing/src/browser/request.ts#L169-L178\r\nhttps://github.com/getsentry/sentry-javascript/blob/61eda62ed5df5654f93e34a4848fc9ae3fcac0f7/packages/tracing/src/utils.ts#L54-L56\r\n\r\nBecause `fetch` is async, the \"current scope\" is not necessarily in the state that one would expect. In the example above, when we call `getActiveTransaction()`, the current hub/scope may have changed and lost reference to the current transaction.","author":{"url":"https://github.com/rhcarvalho","@type":"Person","name":"rhcarvalho"},"datePublished":"2021-06-25T14:05:36Z","url":"https://github.com/getsentry/sentry-javascript/issues/3751"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:e57e951e-84b8-d773-55fa-d8280f82ee01 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86A2:560C3:21608C:2DF043:6B04B712 |
| html-safe-nonce | a71734c18103a0efad7b9607f92d7c041cc556985cbe9918c652ea4534cd62fa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkEyOjU2MEMzOjIxNjA4QzoyREYwNDM6NkIwNEI3MTIiLCJ2aXNpdG9yX2lkIjoiMzAzNjA5NDE1MTIwMTY5OTYwMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fb3067f5a970e0b6583504e19b23d5ddcf8d4dda6bb4578d0f964123f0798545 |
| hovercard-subject-tag | issue:930208020 |
| 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/getsentry/sentry-javascript/3751/issue_layout |
| twitter:image | https://opengraph.githubassets.com/16686353d5cbbf3861274a18dc57237ce3f0c946afa65f65a1d89d627f2a2d88/getsentry/sentry-javascript/issues/3751 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/16686353d5cbbf3861274a18dc57237ce3f0c946afa65f65a1d89d627f2a2d88/getsentry/sentry-javascript/issues/3751 |
| og:image:alt | Currently, the @sentry/browser SDK has no mechanism to automatically clone the Hub (and, consequently, Scope), unlike @sentry/node that automatically clones a new hub per Node Domain -- and we use ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rhcarvalho |
| hostname | github.com |
| expected-hostname | github.com |
| None | 378bd702ac792b3550e484ad39d9a898fcd25bb52ba6962b0c9cc457c1ea71c7 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| 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 | b032e77f1ea437ede6c115446a0a467709b468e7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width