tags:

views:

1237

answers:

5

After hitting a few StackOverflowExceptions in .NET I noticed they completely bypass the unhandled exception handlers that .NET offers (Application.ThreadException / AppDomain.UnhandledException). This is very disturbing since we have critical cleanup code in those exception handlers.

Is there any way to overcome this?

+13  A: 

Not really; a stack overflow, or an out of memory exception happens within the CLR itself means something has gone critically wrong (I usually get it when I've been a dumbass and created a recursive property).

When this state occurs there is no way for the CLR to allocate new function calls or memory to enable it to call into the exception handlers; it's a "we must halt now" scenario.

If, however, you throw the exception yourself your exception handlers will be called.

blowdart
I find it somewhat surprising, after all the clr is microsoft owned code, they could have spared a few stack frames to allow exception handling.
gpgemini
How would it know how much to reserve? In a stack overflow situation the stack itself has grown to such a size that it's not handleable. By reserving some space you'd need to reserve the maximum stack size, which would in turn half the stack available to software.
blowdart
Ok, I do get this idea... Kind of... In a single-threaded environment. But hey, we could create more than one thread, can't we? And every thread has a separate stack, doesn't it? Then why terminate the whole process when just one thread ran out of stack?
Fyodor Soikin
+1  A: 

A stackoverflow isn't something you can just recover from, since it can't allocate more stack memory to even call your exception handler.

The only thing you can really do is track down the cause and prevent it from happening at all (eg becareful with recursion, and don't allocate large objects on the stack).

Fire Lancer
+10  A: 

There are three kind of so-called "asynchronous exceptions". That are the ThreadAbortException, the OutOfMemoryException and the mentioned StackOverflowException. Those excepions are allowed to occur at any instruction in your code.

And, there's also a way to overcome them:

The easiest is the ThreadAbortException. When the current code executes in a finally-block. ThreadAbortExceptions are kind of "moved" to the end of the finally-block. So everything in a finally-block can't be aborted by a ThreadAbortException.

To avoid an OutOfMemoryException, you have only one possibility: Do not allocate anything on the Heap. This means that you're not allowed to create any new reference-types.

To overcome the StackOverflowException, you need some help from the Framework. This help manifests in Constrained Execution Regions. The required stack is allocated before the actual code is executed and additionally also ensures that the code is already JIT-Compiled and therefor is available for execution.

There are three forms to execute code in Constrained Execution Regions (copied from the BCL Team Blog):

  • ExecuteCodeWithGuaranteedCleanup, a stack-overflow safe form of a try/finally.
  • A try/finally block preceded immediately by a call to RuntimeHelpers.PrepareConstrainedRegions. The try block is not constrained, but all catch, finally, and fault blocks for that try are.
  • As a critical finalizer - any subclass of CriticalFinalizerObject has a finalizer that is eagerly prepared before an instance of the object is allocated.
    • A special case is SafeHandle's ReleaseHandle method, a virtual method that is eagerly prepared before the subclass is allocated, and called from SafeHandle's critical finalizer.

You can find more at these blog posts:

Constrained Execution Regions and other errata [Brian Grunkemeyer] at the BCL Team Blog.

Joe Duffy's Weblog about Atomicity and asynchronous exception failures where he gives a very good overview over asynchronous exceptions and robustness in the .net Framework.

Thomas Danecker
A: 

Thomas, your suggestion looks very promissing, but I was still unable to execute finalization code following a StackOverflowException, like this:

  public class Clean : CriticalFinalizerObject {
    ~Clean() {
      Console.WriteLine("clean");
    }
  }

  class Program {
    static void Rec() {
      Rec();
    }

    static void Main(string[] args) {
      Clean c = new Clean();
      Rec();
      GC.KeepAlive(c);
    }
  }

Am I doing something wrong ?

gpgemini
Uh, oh, you asked the question one year ago and I didn't notice... sorry for that. Just for the general community interested in an answer: Console.WriteLine is a very heavy-weight method (it even prints on the screen!) Just see the following link on things you're allowed to use in CERs: http://msdn.microsoft.com/en-us/library/ms228973.aspx
Thomas Danecker
+1  A: 

blowdart nailed it, above. Dumbass recursive property, as he likes to call it. Really just a problem with typing code too quickly.

private Thing _myThing = null;

Public Thing MyThing { get{ return this.MyThing;} set{ this.MyThing = value;} }

NBB