Title: profiling-node: profile envelope timestamp breaks Next.js Cache Components prerendering · Issue #26431 · getsentry/sentry-javascript · GitHub
Open Graph Title: profiling-node: profile envelope timestamp breaks Next.js Cache Components prerendering · Issue #26431 · getsentry/sentry-javascript
X Title: profiling-node: profile envelope timestamp breaks Next.js Cache Components prerendering · Issue #26431 · getsentry/sentry-javascript
Description: Is there an existing issue for this? I searched the existing issues and PRs, including #21333, #21351, #21510 and #18700. I reviewed the documentation and contribution guidelines. I reproduced this with the latest published SDK, 11.13.0....
Open Graph Description: Is there an existing issue for this? I searched the existing issues and PRs, including #21333, #21351, #21510 and #18700. I reviewed the documentation and contribution guidelines. I reproduced this...
X Description: Is there an existing issue for this? I searched the existing issues and PRs, including #21333, #21351, #21510 and #18700. I reviewed the documentation and contribution guidelines. I reproduced this...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/26431
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"profiling-node: profile envelope timestamp breaks Next.js Cache Components prerendering","articleBody":"### Is there an existing issue for this?\n\n- [x] I searched the existing issues and PRs, including #21333, #21351, #21510 and #18700.\n- [x] I reviewed the documentation and contribution guidelines.\n- [x] I reproduced this with the latest published SDK, 11.13.0.\n\n### How do you use Sentry?\n\nSentry SaaS (sentry.io).\n\n### Which SDK are you using?\n\n`@sentry/nextjs` with `@sentry/profiling-node` (package: profiling-node).\n\n### SDK Version\n\nOriginally observed with 11.11.0; the deterministic reproduction below also fails with 11.13.0, in both CommonJS and ESM.\n\n### Framework Version\n\nNext.js 16.4.5, `cacheComponents: true`; Node.js 26.13.0, macOS arm64.\n\n### Problem\n\nClosing the last sampled span during an on-demand prerender can synchronously flush a CPU profile. The profiler's separate `createEventEnvelopeHeaders()` still builds `sent_at` with a zero-argument `new Date()`, which reads the render clock and violates Next.js Cache Components' prerender restrictions. The error occurs during on-demand prerendering; the server bundle points to the span-end callback that flushes the profile.\n\nThe current [profiling-node helper](https://github.com/getsentry/sentry-javascript/blob/11.13.0/packages/profiling-node/src/utils.ts#L155-L170) remains separate from core's event envelope helper. Its profile envelope is constructed before `client.sendEnvelope()`, so changing a transport or `beforeEnvelope` hook would be too late.\n\n### Steps to reproduce\n\nThe self-contained example below uses the real native CPU profiler and the actual Next.js SDK safe-context setup. It supplies an isolated client that only collects envelopes, a simulated Next.js work-unit store, and a guarded clock to make the restriction deterministic. It does not start a Next.js server or send telemetry.\n\n1. In an empty directory, install `@sentry/nextjs@11.13.0`, `@sentry/profiling-node@11.13.0`, `next@16.4.5`, `react@19.3.1` and `react-dom@19.3.1` with pnpm. Allow the official `@sentry/node-cpu-profiler` install script if pnpm asks.\n2. Save the following file as `repro.mjs`.\n3. Run `node repro.mjs commonjs` and `node repro.mjs module`.\n\n\u003cdetails>\n\u003csummary>Deterministic native-profiler reproduction\u003c/summary>\n\n```javascript\nimport assert from \"node:assert/strict\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport { createRequire } from \"node:module\";\nimport { performance } from \"node:perf_hooks\";\n\nconst require = createRequire(import.meta.url);\nconst workUnitStorage = new AsyncLocalStorage();\nglobalThis[Symbol.for(\"@next/work-unit-async-storage@profiling-test\")] = workUnitStorage;\nrequire(\"@sentry/nextjs\");\nconst { nodeProfilingIntegration } =\n\tprocess.argv[2] === \"commonjs\" ? require(\"@sentry/profiling-node\") : await import(\"@sentry/profiling-node\");\nconst hooks = new Map();\nconst envelopes = [];\nconst integration = nodeProfilingIntegration();\n\nintegration.setup({\n\tgetOptions: () => ({ profileSessionSampleRate: 1, profileLifecycle: \"trace\" }),\n\ton(name, callback) {\n\t\thooks.set(name, callback);\n\t\treturn () => hooks.delete(name);\n\t},\n\tgetDsn: () => undefined,\n\tgetSdkMetadata: () => undefined,\n\tsendEnvelope(envelope) {\n\t\tenvelopes.push(envelope);\n\t\treturn Promise.resolve({ statusCode: 200 });\n\t},\n});\n\nconst span = {\n\tspanContext: () => ({ traceId: \"1\".repeat(32), spanId: \"2\".repeat(16), traceFlags: 1 }),\n};\nconst OriginalDate = Date;\nconst startedAt = OriginalDate.now();\n\nglobalThis.Date = new Proxy(OriginalDate, {\n\tconstruct(target, args, newTarget) {\n\t\tif (args.length === 0) {\n\t\t\tassert.equal(workUnitStorage.getStore(), undefined, \"Profiling must not read new Date() during prerender\");\n\t\t}\n\t\treturn Reflect.construct(target, args, newTarget);\n\t},\n\tget(target, property, receiver) {\n\t\tif (property === \"now\") {\n\t\t\treturn () => {\n\t\t\t\tassert.equal(workUnitStorage.getStore(), undefined, \"Profiling must not read Date.now() during prerender\");\n\t\t\t\treturn OriginalDate.now();\n\t\t\t};\n\t\t}\n\t\treturn Reflect.get(target, property, receiver);\n\t},\n});\n\ntry {\n\tworkUnitStorage.run(\"prerender\", () => {\n\t\thooks.get(\"spanStart\")(span);\n\t\tconst start = performance.now();\n\t\tlet work = 0;\n\t\twhile (performance.now() - start \u003c 40) {\n\t\t\twork += Math.sin(work + 0.1);\n\t\t}\n\t\thooks.get(\"spanEnd\")(span);\n\t});\n} finally {\n\tglobalThis.Date = OriginalDate;\n}\n\nassert.equal(envelopes.length, 1);\nconst sentAt = envelopes[0][0].sent_at;\nassert.ok(Date.parse(sentAt) >= startedAt && Date.parse(sentAt) \u003c= Date.now());\nprocess.stdout.write(JSON.stringify({ type: envelopes[0][1][0][0].type, sentAt }));\n```\n\n\u003c/details>\n\n### Expected result\n\nThe profile can be flushed without reading the ambient prerender clock directly. A valid `profile_chunk` envelope is collected with an ISO `sent_at` timestamp. Profiling and the page's rendering strategy stay unchanged.\n\n### Actual result\n\nBoth module formats fail with:\n\n```text\nAssertionError [ERR_ASSERTION]: Profiling must not read new Date() during prerender\n at createEventEnvelopeHeaders\n at makeProfileChunkEnvelope\n at ContinuousProfiler._flush\n at ContinuousProfiler._stopChunkProfiling\n```\n\n### Related fixes and why this case is still uncovered\n\n- #21333 was fixed by #21351 in 10.57.0. That fix changed clock reads in **core** and added guarded-clock coverage there. It did not change profiling-node's separate helper for `profile_chunk` envelopes, so upgrading past that release does not cover this path.\n- #21510, fixed by #21573 in 10.59.0, concerns safe random-ID setup in build prerender workers. Here the Next.js safe runner is registered and active; the remaining read is a bare `new Date()` in profiling-node.\n- #25315 improves how the Next.js SDK exits its render store on Next.js 16.4+ and handles Cloudflare request lifetimes. The reproduction uses the current Next.js runner; profiling-node bypasses it for this timestamp.\n\n### Proposed approach and request for a maintainer's go-ahead\n\nI would like to contribute a small draft PR replacing this timestamp read with `new Date(safeDateNow()).toISOString()`, importing the existing core helper through its internal export, plus regression coverage for the profiling envelope under a guarded clock.\n\nThis follows the responsibility split described in #18700: the Next.js SDK installs the safe runner, and shared SDK code uses the clock/random helpers without importing Next.js. The [discussion with gnoff](https://github.com/getsentry/sentry-javascript/pull/18700#issuecomment-3719913841) also cautions against broad context escapes that can hide non-determinism affecting rendered or cached values. Here the proposed escape is limited to the telemetry envelope timestamp. I am treating that discussion as design precedent, not as prior approval for this change.\n\nPer [External contributors](https://github.com/getsentry/sentry-javascript/blob/develop/CONTRIBUTING.md#external-contributors), I have not written the SDK patch or opened a PR. Would a maintainer be happy for me to prepare this focused fix against `develop`?\n","author":{"url":"https://github.com/JavierCane","@type":"Person","name":"JavierCane"},"datePublished":"2026-11-16T16:38:55Z","url":"https://github.com/getsentry/sentry-javascript/issues/26431"}
| 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:f0c8bd94-8877-2b6c-e65d-f8e56f447f79 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A524:393E49:FA3B5:15E74B:6B042FE3 |
| html-safe-nonce | b553f1bfef8ee710024ad3d6690dfb2741fec357e1a517bd4b88b2f4d2a60108 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTI0OjM5M0U0OTpGQTNCNToxNUU3NEI6NkIwNDJGRTMiLCJ2aXNpdG9yX2lkIjoiNDE2MzIwMTM4MTk0MTEyOTE4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | eabbe804479df761775bfc0a928d9e96438dc5f8a8156d5bc235e5dc14c935e9 |
| hovercard-subject-tag | issue:6425943673 |
| 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/26431/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ddac7c3417bc49b8cf8b901a175b81dfa5c572780e26f1d22cc5b2f84d369540/getsentry/sentry-javascript/issues/26431 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ddac7c3417bc49b8cf8b901a175b81dfa5c572780e26f1d22cc5b2f84d369540/getsentry/sentry-javascript/issues/26431 |
| og:image:alt | Is there an existing issue for this? I searched the existing issues and PRs, including #21333, #21351, #21510 and #18700. I reviewed the documentation and contribution guidelines. I reproduced this... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JavierCane |
| hostname | github.com |
| expected-hostname | github.com |
| None | b7a1d2b9101496921b08579fbee988d014345aef2f5bd660d1f8c7332d81560e |
| 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 | c3dd496c3da6ea6b77e69c09279d2b81604654f1 |
| 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