none
Catching : StackOverflowException RRS feed

  • Question

  • Hi All,


    I have developed a windows service ( .NET 2.0 framework) and i have the following flag set in app.config...

    <configuration>
      <runtime>
        <legacyUnhandledExceptionPolicy enabled="1" />
      </runtime>
    </configuration>

    After deployment, i have noticed the following entry in the event log and the service is stopped when this error pops-up...

    ".NET Runtime 2.0 Error Reporting : Faulting application documerger.exe, version 1.0.0.0, stamp 4a0cd39a, faulting module kernel32.dll, version 5.0.2195.7135, stamp 46236f98, debug? 0, fault address 0x0002bcb1. "

    I did some de-bugging and noticed that a recursive function is the culprit...(the event log is not conveying anything about StackOverflowException, but i could reproduce the same message with a recursive function using a sample window service)...

    I have wrapped the code in try/catch block, but the exception is not getting caught..

    I also have AppDomain.CurrentDomain.UnhandledException wired-up, but unable to catch the StackOverflowException...

    I could change the function to aviod recursion, but i just wanted to know if we can catch the StackOverflowException before doing any code change...

    My question/doubt is, Why the service is getting crashed, even if i have enabled "legacyUnhandledExceptionPolicy"? Isn't the service supposed to suppress all the exceptions and keep running?

    Thanks
    Kiran..
    • Edited by kiran_tsp Monday, May 18, 2009 9:07 AM
    Monday, May 18, 2009 8:37 AM

Answers

  • -> but i just wanted to know if we can catch the StackOverflowException before doing any code change...

    Nope, you cannot stop the StackOverflowException from propagating up the call stack, although you could write some backout code when StackOverflowException is thrown with Constrained Execution Region feature:

    http://msdn.microsoft.com/en-us/library/ms228973.aspx

    -> My question/doubt is, Why the service is getting crashed, even if i have enabled "legacyUnhandledExceptionPolicy"? Isn't the service supposed to suppress all the exceptions and keep running?

    Nope, as far as I know, "legacyUnhandledExceptionPolicy" is used when you don't want to be torned down when an exception is thrown from other explicitly created threads (Thread.Start()) rather than the main thread. I strongly recommend you not to use this configuration element, and stick with the current correct CLR behavior.

    Thanks
    Marco
    Another Paradigm Shift
    http://shevaspace.blogspot.com
    • Edited by Marco Zhou Monday, May 18, 2009 9:16 AM bad typo
    • Marked as answer by kiran_tsp Monday, May 18, 2009 10:22 AM
    Monday, May 18, 2009 9:16 AM

All replies

  • -> but i just wanted to know if we can catch the StackOverflowException before doing any code change...

    Nope, you cannot stop the StackOverflowException from propagating up the call stack, although you could write some backout code when StackOverflowException is thrown with Constrained Execution Region feature:

    http://msdn.microsoft.com/en-us/library/ms228973.aspx

    -> My question/doubt is, Why the service is getting crashed, even if i have enabled "legacyUnhandledExceptionPolicy"? Isn't the service supposed to suppress all the exceptions and keep running?

    Nope, as far as I know, "legacyUnhandledExceptionPolicy" is used when you don't want to be torned down when an exception is thrown from other explicitly created threads (Thread.Start()) rather than the main thread. I strongly recommend you not to use this configuration element, and stick with the current correct CLR behavior.

    Thanks
    Marco
    Another Paradigm Shift
    http://shevaspace.blogspot.com
    • Edited by Marco Zhou Monday, May 18, 2009 9:16 AM bad typo
    • Marked as answer by kiran_tsp Monday, May 18, 2009 10:22 AM
    Monday, May 18, 2009 9:16 AM
  • Hi Marco,

    thank you very much for the reply...

    Just wanted to give some back ground on my service...It's a migrated one from .NET 1.1 to .NET 2.0...and the erroneous recursive function is called from a thread launched using Thread.Start in the service OnStart() event...

    As per the MSDN link : http://msdn.microsoft.com/en-us/library/system.stackoverflowexception.aspx,
    "In prior versions of the .NET Framework, your application could catch a StackOverflowException object (for example, to recover from unbounded recursion). However, that practice is currently discouraged because significant additional code is required to reliably catch a stack overflow exception and continue program execution.".

    So, just wanted to find out if .NET 1.1 handles StackOverflowException & if yes, how (The CERs mentioned by you in the link : http://msdn.microsoft.com/en-us/library/ms228973.aspx, i believe are supported from .NET 2.0 onwards)?

    I have developed a sample .NET 1.1 windows service to find out the behavior...All i have is a service crash, but .NET 1.1 framework is gracious enough :-) to provide me with an error dialogue (which is not displayed with .NET 2.0) ...

    ---------------------------
    windowsservice 2003.exe - Common Language Runtime Debugging Services
    ---------------------------
    Application has generated an exception that could not be handled.

    Process id=0x1084 (4228), Thread id=0x12ec (4844).

    Click OK to terminate the application.
    Click CANCEL to debug the application.
    ---------------------------
    OK   Cancel  
    ---------------------------

    And the "DebugDiag" tool tells me that - (0x800703e9-StackOverflowException) has caused this crash..

    [5/18/2009 3:54:33 PM] Thread exited. Exiting thread system id - 4844. Exit code - 0x800703e9
    [5/18/2009 3:54:33 PM] Process exited. Exit code - 0x800703e9

    Regards
    Kiran...

    Monday, May 18, 2009 10:35 AM
  • Hello Kiran,

    we are also having similar question. Basically our worker process (w3wp.exe) gets crashed. And we have attached Debug Diagnostics tool for w3wp. But when worker process gets crashed it never generates any crash dump and when we check Debug Diag. Log file it says following entry:

    http://forums.iis.net/p/1183520/2010387.aspx#2010387

    You can read more info about our scenario from here : http://forums.iis.net/p/1183520/2010387.aspx#2010387

    So, in our scenario we are not sure which method is doing recursion. So, if you got any hint. We will appreciate your support.

    -Kiran

    I am Begineer
    Wednesday, January 11, 2012 4:56 PM