Error in 3rd Party Library Causing Hanging in Release

Viewed 195

My main program is an ASP.Net Core Web API that has a third party library in a hosted service. The third party library is initializing fine but then it throws some errors sometime throughout its lifecycle.

It supplies a way of hooking into the object via an event and will let me know what the error is so that I can handle it but it still throws in the third party library..

Since I am handling the event myself, I want to completely ignore these errors that are occurring in this library. Is there anyway that I can do that?

Error That is occuring

I have already tried to add a global exception handler and the strange thing is, this exception handler never gets hit. The only way I can get the exception is to set my exception settings to break when CLR exceptions happen like in the picture above

Global Exception Handler

This does not crash my program. For some reason, the program just hangs. When I turn off CLR exceptions in the "Break when thrown" window, then the program runs just fine. It is almost like visual studio is doing something special to handle these types of exceptions that a console version cannot do

The only way that I can seem to get a console version of this running, is attach a visual studio debugger to the process and when the exception is hit, press the green play button "Continue" in visual studio. Otherwise the application just seems to hang on the exception being thrown by the third party library.

The application will run fine as long as visual studio is attached and the CLR break exceptions are not checked

Does anyone know how to make sure that these types of exceptions do not hang the program when released?

Additional Info:

  • The third party library is a .NET Framework 4 library
  • The Asp.Net project is targetting "net5.0-windows"
  • The 3rd party class is probably using multi-threading

if it helps, this is how I am creating the third party class Instantiation of third party library

2 Answers

Handling NullReferenceException in release code(Official advice)

It's usually better to avoid a NullReferenceException than to handle it after it occurs. Handling an exception can make your code harder to maintain and understand, and can sometimes introduce other bugs. A NullReferenceException is often a non-recoverable error. In these cases, letting the exception stop the app might be the best alternative.

However, there are many situations where handling the error can be useful:

1.Your app can ignore objects that are null. For example, if your app retrieves and processes records in a database, you might be able to ignore some number of bad records that result in null objects. Recording the bad data in a log file or in the application UI might be all you have to do.

2.You can recover from the exception. For example, a call to a web service that returns a reference type might return null if the connection is lost or the connection times out. You can attempt to reestablish the connection and try the call again.

3.You can restore the state of your app to a valid state. For example, you might be performing a multi-step task that requires you to save information to a data store before you call a method that throws a NullReferenceException. If the uninitialized object would corrupt the data record, you can remove the previous data before you close the app.

4.You want to report the exception. For example, if the error was caused by a mistake from the user of your app, you can generate a message to help them supply the correct information. You can also log information about the error to help you fix the problem. Some frameworks, like ASP.NET, have a high-level exception handler that captures all errors to that the app never crashes; in that case, logging the exception might be the only way you can know that it occurs.

So after days of research I've finally found an event to hook into to give you error messages from ANY source no matter how many level deep you go in threads.

AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

Hooking into this event it will allow you to see errors from every library and every thread. Simply place the above into you program.cs (or whatever startup file you have) and magically you will be flooded with all of the unknown errors from all of the 3rd party libraries you thought were once flawless.

private static void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
    Console.WriteLine(e.Exception.Message, e.Exception.StackTrace);
}

I've done so with the following method and low and behold. The third party library was trying to reference another project in an unsafe way and throwing an error. Since I didn't need this other project reference the built exe did not have a reference to this assembly because I had no direct reference to it in the project (darn smarty pants who need to optimize everything). I was able to run correctly because in my visual studio solution, I had a reference to this other project. So the third party library would pick up on it as soon as visual studio connected with the debugger through some sort of dark magic.

Anyways, I made a throw away object that used the project that was required and the issue was solved.

I really hope that this helps someone else and saves them the days it took me to find this.

Related