sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Using `@sentry/nextjs` and `@sentry/cloudflare` together crashes every request with `context.getValue is not a function`

Open JPeer264 opened this issue 2 months ago • 4 comments

Description

A Next.js app on Cloudflare Workers that initializes @sentry/nextjs in instrumentation.ts and also wraps the Worker entry with @sentry/cloudflare's withSentry fails on every request:

TypeError: e.getValue is not a function
    at getScopesFromContext
    at getScopes
    ...
    at _onSpanEnded
    at end

Seen with @sentry/[email protected] and @sentry/[email protected], which share the same @sentry/core.

Root cause

The two SDKs install different async context strategies, and the second one reuses the AsyncLocalStorage of the first even though the two strategies put different things into it:

Strategy Installed by ALS store
setAsyncLocalStorageAsyncContextStrategy (packages/server-utils/src/async-context.ts) @sentry/cloudflare (withSentry) { scope, isolationScope }
setOpenTelemetryContextAsyncContextStrategy (packages/opentelemetry/src/asyncContextStrategy.ts) @sentry/nextjs edge build (via @sentry/vercel-edge) an OpenTelemetry Context, also read by the global context manager it registers

Both pick up an existing store like this, without checking which strategy created it (added in #22889):

const existing = getAsyncContextStrategy(getMainCarrier()).getTracingChannelBinding?.()?.asyncLocalStorage;
const asyncLocalStorage = existing ?? new AsyncLocalStorage();

Once the stores are shared, api.context.active() returns the plain { scope, isolationScope } object, and getScopesFromContext(ctx) (or trace.getSpan(ctx)) calls ctx.getValue() on it. It fails in both install orders.

Reproduction

No framework needed:

import { getCurrentScope, withIsolationScope } from '@sentry/core';
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/cloudflare';
import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';

setAsyncLocalStorageAsyncContextStrategy();
withIsolationScope(() => {
  setOpenTelemetryContextAsyncContextStrategy();
  getCurrentScope(); // TypeError: context.getValue is not a function
});

The reverse order fails the same way:

import { context, trace } from '@opentelemetry/api';

setOpenTelemetryContextAsyncContextStrategy();
setAsyncLocalStorageAsyncContextStrategy();
withIsolationScope(() => {
  trace.getSpan(context.active()); // TypeError: context.getValue is not a function
});

Expected

A strategy only reuses an AsyncLocalStorage that holds the store shape it expects, for example by tagging the binding with its store type and creating a fresh ALS on a mismatch.

This only stops the crash. Two SDKs in one runtime still end up with one active strategy and one current client, so combining @sentry/nextjs and @sentry/cloudflare stays unsupported. It would still be better to fail soft than to break every request. The same clash can happen with any other pair of SDKs that use different strategies.

JPeer264 avatar Sep 22 '26 17:09 JPeer264

JS-3751

linear-code[bot] avatar Sep 22 '26 17:09 linear-code[bot]

I reproduced both snippets on develop at b7227bb6, using fresh package tarballs on Node 20.19.5. ESM and CommonJS both fail with context.getValue is not a function. Same-strategy reinitialization and async scope isolation pass.

If nobody is working on this, I'd like to mark each store's type and reuse only matching stores, with regression tests for both orders. Mixed-SDK support and replacing the global OTel manager would stay out of scope.

Should bindings without a marker be treated as incompatible? Also, OTel → plain → OTel currently shares one store. Allocating a new store can leave the existing global manager pointing elsewhere. Is that unsupported sequence outside this fix?

ihsraham avatar Sep 24 '26 09:09 ihsraham

Hey thanks a lot for volunteering. You can definitely pick this up. I just want to highlight that we have rather high priority on this since we'll improve our runtime + framework compatibility.

I'd like to mark each store's type and reuse only matching stores

I haven't looked into a solution, but a marker might sound like a possible fix.

Should bindings without a marker be treated as incompatible? Also, OTel → plain → OTel currently shares one store. Allocating a new store can leave the existing global manager pointing elsewhere. Is that unsupported sequence outside this fix?

It is definitely not wanted to create a new store. This page shows exactly what should be supported and how it should work out together: https://docs.sentry.io/platforms/javascript/guides/nextjs/opentelemetry/ (hope that is enough information to start on this ticket).

If you need more help or assistance just reach out

JPeer264 avatar Sep 24 '26 11:09 JPeer264

Closing. Reason is here

JPeer264 avatar Sep 28 '26 07:09 JPeer264