Title: Memory leak, instrumentXHR `virtualError` retains every XMLHttpRequest when requests are chained from readystatechange · Issue #24249 · getsentry/sentry-javascript · GitHub
Open Graph Title: Memory leak, instrumentXHR `virtualError` retains every XMLHttpRequest when requests are chained from readystatechange · Issue #24249 · getsentry/sentry-javascript
X Title: Memory leak, instrumentXHR `virtualError` retains every XMLHttpRequest when requests are chained from readystatechange · Issue #24249 · 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/24249
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Memory leak, instrumentXHR `virtualError` retains every XMLHttpRequest when requests are chained from readystatechange","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/browser\n\n### SDK Version\n\n10.73.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\nMinimal reproduction can be found here https://github.com/augustalfredsson/sentry-xhr-leak-repro\n\nThe reproduction is one HTML page and a Node server. The page loads the Sentry browser bundle from the CDN, calls\n`Sentry.init` with the default integrations, and runs four request chains. Each chain opens the next\n`XMLHttpRequest` from the `readystatechange` callback of the previous one:\n\n```js\nconst next = () => {\n const xhr = new XMLHttpRequest();\n xhr.open(\"GET\", \"/api\", true);\n xhr.onreadystatechange = () => {\n if (xhr.readyState === 4) {\n next(); // open the next request on the same call stack\n }\n };\n xhr.send();\n};\nnext();\n```\n\n```js\nSentry.init({\n dsn: \"https://examplePublicKey@o0.ingest.sentry.io/0\",\n beforeSend: () => null, // the page never sends events\n});\n```\n\nThis is a common pattern: polling loops, chunked uploads, chained REST calls, and hls.js Low-Latency HLS playlist\nreloads.\n\n### Steps to Reproduce\n\n1. Run `node server.js`, then open `http://localhost:8787/?sentry=on` in Chrome and press Start.\n2. The page shows the size of the JS heap, but you can also see it in DevTools -> Memory panel and take a heap snapshot after 30 seconds. Filter the class list by `XMLHttpRequest`.\n3. Wait one minute and you'll see that the JS heap size keeps growing. Take a second snapshot if you took one earlier. Compare the count and the heap size.\n4. Open `http://localhost:8787/?sentry=off`, press Start, and repeat. This is the control without the Sentry\nbundle. You'll see that the JS heap size doesn't grow.\n\nOr run `node verify.js 40`, which does the same in headless Chromium and counts live `XMLHttpRequest` objects\nthrough CDP `Runtime.queryObjects`.\n\n### Expected Result\n\nA completed `XMLHttpRequest` becomes garbage after its callbacks have run and the JS heap size doesn't grow.\n\n### Actual Result\n\nEvery completed request stays alive for the life of the page. Here's a headless run of 40 seconds with four chains and 8 KB\nresponses:\n\n```\nsentry=on: completed 224991 requests. Live XMLHttpRequest: 15591 -> 229113. Heap: 28.2 MB -> 400.7 MB\nsentry=off: completed 242321 requests. Live XMLHttpRequest: 4 -> 8. Heap: 0.7 MB -> 0.7 MB\n```\n\nThe live `XMLHttpRequest` count equals the number of completed requests. Each retained request also pins its\n`responseText` and everything the app's callback closures reference.\n\n### Additional Context\n\n**What we think happens** \nIn `packages/browser-utils/src/instrument/xhr.ts`, the `open` wrapper\ncreates `const virtualError = new Error()` for every request and never releases it. The\n`setRequestHeader` Proxy that the wrapper puts on the XHR instance lives in the same\nfunction scope, so it keeps `virtualError` alive for as long as the XHR is alive. Nobody\nreads `virtualError.stack`, so V8 keeps the raw stack frames, and each raw frame holds its\nreceiver object. When request N+1 is opened inside the callback of request N, one of those\nframes has request N as its receiver. So request N+1 retains request N, which retains\nrequest N-1, and so on. The request in flight is a Blink pending activity and thus a GC\nroot, and the whole chain hangs from it.\n\nThe path from a heap snapshot:\n\n```\nBlink \"Pending activities\" (the one in-flight XHR)\n-> XMLHttpRequest#N\n-> own property `setRequestHeader` (Proxy)\n-> Proxy `apply` closure -> closure context of the `open` handler\n-> `virtualError` -> raw stack frames\n-> frame receiver = XMLHttpRequest#N-1\n-> ... -> XMLHttpRequest#N-2 -> ...\n```\n\n**Related PR** \n#22216 (10.66.0) removes the `readystatechange` listener after the request\ncompletes. That does not break this chain, because the `setRequestHeader` Proxy still holds\nthe context.\n\n**Possible fixes** \nAny one of these breaks the chain:\n\n- Set `virtualError = undefined` after the `readyState === 4` handler ran.\n- Create the `setRequestHeader` Proxy in its own function, so it does not share a closure\ncontext with `virtualError`.\n- Keep only the formatted `stack` string, or hold `virtualError` in a `WeakRef`.\n\n**Workaround**\nTurn off the XHR instrumentation. Both default integrations call\n`instrumentXHR`, so both options are needed. This loses XHR breadcrumbs.\n\n```js\nintegrations: [\n Sentry.breadcrumbsIntegration({ xhr: false }),\n Sentry.browserApiErrorsIntegration({ XMLHttpRequest: false }),\n],\n```\n\n**Environment** \nChrome 151.0.7922.176, macOS 26.6.2. Headless numbers from Chromium 1200\n(Playwright 1.57) on macOS.\n\n### Priority\n\n_No response_","author":{"url":"https://github.com/augustalfredsson","@type":"Person","name":"augustalfredsson"},"datePublished":"2026-09-09T13:17:39Z","url":"https://github.com/getsentry/sentry-javascript/issues/24249"}
| 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:4fa49363-e22e-61d9-e9e1-27e6afd92372 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8082:3B1B50:7AC4A5:A2B4BF:6B04CC56 |
| html-safe-nonce | 74ff07c4e9b5741d23e6db4bdbebb317fa2d0037fd2663ef9989ee86acfcdf94 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDgyOjNCMUI1MDo3QUM0QTU6QTJCNEJGOjZCMDRDQzU2IiwidmlzaXRvcl9pZCI6IjM3NTYyNDc3ODAzMzE2NzA2MTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 250461c8cd233a28d6f5f48550a183c940b6f8b7d912ca1276dbcfeb06f41663 |
| hovercard-subject-tag | issue:5399945495 |
| 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/24249/issue_layout |
| twitter:image | https://opengraph.githubassets.com/896f766adb977476e02f3619bf19e9512874bd565a5bb584d6699d9c38e081ac/getsentry/sentry-javascript/issues/24249 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/896f766adb977476e02f3619bf19e9512874bd565a5bb584d6699d9c38e081ac/getsentry/sentry-javascript/issues/24249 |
| 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 | augustalfredsson |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27ed421e5c2dac040a37529b5b9191edcc4a3de70521471e81fb4b4d9dc2aa7d |
| 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 | 63dc19014c621c67306d7899c04c78dcd0d95803 |
| 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