question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sentry loses its scope inside the ASP.NET pipeline because it uses AsyncLocal for storage

See original GitHub issue

The official Sentry SDK docs for ASP.NET instructs one to start and finish tracing transactions like this in Global.asax.cs:

protected void Application_BeginRequest()
{
    Context.StartSentryTransaction();
}

protected void Application_EndRequest()
{
    Context.FinishSentryTransaction();
}

This approach is buggy, as it will sometimes cause the Sentry scope to be lost inside the (non-Core) ASP.NET pipeline. The underlying problem is that the Sentry SDK stores its scopes in an AsyncLocal field in SentryScopeManager.cs. This is not a safe way to store such state in (non-Core) ASP.NET. The only safe method is likely to use HttpContext.Items. For more information, see:

In my use case, I only care about the transaction reference, so I applied the following workaround to Global.asax.cs, based on the above mentioned blog post my Microsoft and how they themselves work around the issue in Application Insights:

public override void Init()
{
    base.Init();
    
    OnExecuteRequestStep((httpContextBase, step) =>
    {
        if (httpContextBase.CurrentNotification == RequestNotification.ExecuteRequestHandler && !httpContextBase.IsPostNotification)
        {
            SentrySdk.ConfigureScope(scope =>
            {
                if (scope.Transaction == null)
                {
                    scope.Transaction = (ITransaction) httpContextBase.Items["__SentryTransaction"];
                }
            });
        }

        step();
    });
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Zero3commented, Aug 12, 2021

@Tyrrrz SentrySdk.GetSpan() now throws a NullReferenceException from deep within Sentry when executed outside a HTTP request. It used to return null. Is this intentional?

The problem is that .Current can return null in the getter here, which is unhandled:

https://github.com/getsentry/sentry-dotnet/blob/28ec35d278b2fa15d356af41079ab81a956fe364/src/Sentry.AspNet/Internal/HttpContextScopeStackContainer.cs#L11-L15

Also see this related issue: https://github.com/getsentry/sentry-dotnet/issues/1157

1reaction
Zero3commented, Jul 24, 2021

Thanks for the fix! I am off for summer holidays, but I might be able to test this in August.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scopes and Hubs for ASP.NET
The scope will hold useful information that should be sent along with the event. For instance contexts or breadcrumbs are stored on the...
Read more >
Can't add Sentry to ASP.NET Core Web API project in .NET 6
It seems that AddSentry is not recognized as an extension method of IServiceCollection . I have searched for similar issues and found some ......
Read more >
sitemap-questions-71.xml
https://stackoverflow.com/questions/769/solving-a-linear-equation 2021-09-01 ... .com/questions/3502565/error-when-executing-a-stored-proc-in-asp-net ...
Read more >
WooodHead
A fast multi-threaded bulk downloader for Oracle queries written in C++11 style and designed to batch process numerous queries concurrently ...
Read more >
sitemap-posts-15.xml
... -10-sites-extensions-you-should-use-if-you-are-a-frontend-developer-1110 ... https://dev.to/split/implement-feature-flags-for-entitlements-in-asp-net- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found