Quote:NCrunch's handling of stack overflows is implemented by cheating. It tracks the size of the stack and stops the overflow before it happens.
Sounds like a viable way, I assume you use the intrumentation API that you also use for measuring method's execution time?
Quote:Trying to recover from an application after an overflow is almost impossible to do reliably.
Yes, I know. In fact, the CLR uses something similar to Environment.FailFast, which skips all exception handling, except in the CLR host process. The only way I have managed to catch an overflow *and* to recover (to the extend as to allow a managed message and error report) is by writing your own CLR host implementation in C++ and the appropriate allocation hooks (while not trivial, it isn't that hard either). This is the same approach Microsoft uses for hosted processes in SQL Server, for instance.
While there are some ways of recovering from an SO exception, the same is not true of an ExecutionEngineException. Though you can catch this exception in the CLR host process as well, there is no way to recover from it (though the CLR host process could opt a way to report this error back to the user by providing a hook through a dynamically loaded error reporting dll, or something similar). Even more than an SO exception, after this exception the process is in an indeterminate state. I think that SEHException and
FatalExecutionError are in the same ballpark. There are
some reports (and
here) that .NET 4.5 and RyuJIT throw these errors on otherwise verifiable code.
I'm not 100% sure, but I think NCrunch uses a host process and child processes for running the tests. Perhaps the host process could check and catch the error, though I think you need C++ SEH injected in the child process to actually do so, I think.