Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log a StackOverflowException in .NET

Tags:

I recently had a stack overflow exception in my .NET app (asp.net website), which I know because it showed up in my EventLog. I understand that a StackOverflow exception cannot be caught or handled, but is there a way to log it before it kills your application? I have 100k lines of code. If I knew the stack trace, or just part of the stack trace, I could track down the source of the infinite loop/recursion. But without any helpful diagnostic information, it looks like it'll take a lot of guess-and-test.

I tried setting up an unhandled exception handler on my app domain (in global.asax), but that didn't seem to execute, which makes sense if a stack overflow is supposed to terminate and not run any catch/finally blocks.

Is there any way to log these, or is there some secret setting that I can enable that will save the stack frames to disk when the app crashes?

like image 774
dan Avatar asked Sep 18 '11 18:09

dan


People also ask

Can you catch a StackOverflowException C#?

Starting with the . NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow.

Can we handle StackOverflowException?

Applying the HandleProcessCorruptedStateExceptionsAttribute attribute to a method that throws a StackOverflowException has no effect. You still cannot handle the exception from user code.

What is StackOverflowException in C#?

A StackOverflowException is thrown when the execution stack overflows because it contains too many nested method calls. For example, suppose you have an app as follows: C# Copy. using System; namespace temp { class Program { static void Main(string[] args) { Main(args); // Oops, this recursion won't stop. } } }

Can you catch a StackOverflowException Java?

Catching a stack overflow exception is a bad idea; you are treating the symptoms without addressing the underlying cause. In the case of the use of recursion and overflow of the set options of the JVM the SO is a normal negative answer of a correct program. We MUST catch it and react.


1 Answers

Your best bet is to use ADPlus.

The Windows NT Kernel Debugging Team has a nice blog post on how to catch CLR exceptions with it. There are a lot of details on that there about different configuration options for it.

ADPlus will monitor the application. You can specify that it run in crash mode so you can tell ADPlus to take a memory dump right as the StackOverflowException is happening.

Once you have a memory dump, you can use WinDbg and a managed debugging extension like Psscor to see what was going on during the stack overflow.

like image 116
vcsjones Avatar answered Oct 18 '22 23:10

vcsjones