0

In our ASP.NET MVC web application, we've identified a code path that can potentially lead to a stack overflow that brings down the Application Pool and returns a 503 Service Unavailable to the client. This SO question was helpful in identifying it.

I'm just trying to safeguard against the stack overflow by throwing an exception, or i've also tried calling Response.End(). But i continue to get the stack overflow result. I should be able to avoid the stack overflow by throwing an exception, obviously depending on properly identifying what causes the overflow and throwing at the right point. It also seems possible that the overflow gets (re-)triggered within the Application_OnError in Global.asax, which occurs after i throw the exception.

Can you provide any guidance on how to gracefully recover from an eventual stack overflow, when it has been detected, besides just fixing the code to prevent the scenario completely, within the context of an ASP.NET MVC request?

This is basically what i'm trying to do, checking for if we're heading for a stack overflow:

if ((HttpContext.Current != null) && (HttpContext.Current.Cache != null))
{
    string idrs = "IsDebugCount";
    int max = 1000000;
    int idc = 0;
    object idco = HttpContext.Current.Cache[idrs];
    if (idco != null)
        idc = (int)idco;
    idc += 1;
                     
    //log4net.LogManager.GetLogger("IsDebug").Error("idc=" + idc);
    if (idc > max)
    {
         string freakout = "IsDebug has been consulted > " + max +
         " times. This seems problematic.";
                         
        log4net.LogManager.GetLogger("IsDebug").Error(freakout);
       
        throw new ArithmeticException(freakout);
        HttpContext.Current.Response.End();
    }
    HttpContext.Current.Cache[idrs] = idc;
}

i've tried both the throw and the Response.End() independently, neither have had the desired effect of avoiding a stack overflow that crashes the Application Pool.

ilasno
  • 714
  • 1
  • 13
  • 31

0 Answers0