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

NullReferenceException on Dispose()

Open nicolaigj opened this issue 7 years ago • 5 comments

Hi!

We have a ASP.NET Framework API project using .NET Framework 4.6.2 and we are using Sentry SDK version 1.1.2. I struggled a bit finding documentation on how to use sentry in a project without a main method so I could wrap sentry SDK around the program, so I ended up using this documentation on Entity framework and it works quite well when I configured Sentry in Global.asax.

Today I received an error in Sentry dashboard that we got a NullReferenceException on sentrySDK.Dispose(). Is the documentation still valid? Should we call Dispose()? It reports other errors as normal, so it works, but the error was quite strange.

Stack trace in Sentry

System.NullReferenceException: Object reference not set to an instance of an object.
  File "D:\a\1\s\ProductAPI\Global.asax.cs", line 57, col 13, in Dispose
    Void Dispose()
  Module "System.Web.HttpApplication", in DisposeInternal

The code that triggered the exception

public override void Dispose()
{
    sentrySDK.Dispose(); //This is line 57 in the code
    base.Dispose();
}

The full configuration in Global.asax with unrelated code omitted.

public class WebApiApplication : System.Web.HttpApplication
{
    private IDisposable sentrySDK;
    protected void Application_Start()
    {
        // Omitted
        var appSettings = container.GetInstance<IConfigurationManager>();

        // Omitted

        sentrySDK = SentrySdk.Init(o =>
        {
            o.Dsn = new Dsn(appSettings.GetString("sentryDSN"));
            o.Environment = appSettings.GetString("sentryEnv");
        });
    }

    private Container RegisterDependencies()
    {
      // Omitted code
    }

    // Global error catcher
    protected void Application_Error()
    {
        var exception = Server.GetLastError();
        SentrySdk.CaptureException(exception);
    }

    public override void Dispose()
    {
        sentrySDK.Dispose();
        base.Dispose();
    }
}

nicolaigj avatar Feb 25 '19 12:02 nicolaigj

I was having the same issue with the code in my Global.asax so I moved it to an IHttpModule and haven't had an issue since. I also tried sentrySdk?.Dispose() but it felt wrong that the object was null in the first place so that's when I moved the code out of there.

xt0rted avatar Feb 25 '19 15:02 xt0rted

It seems that Application_Start wasn't called. Is it possible? because sentrySdk cannot be null otherwise. I'd say that sentrySdk?.Dispose() is a better approach. if it's null then there's nothing to flush and it's OK.

bruno-garcia avatar Feb 25 '19 21:02 bruno-garcia

Not sure if this helps, but I used the exact same code (with the exception of specifying the DSN and environment differently) in an Asp.Net MVC project (4.7.2) and had the same symptoms along with the following:

  • At the end of the Application_Start() method, the _sentrySdk object was initialized, however, upon entering the Application_Error() method, the _sentrySdk object was already null.
  • None of my exceptions were being recorded by Sentry.io

I also tried with the following in the Application_Error() method however the results were the same:

using (var sentry = SentrySdk.Init(o =>
{
    o.Dsn = new Dsn(ConfigurationManager.AppSettings["SentryDsn"]);
    o.Environment = ConfigurationManager.AppSettings["SentryEnvironment"];
}))
{
    SentrySdk.CaptureException(exception);
}

Finally, I replaced the package with the older SharpRaven package and logging works as expected:

var raven = new RavenClient(ConfigurationManager.AppSettings["SentryDsn"]);
raven.Environment = ConfigurationManager.AppSettings["SentryEnvironment"];
raven.Capture(new SentryEvent(exception));

jehrich avatar May 06 '19 20:05 jehrich

I'm unsure what do you mean by was already null. In no code path the sample nullifies the _sentrySdk variable. It simply calls Dispose on it when Global.asax/WebApiApplication itself gets disposed. In any regard, if the SDK is already disposed when Application_Error is called, that'd be a race condition in the IIS pipe-line where Dispose was called while or before a thread is invoking Application_Error.

A replacement for your RavenSharp example is to simply use new SentryClient with the new SDK. This is pretty much what raven sharp is doing there. Make sure to Dispose of the SentryClient object before returning to flush the event that you've just queued up.

bruno-garcia avatar May 13 '19 17:05 bruno-garcia

While debugging samples with ASP.NET Classic I noticed that multiple instances of Global.asax where beiing disposed without even having Application_Start method called. As we updated the sample to show, please consider _sentry not be there to support these cases.

There will be 1 instance that will have the Sentry SDK instance initialized.

bruno-garcia avatar Jun 20 '19 09:06 bruno-garcia