#breadcrumbs are leaking into different contexts when i throw an error.
12 messages · Page 1 of 1 (latest)
Hi @scarlet slate. I'm happy to dig into this for you! I'll check my internal sources for answers to your question! :wave:
The knowledge sources confirm that breadcrumb and scope leakage between requests is a known issue in some JavaScript/serverless setups, especially in environments like AWS Lambda and Next.js, and that it’s related to how global scope and async context are handled. [Corrupt breadcrumbs]
For Node-based frameworks (Express, Next.js API routes, serverless handlers, etc.), the Sentry JavaScript SDK now uses Node’s AsyncLocalStorage to automatically isolate scope and breadcrumbs per request. [Node async context] This same mechanism is documented for other serverless environments like AWS Lambda, Azure Functions, and GCP Functions. [AWS Lambda async; GCP Functions async]
Key points from the docs that apply to your case:
-
Per-request isolation is automatic
By default, each request should get its own scope and breadcrumbs; they should not be shared between requests. [Node async context] -
Manual isolation for special code paths
If you have background jobs, long-running tasks, or custom webhook handling that might run outside the normal request lifecycle, you can explicitly isolate Sentry state usingwithIsolationScope:const Sentry = require("@sentry/node");
async function handleWebhook(event) {
return await Sentry.withIsolationScope(async () => {
// Everything inside here is isolated:
// breadcrumbs, tags, user, etc. won't leak to other requests
// your webhook logic here
});
}
[[Node async context](<https://docs.sentry.io/platforms/javascript/guides/node/configuration/async-context/>)]
3. **Serverless/global-scope caveats**
In serverless environments (like Vercel/Next.js API routes or edge/serverless functions), re-used execution environments can cause global state to persist between invocations. The GitHub issue you’re effectively describing notes that breadcrumbs “may leak right now” and that this was planned to be fixed in a major SDK update. [[Corrupt breadcrumbs](<https://github.com/getsentry/sentry-javascript/issues/9717>)]
Given what’s in the sources, there isn’t a Next.js–specific extra flag documented beyond the general async-context behavior. The most concrete, documented mitigation you can apply today is:
- Ensure you’re on a recent `@sentry/nextjs` version where async context isolation is in place.
- Wrap webhook handling (and any other serverless handler where you see leakage) in `Sentry.withIsolationScope` so that breadcrumbs and scope are explicitly isolated for that execution. [[Node async context](<https://docs.sentry.io/platforms/javascript/guides/node/configuration/async-context/>)]
If, after doing that, you still see leakage, the knowledge sources don’t provide further Next.js-specific configuration knobs, and it may be an SDK bug similar to the one described in the GitHub issue.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@scarlet slate Our robots are still learning and improving, please let us know if this response was correct by reacting below.
@pliant jolt I've tried Sentry.withIsolationScope but context is still corrupting other breadcrumbs. Within my route i isolate scope and then get the isolation scope within the inngest function but breadcrumb corruption is still happening
Let me check the knowledge base for you again. One moment! :books:
Let me check the knowledge base for you again. One moment! :books:
Yes — and I already did in the previous message, but I’ll clarify the current state based on the docs and issue you shared.
From the available sources:
- Breadcrumb leakage between requests/contexts is a known problem in the JS SDK, especially in Lambda-like/serverless environments; Sentry explicitly notes that “breadcrumbs are a bit buggy at the moment and they may leak right now” and that this is tied to global state in the SDK architecture. [Corrupt breadcrumbs]
- The intended solution is per-request isolation via async context plus
Sentry.withIsolationScope, which should isolate scope and breadcrumbs for a given request or background job. [Request isolation; Scopes]
You’ve already tried withIsolationScope and are still seeing corruption. The knowledge sources:
- Do not document any additional Next.js- or Inngest-specific configuration beyond using the standard async context and
withIsolationScope. - Do not provide a documented workaround for the case where
withIsolationScopeis used correctly but breadcrumbs still leak.
So, based strictly on the docs and the GitHub issue, all I can reliably say is:
- Make sure you’re on a recent
@sentry/nextjsversion where async context/request isolation is implemented. [Request isolation] - Wrap the webhook / background job logic in
Sentry.withIsolationScope(which you’ve already done). [Scopes]