Methods of .NET self-annihilation part 3: Stack Overflow

This is another issue that results in Frequent Web Application Restarts, which causes the performance to plunge.

Death by Stack Overflow

This one is interesting, yet it has nothing to do with StackOverflow.com.

The StackOverflowException is one of very few exception classes in .NET that are not catch-able. Scratch that, it’s not possible to “swallow” it in a try-catch clause, because even if you don’t re-throw it, .NET will:

Starting with the .NET Framework 2.0, you can’t catch a StackOverflowException object with a try/catch block, and the corresponding process is terminated by default.

Some time back, I was involved in a case where the w3wp.exe’s started crashing. No recent deployments had been made. This message could be found in the event log:

The important piece in this message is  0xc00000fd , that’s the exception code meaning “Stack Overflow”.

Needless to say, the restarts were detrimental to the site’s performance.

It was found that this exception was caused by a code snippet like this:

If the method is called with the same suggested name repeatedly (turns out most mobile devices name their uploads “image.jpg”) and then image0.jpg .. image6842.jpg, after a few months “image6843.jpg” would be churned out as the unique name for the upload. Unfortunately when the function recursed that deep, it ran out of stack space and so the StackOverflowException was thrown by the .NET runtime.

This blog post covers the tools needed to troubleshoot issues of this kind quite well and also explains steps required to find which piece of your application causes it to die.

2 thoughts on “Methods of .NET self-annihilation part 3: Stack Overflow”

  1. Been there. We inherited a site that a few years later restarted a few times per hour. Turned out it was a function that parsed links in comments that did an infinite loop. Took som headbanging to find that little feature…

    1. You think having this post would’ve helped you nail it? If not, what’s missing?

Leave a Reply

Your email address will not be published. Required fields are marked *