Repository navigation
Description
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
HttpContextScopeStackContainerfalls back to the sharedFallbackStackfield.AddAspNet()sets that container whateverIsGlobalModeEnabledsays.- With the two threads above running against a scope stack container that behaves like that fallback, the event from A carried
worker=Bin 20 runs out of 20. With the default container, each event kept its own tag in all 20 runs.
Activity
linear-code commentedon Dec 3, 2026
NET-988