Title: `wrapHandler` shares one isolation scope across Lambda invocations, so `setUser` and breadcrumbs leak · Issue #26180 · getsentry/sentry-javascript · GitHub
Open Graph Title: `wrapHandler` shares one isolation scope across Lambda invocations, so `setUser` and breadcrumbs leak · Issue #26180 · getsentry/sentry-javascript
X Title: `wrapHandler` shares one isolation scope across Lambda invocations, so `setUser` and breadcrumbs leak · Issue #26180 · getsentry/sentry-javascript
Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using the latest SDK release https://github.com/...
Open Graph Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th...
X Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/26180
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`wrapHandler` shares one isolation scope across Lambda invocations, so `setUser` and breadcrumbs leak","articleBody":"### Is there an existing issue for this?\n\n- [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues\n- [x] I have reviewed the documentation https://docs.sentry.io/\n- [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases\n\n### How do you use Sentry?\n\nSentry Saas (sentry.io)\n\n### Which SDK are you using?\n\n@sentry/aws-serverless\n\n### SDK Version\n\n11.11.0\n\n### Framework Version\n\n_No response_\n\n### Link to Sentry event\n\n_No response_\n\n### Reproduction Example/SDK Setup\n\n```js\n// repro.mjs\nimport * as Sentry from '@sentry/aws-serverless';\n\nconst events = [];\n\nSentry.init({\n dsn: 'https://public@example.com/1',\n beforeSend(event) {\n events.push(event);\n return null;\n },\n});\n\nconst handler = Sentry.wrapHandler(async event => {\n if (event.user) {\n Sentry.setUser({ id: event.user });\n Sentry.addBreadcrumb({ message: `handled request for ${event.user}` });\n } else {\n Sentry.captureException(new Error('anonymous request failed'));\n }\n});\n\nconst context = {\n functionName: 'repro',\n functionVersion: '$LATEST',\n invokedFunctionArn: 'arn:aws:lambda:us-east-1:123456789012:function:repro',\n logGroupName: '/aws/lambda/repro',\n logStreamName: 'stream',\n getRemainingTimeInMillis: () => 3000,\n};\n\n// Two invocations on the same warm container\nawait handler({ user: 'alice' }, { ...context, awsRequestId: '1' });\nawait handler({}, { ...context, awsRequestId: '2' });\n\nconst [event] = events;\nconsole.log({\n user: event.user,\n breadcrumbs: event.breadcrumbs?.map(breadcrumb => breadcrumb.message),\n awsRequestId: event.contexts['aws.lambda'].aws_request_id,\n});\n```\n\n### Steps to Reproduce\n\n1. `npm install @sentry/aws-serverless@11.11.0`\n2. `node repro.mjs`. It runs two invocations of one wrapped handler in the same process, like a warm execution environment does. The first sets a user and a breadcrumb, and the second captures an error.\n\n### Expected Result\n\nThe second invocation's event has neither the first invocation's user nor its breadcrumb:\n\n```\n{ user: undefined, breadcrumbs: undefined, awsRequestId: '2' }\n```\n\n### Actual Result\n\n```\n{\n user: { id: 'alice' },\n breadcrumbs: [ 'handled request for alice' ],\n awsRequestId: '2'\n}\n```\n\n### Additional Context\n\n`setUser` and `addBreadcrumb` write to the isolation scope ([exports.ts](https://github.com/getsentry/sentry-javascript/blob/5f9835b405b1f08189d0e59ea0749a6b82d3e289/packages/core/src/exports.ts#L153-L155), [breadcrumbs.ts](https://github.com/getsentry/sentry-javascript/blob/5f9835b405b1f08189d0e59ea0749a6b82d3e289/packages/core/src/breadcrumbs.ts#L20-L22)), but `wrapHandler` only forks the current scope for each invocation ([sdk.ts#L229-L231](https://github.com/getsentry/sentry-javascript/blob/5f9835b405b1f08189d0e59ea0749a6b82d3e289/packages/aws-serverless/src/sdk.ts#L229-L231), and [#L277](https://github.com/getsentry/sentry-javascript/blob/5f9835b405b1f08189d0e59ea0749a6b82d3e289/packages/aws-serverless/src/sdk.ts#L277) for streaming handlers). There's no incoming HTTP request for the HTTP integration to fork on, so nothing else forks the isolation scope either. A warm execution environment keeps one isolation scope for its whole life: the user, tags and breadcrumbs from earlier invocations end up on later events, and breadcrumbs keep piling up to `maxBreadcrumbs`. The auto-instrumentation path (`instrumentHandler`) goes through `wrapHandler` too.\n\nThe timeout warning is affected in a related way. Its timer starts [before the fork](https://github.com/getsentry/sentry-javascript/blob/5f9835b405b1f08189d0e59ea0749a6b82d3e289/packages/aws-serverless/src/sdk.ts#L197), so the warning gets whatever the shared isolation scope holds (e.g. the previous invocation's user) and has no `aws.lambda` context.\n\n`@sentry/google-cloud-serverless` already forks the isolation scope per invocation ([`domainify`](https://github.com/getsentry/sentry-javascript/blob/5f9835b405b1f08189d0e59ea0749a6b82d3e289/packages/google-cloud-serverless/src/utils.ts#L8-L10)), and `withIsolationScope` is [documented](https://github.com/getsentry/sentry-javascript/blob/5f9835b405b1f08189d0e59ea0749a6b82d3e289/packages/core/src/currentScopes.ts#L112-L113) as meant for SDKs rather than application code, so this seems like the wrapper's job. Until then, wrapping the handler body yourself works around it: `Sentry.wrapHandler((event, context) => Sentry.withIsolationScope(() => handler(event, context)))`.\n\nSuggested fix: in `wrapHandler` and `wrapStreamingHandler`, use `withIsolationScope` instead of `withScope` (it forks the current scope too), and start the timeout-warning timer inside that callback. With that change applied to 11.11.0's build, the repro prints the expected result, and each invocation's timeout warning carries that invocation's own user and `aws.lambda` context. I have a patch with tests and am happy to open a PR.\n\n### Priority\n\n\u003csub>React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding `+1` or `me too`, to help us triage it.\u003c/sub>\n","author":{"url":"https://github.com/abrahamguo","@type":"Person","name":"abrahamguo"},"datePublished":"2026-11-06T13:42:45Z","url":"https://github.com/getsentry/sentry-javascript/issues/26180"}
| 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:b99a9e7d-8279-59f4-0bc9-86a110666f00 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B4C4:1F98:29EFE3F:380274B:6B0455D8 |
| html-safe-nonce | d0644a964ab5ef2ae397aa1324c75de43d5215fa27beeda0d8f430000ee37aa7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNEM0OjFGOTg6MjlFRkUzRjozODAyNzRCOjZCMDQ1NUQ4IiwidmlzaXRvcl9pZCI6IjI3MjgyNzc0MjcxMDAwNzk1NzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d591e165037d1ce73e73684a7942ec2f8b046285e3421418ffda4e6d4e587d00 |
| hovercard-subject-tag | issue:6243727379 |
| 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/26180/issue_layout |
| twitter:image | https://opengraph.githubassets.com/66f1880fc950f316ed81fe3ec1d3550fc233c8105b8506285c27f5466aafc17a/getsentry/sentry-javascript/issues/26180 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/66f1880fc950f316ed81fe3ec1d3550fc233c8105b8506285c27f5466aafc17a/getsentry/sentry-javascript/issues/26180 |
| og:image:alt | Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abrahamguo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0 |
| 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 | e086f9b7e88322eb2b41885a6ad425b215737fa5 |
| 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