Skip to content

About

Repro: Next 16 cacheComponents prerender-random via OTel ProxyTracer bypass of #82350, + Sentry captureException at build

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Next.js cacheComponents × OpenTelemetry/Sentry prerender-random repro

Minimal reproduction for two related bugs where third-party randomness fails cacheComponents prerender validation. In production apps (Next 16 + @sentry/nextjs on Vercel) this breaks every ISR revalidation of PPR pages — shells go permanently stale, on-demand renders can 500, and Sentry reports it as a message-less Error: null (StaticGenBailoutError has no message; the real reason only appears in server logs).

Versions: [email protected], @opentelemetry/[email protected], @opentelemetry/[email protected], @sentry/[email protected], Node 24.

pnpm install

Bug 1 — span creation bypasses the #82350 tracer patch (next-prerender-random)

vercel/next.js#82350 ("Allow span creation while prerendering") deliberately allows OTel spans during prerender by patching trace.getTracerProvider().getTracer so span creation exits workUnitAsyncStorage.

The patch is bypassed for any tracer acquired before the delegate is registered on the global ProxyTracerProvider — which is the standard constructor pattern of OTel instrumentation libraries: InstrumentationAbstract's constructor calls this._tracer = trace.getTracer(...) directly (source). Every @sentry/node integration (pg, undici, http, …) goes through it, and @sentry/node's init order guarantees the race: _init first runs nodeCore.init() (sets up integrations → constructs instrumentations → trace.getTracer() while no delegate exists), and only then initOpenTelemetry(client) registers the NodeTracerProvider. This applies equally to @sentry/nextjs, whose server entry delegates to @sentry/node's init.

Such a tracer is a ProxyTracer; its _getTracer() resolves the real tracer via ProxyTracerProvider.getDelegateTracer() (source), which the patch does not wrap. Span-id generation then calls RandomIdGenerator's raw Math.random() inside the prerender scope and the route fails validation. (Note the ProxyTracer.startSpan frame in the real-world stack below — only possible for a tracer acquired before the delegate existed.)

Reproduce

pnpm dev
# visit or curl:
curl -s http://localhost:3000/early-span   # BUG
curl -s http://localhost:3000/late-span    # control: works

Server output for /early-span:

Error: Route "/early-span" used `Math.random()` before accessing either uncached data (e.g. `fetch()`) or Request data (e.g. `cookies()`, `headers()`, `connection()`, and `searchParams`). [...]
    at EarlySpanPage (app/early-span/page.tsx:22:25)

/late-span does the exact same startSpan with a tracer acquired at render time (after the patch) and prerenders fine — the only difference is when trace.getTracer() was called.

instrumentation.ts simulates the instrumentation-library pattern with no Sentry involved: it acquires a tracer, then registers a plain NodeTracerProvider.

Real-world stack (Next 16.2.9 + @sentry/nextjs 10.57.0, pg integration)

Error: Route "/product/[category]" used `Math.random()` before accessing either uncached data [...]
    at RandomIdGenerator.generateId [as generateSpanId] (@opentelemetry/sdk-trace-base/src/platform/node/RandomIdGenerator.ts:31:41)
    at Tracer.startSpan (@opentelemetry/sdk-trace-base/src/Tracer.ts:82:38)
    at ProxyTracer.startSpan (@opentelemetry/api/src/trace/ProxyTracer.ts:41:30)
    at BoundPool.connect (@sentry/node/src/integrations/tracing/postgres/vendored/instrumentation.ts:549:36)
    at fetchFAQ (src/payload/faq.ts:11:16)

In production this fires on every background revalidation of a partially prerendered route that touches an instrumented resource (here: a Postgres query in the static shell), so the shell never refreshes.

Not specific to Sentry — reproduces with @vercel/otel too

The bypass is provider-agnostic. Re-run the dev repro with @vercel/otel registering the delegate provider:

REPRO_MODE=vercel-otel pnpm dev
curl -s http://localhost:3000/early-span   # still BUG
curl -s http://localhost:3000/late-span    # still fine

Plain registerOTel('next-app') by itself is unaffected (Next's internal spans and @vercel/otel's built-in fetch instrumentation go through the patched path — consistent with #72024 being fixed by #82350). But the documented pattern of passing custom instrumentations, registerOTel({ instrumentations: [new PgInstrumentation()] }), constructs them in user code before registration — same bypass.

Suggested fix

Also wrap ProxyTracerProvider.getDelegateTracer (or wrap the delegate provider's getTracer at setDelegate time) in the #82350 patch.

Bug 2 — captureException during build prerender (next-prerender-crypto)

next build fails when Sentry.captureException is reachable from a prerendered scope (here: generateMetadata reporting a recoverable data problem):

pnpm build
Error: Route "/capture-metadata" used `crypto.randomUUID()` before accessing either uncached data [...]
Error occurred prerendering page "/capture-metadata".

Sentry wraps its randomness (uuid4 → withRandomSafeContext, getsentry/sentry-javascript#18700), but the runner under Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__') is only registered by Sentry.init() — and build prerender workers never load instrumentation.ts, so the runner doesn't exist there and crypto.randomUUID() runs unwrapped inside the prerender scope.

(That build workers don't run instrumentation is also observable in Bug 1: pnpm build with app/capture-metadata removed succeeds because __earlyTracer is never set at build time. Bug 1 is runtime-only; Bug 2 is build-only.)

Related: getsentry/sentry-javascript#21333 fixed the Date.now() flavor of this in v10.57.0, but not this build-time crypto.randomUUID() path.

Workarounds we ship in production

  • Register a cold-start AsyncLocalStorage.snapshot() under Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__') and wrap Math.random with it (escapes the work-unit scope for OTel span ids).
  • Wrap capture() call sites reachable from prerendered scopes in workUnitAsyncStorage.exit() (build workers don't load instrumentation, so the escape must live at the call site).

About

Repro: Next 16 cacheComponents prerender-random via OTel ProxyTracer bypass of #82350, + Sentry captureException at build

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages