25

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?

6
  • 5
    You can use DebugDiag to troubleshoot these Commented Sep 18, 2011 at 18:19
  • or ADPlus Commented Sep 18, 2011 at 19:22
  • @martin I'm not familiar with DebugDiag. Does it add any overhead to my server (like the way attaching a debugger is slow), or is it a system-level event handler for process crashes? Commented Sep 18, 2011 at 21:38
  • @dan- I can't remember hence only posting a comment. I used it a couple of years ago to get to the bottom of a stackoverflow caused by a third party component but that was easily reproducible and only on an internal low traffic website. Commented Sep 18, 2011 at 21:41
  • @martin it seems to have a slight performance hit. the hit goes higher for the more things you configure it to watch. very basic testing suggested responses were ~5% slower when DebugDiag is generating memory dumps only for stackoverflows, and about a 400% hit to log any uncaught exception. Commented Sep 20, 2011 at 5:02

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

Very wise to ask about StackOverFlowException on StackOverflow :)

AFAIK, about the only thing you can do is get a dump on a crash. The CLR is going to take you down unless you host your own CLR.

A related question on getting a dump of IIS worker process on crash:

Here's a couple other SO related threads:

Hope these pointers help.

Comments

2

You could try logging messages at key points in your application and take out the part where the flow of log messages break.

Then, try and replicate it with that section of code using unit tests.

The only way to get a trace for a stack overflow error that I know of is to have an intermediary layer taking snapshots of the running code and writing that out. This always has a heavy performance impact.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.