StackOverflow Exception – wrong implementation or leak of resources

During this week, I had the opportunity to analyze a very interesting trouble and I want to share it with all of you. I do not want to go too much in deep about what occurred on the real scenario otherwise you could consider this post very long and boring. My idea is to share here some concepts:

  • A StackOverflow exception occurs when your code uses all resources available for stack. So, the first consideration to keep in mind is this one: What is the size of my call stack?
    There is a public KB that answers to this question https://support.microsoft.com/kb/932909/en-gb concerning w3wp.exe (process who hosts our web page)
    • 256 KB for 32 bit process
    • 512 KB for 64 bit process

The common mistake concerning StackOverflow exception is to write down a recursive function that never ends its work. In this scenario, it is expected to have this exception. Of course, I do not want to discuss here about an infinite recursive function… that is a bug and nothing more. Let’s consider to write down a function like this one:

  
 public  int DoWork1( long x)
 {
     if (x == 0)
         return 0;
     return DoWork1(x - 1);
 }
  

According to input value, this function could grow-up in term of resources use in the stack. Let’s consider to have an application like this one:

 protected void Button1_Click(object sender, EventArgs e)
 {
     long input=10000;
     Work w = new Work(input);
     ThreadStart threadDelegate = new ThreadStart(w.DoWork);
     Thread newThread = new Thread(threadDelegate);
     newThread.Start();
  
 }
  
  
 class Work
 {
     private long i;
  
     public Work(long i)
     {
         this.i = i;
     }
  
     public void DoWork()
     {
          int ouput=DoWork1(i);
     }
  
     public  int DoWork1( long x)
     {
         if (x == 0)
             return 0;
         return DoWork1(x - 1);
     }
 }
  

Since the input value is very high, this application will raise a StackOverflow Exception even if reading the code everything seem to be fine. We have a StackOverflow Exception because our code uses all available resources.

Is there any possibility to fix it? Yes, of course otherwise I was not writing this post on my blog :-)..There are two possibilities to fix this issue:

  1. Increase the stack size for all threads of your application: https://blogs.msdn.com/b/tom/archive/2008/03/31/stack-sizes-in-iis-affects-asp-net.aspx

  2. Customize stack size for my thread “newThread”.

    NET Thread API has a constructor that allows you to specify the maximum stack size to use https://msdn.microsoft.com/en-us/library/5cykbwz4.aspx

    Changing the definition of my newThread in the following way:

  Thread newThread = new Thread(threadDelegate, 1048576);

The trouble is fixed.

Please note that what I mentioned here is only a simplification of what could happen on a real web application. Consider stack limit especially when you write down your classes and attributes. This because on a real scenario, it could happens that 3rd party code or .NET framework core dlls need to parse your code in a recursive way. If your classes include many members, a StackOverflow Exception could come out even if you do not expect it.

Hope this help.

Carmelo