Title: core: Idle span ends early because _restartChildSpanTimeout writes its handle to _idleTimeoutID · Issue #23404 · getsentry/sentry-javascript · GitHub
Open Graph Title: core: Idle span ends early because _restartChildSpanTimeout writes its handle to _idleTimeoutID · Issue #23404 · getsentry/sentry-javascript
X Title: core: Idle span ends early because _restartChildSpanTimeout writes its handle to _idleTimeoutID · Issue #23404 · 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/23404
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"core: Idle span ends early because _restartChildSpanTimeout writes its handle to _idleTimeoutID","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.70.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\nThe defect is in `@sentry/core`'s `startIdleSpan`, so the shortest reproduction is a unit test in this repo.\nSave as `packages/core/test/lib/tracing/idlespan-repro.test.ts` and run\n`cd packages/core && yarn vitest run test/lib/tracing/idlespan-repro.test.ts`:\n\n```ts\nimport { beforeEach, describe, expect, it, vi } from 'vitest';\nimport { getClient, setCurrentClient, spanToJSON, startInactiveSpan } from '../../../src';\nimport { startIdleSpan } from '../../../src/tracing/idleSpan';\nimport { getDefaultTestClientOptions, TestClient } from '../../mocks/client';\nimport { resetGlobals } from '../../testutils';\n\nconst dsn = 'https://123@sentry.io/42';\n\ndescribe('idle span deadline after idleSpanEnableAutoFinish', () => {\n beforeEach(() => {\n vi.useFakeTimers();\n resetGlobals();\n const client = new TestClient(getDefaultTestClientOptions({ dsn, tracesSampleRate: 1 }));\n setCurrentClient(client);\n client.init();\n });\n\n it('keeps the idle span open until idleTimeout after the LAST child ends', () => {\n const idleSpan = startIdleSpan({ name: 'pageload' }, { disableAutoFinish: true, finalTimeout: 99_000 });\n const idleSpanId = idleSpan.spanContext().spanId;\n\n // A child is in flight when the page-load signal arrives (readyState 'interactive').\n const childA = startInactiveSpan({ name: 'child-A' });\n\n vi.advanceTimersByTime(500);\n getClient()!.emit('idleSpanEnableAutoFinish', idleSpan);\n\n vi.advanceTimersByTime(700); // t=1200\n childA!.end(); // idle deadline should now be t=2200\n\n vi.advanceTimersByTime(299); // t=1499\n console.log('t=1499 pageload ended?', spanToJSON(idleSpan).end_timestamp !== undefined);\n\n vi.advanceTimersByTime(2); // t=1501\n console.log('t=1501 pageload ended?', spanToJSON(idleSpan).end_timestamp !== undefined);\n\n // A late child starting inside the lost window, still 400ms before the correct t=2200 deadline.\n vi.advanceTimersByTime(299); // t=1800\n const childB = startInactiveSpan({ name: 'child-B' });\n console.log('t=1800 child-B attached to pageload?', spanToJSON(childB!).parent_span_id === idleSpanId);\n console.log(' child-A was attached to pageload?', spanToJSON(childA!).parent_span_id === idleSpanId);\n\n expect(spanToJSON(idleSpan).end_timestamp).toBeUndefined();\n });\n});\n```\n\n### Steps to Reproduce\n\n1. Check out `develop` (reproduced on b0b09f8) or `10.70.0`, run `yarn && yarn build:dev`.\n2. Add the test file from the section above and run it.\n3. Or read `packages/core/src/tracing/idleSpan.ts` directly:\n - `:99` declares `_idleTimeoutID`\n - `:102` declares `_childSpanTimeoutID`\n - `:230` `_restartIdleTimeout` does `_idleTimeoutID = setTimeout(...)`\n - `:243` `_restartChildSpanTimeout` also does `_idleTimeoutID = setTimeout(...)`\n `_childSpanTimeoutID` is declared and read (`:219`) but never assigned anywhere:\n `grep -n '_childSpanTimeoutID' packages/core/src/tracing/idleSpan.ts`\n4. In the browser this is reached through the pageload span: `browserTracingIntegration.ts:412` passes\n `disableAutoFinish: isPageloadSpan`, and `:423-424` emit `idleSpanEnableAutoFinish` as soon as\n `document.readyState` is `'interactive'` or `'complete'` — often while fetch/xhr child spans are in flight.\n The handler at `idleSpan.ts:392-395` calls `_restartIdleTimeout()` and then, when `activities.size` is\n non-zero, `_restartChildSpanTimeout()`, which overwrites the idle timer's handle.\n\n### Expected Result\n\nThe idle span's deadline is measured from the end of the last child span. With `idleTimeout` at its default\n1000ms and the last child ending at t=1200, the span should stay open until t=2200, so a child started at\nt=1800 is still part of the pageload transaction:\n\nt=1499 pageload ended? false\nt=1501 pageload ended? false\nt=1800 child-B attached to pageload? true\n child-A was attached to pageload? true\n\n### Actual Result\n\nThe span ends at t≈1500 — 1000ms after the auto-finish signal at t=500, not 1000ms after the last child ended\nat t=1200. A child started at t=1800, still 400ms before the correct deadline, is no longer attached to the\npageload span:\n\nt=1499 pageload ended? false\nt=1501 pageload ended? true \u003c-- ends here\nt=1800 child-B attached to pageload? false \u003c-- dropped\n child-A was attached to pageload? true \u003c-- control\n\n### Additional Context\n\nRoot cause\n\n`_restartChildSpanTimeout` (idleSpan.ts:241-249) arms the child-span timeout but stores the handle in\n`_idleTimeoutID` at :243 instead of `_childSpanTimeoutID`. Two consequences:\n\n1. `_childSpanTimeoutID` is never assigned, so `_cancelChildSpanTimeout()` (:218-223) is dead code at both of\n its call sites (:242 and :279).\n2. `_restartChildSpanTimeout` overwrites `_idleTimeoutID` without clearing what it pointed at, orphaning an\n armed idle timer that nothing can cancel. It later fires with `activities.size === 0 &&\n _autoFinishAllowed`, ending the span early.\n\nIn the repro: `_restartIdleTimeout()` at t=500 arms T_idle for t=1500 in `_idleTimeoutID`; the immediately\nfollowing `_restartChildSpanTimeout()` overwrites that handle, so when `_popActivity` runs at t=1200 its\n`_cancelIdleTimeout()` cancels the *child* timer and arms a correct T_idle2 for t=2200 — but the orphaned\nT_idle from t=500 is still armed and fires first.\n\nThe build output confirms the variable is never written: rollup tree-shook it away entirely.\n\n $ grep -c '_childSpanTimeoutID' packages/core/build/cjs/tracing/idleSpan.js\n 0\n $ grep -c '_cancelChildSpanTimeout' packages/core/build/cjs/tracing/idleSpan.js\n 0\n $ grep -n '_idleTimeoutID = setTimeout' packages/core/build/cjs/tracing/idleSpan.js\n 95: _idleTimeoutID = setTimeout(() => {\n 103: _idleTimeoutID = setTimeout(() => {\n\nAlso present in 10.70.0, at idleSpan.ts:235 and :248.\n\nScope\n\n- Only the `idleSpanEnableAutoFinish` path (:392-395) is harmful. The other `_restartChildSpanTimeout` caller,\n `_pushActivity` (:262), is harmless because `_cancelIdleTimeout()` already ran at :256 — which is why no\n existing test catches this.\n- Harm requires that every activity in flight at signal time ends before `signalTime + idleTimeout`. If any is\n still open when the orphan fires, the `activities.size === 0` guard makes it a no-op.\n- The lost window is `lastChildEnd - signalTime`, i.e. 0..idleTimeout — 700ms in this repro, not a flat 1000ms.\n- Transaction *duration* is unaffected: the end timestamp is trimmed to the last child's end either way. The\n damage is child spans that start in the lost window being dropped, and they are not counted in\n `sentry.idle_span_discarded_spans` either.\n- `childSpanTimeout` itself still works today, but only by accident via `_cancelIdleTimeout`. This report is\n about the early idle-span end, not about `childSpanTimeout` being broken.\n\nSuggested fix\n\nOne line — idleSpan.ts:243 becomes `_childSpanTimeoutID = setTimeout(`. `_cancelChildSpanTimeout()` at :242\nand :279 then do their job. Happy to open a PR with a regression test in\npackages/core/test/lib/tracing/idleSpan.test.ts.\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>","author":{"url":"https://github.com/suhailopensource","@type":"Person","name":"suhailopensource"},"datePublished":"2026-08-13T13:50:04Z","url":"https://github.com/getsentry/sentry-javascript/issues/23404"}
| 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:4ef2f8f4-afc5-aee1-3932-737ac8816ccb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CA56:18C3B1:3A24CC:518BE7:6B0479FA |
| html-safe-nonce | 6676bb1aeec09939472457cbcc07f605ad368411fc955871d7154406dad10327 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQTU2OjE4QzNCMTozQTI0Q0M6NTE4QkU3OjZCMDQ3OUZBIiwidmlzaXRvcl9pZCI6IjI1Mjc5ODkyNTA4MTgwNzkyMjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ad8b318228bf1cccda61e3db07750eb525092ca7e27fb826f471669c9a98fd25 |
| hovercard-subject-tag | issue:5142127353 |
| 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/23404/issue_layout |
| twitter:image | https://opengraph.githubassets.com/65ce46f2c7451f5a7031fcadaab0a72efa25048e6f6c444a7c74b3019f6e71b9/getsentry/sentry-javascript/issues/23404 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/65ce46f2c7451f5a7031fcadaab0a72efa25048e6f6c444a7c74b3019f6e71b9/getsentry/sentry-javascript/issues/23404 |
| 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 | suhailopensource |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9c099f1b85def81ca352464c276510dfef32deaf3c305404e65b8d270284a58b |
| 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 | cf73f57448d52b3a7b619195e182b33d3b3454ca |
| 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