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?

dan
  • 9,712
  • 6
  • 49
  • 62
  • 5
    [You can use DebugDiag to troubleshoot these](http://blogs.msdn.com/b/tess/archive/2009/03/20/debugging-a-net-crash-with-rules-in-debug-diag.aspx) – Martin Smith Sep 18 '11 at 18:19
  • or [ADPlus](http://blogs.msdn.com/b/cobold/archive/2010/03/01/collection-crash-dumps.aspx) – Andomar Sep 18 '11 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? – dan Sep 18 '11 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. – Martin Smith Sep 18 '11 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. – dan Sep 20 '11 at 05:02
  • 3
    The most relevant question for a site like this! A StackOverflow exception! – bbosak Oct 07 '11 at 03:06
  • @bryanmac sort of. i've set up debugdiag on some of my production servers. the problem is debugdiag slows down the server. it's not an ideal, but it was the best I could figure out. I really wish there was a way to have the CLR log the stack trace of a stackoverflow exception. – dan May 18 '12 at 05:29

3 Answers3

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.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
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.

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
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.

Lian
  • 131
  • 1
  • 7