Skip to content

Sentry.AspNet shares one scope stack between all threads without an HttpContext #6092

Backlog

Description

@ric-oliv

Version

Sentry.AspNet 7.1.0 on .NET Framework, with AddAspNet(). The code is unchanged since #1180.

What happens

AddAspNet() always sets HttpContextScopeStackContainer as the scope stack container. When HttpContext.Current is null, that container reads and writes FallbackStack, which is one field for the whole app. So every thread without an HttpContext shares a single scope stack, including background threads, timers and Hangfire workers.

A scope that one of those threads pushes becomes the current scope on all the others. An event captured on one thread can carry the tags, user and breadcrumbs another thread set. A thread that disposes its scope can also pop a scope that another thread pushed. Anything that pushes a scope outside a request runs into this, such as SentrySdk.PushScope() or ILogger.BeginScope with Sentry's logger provider.

#1180 added the fallback so the SDK doesn't throw when it touches the scope before any request exists, see #1157.

#6074 will make this more common, because Sentry.Hangfire then pushes a scope for every job and Hangfire runs up to 20 workers by default. Under AddAspNet(), concurrent jobs keep sharing scope data, and an event from one job can carry another job's tags and trace ID.

Expected

Each thread without an HttpContext keeps its own scope stack, as it does without AddAspNet(). A scope pushed on one thread doesn't change what events on other threads carry.

Steps to reproduce

Run this on .NET Framework outside a request, for example in a console app:

SentrySdk.Init(options =>
{
    options.Dsn = "<your DSN>";
    options.AddAspNet();
});

var barrier = new Barrier(2);

void Worker(string name)
{
    using (SentrySdk.PushScope())
    {
        SentrySdk.ConfigureScope(scope => scope.SetTag("worker", name));
        barrier.SignalAndWait();
        SentrySdk.CaptureMessage($"from {name}");
        barrier.SignalAndWait();
    }
}

var a = new Thread(() => Worker("A"));
var b = new Thread(() => Worker("B"));
a.Start();
b.Start();
a.Join();
b.Join();

Both events carry the same worker tag. Without options.AddAspNet(), each event carries its own.

Evidence

  • HttpContextScopeStackContainer falls back to the shared FallbackStack field.
  • AddAspNet() sets that container whatever IsGlobalModeEnabled says.
  • With the two threads above running against a scope stack container that behaves like that fallback, the event from A carried worker=B in 20 runs out of 20. With the default container, each event kept its own tag in all 20 runs.

Activity

  1. linear-code commented on Dec 3, 2026

    @linear-code
  2. added
    .NET
    Bug
    on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETBugState: Needs Discussion

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions