'StackOverflowExceptions in nested async methods on unwinding of the stack

We have a lot of nested async methods and see behavior that we do not really understand. Take for example this simple C# console application

public class Program
{
    static void Main(string[] args)
    {
        try
        {
            var x = Test(index: 0, max: int.Parse(args[0]), throwException: bool.Parse(args[1])).GetAwaiter().GetResult();

            Console.WriteLine(x);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex);
        }
        
        Console.ReadKey();
    }
    
    static async Task<string> Test(int index, int max, bool throwException)
    {
        await Task.Yield();
    
        if(index < max)
        {
            var nextIndex = index + 1;
            try
            {
                Console.WriteLine($"b {nextIndex} of {max} (on threadId: {Thread.CurrentThread.ManagedThreadId})");
              
                return await Test(nextIndex, max, throwException).ConfigureAwait(false);
            }
            finally
            {
                Console.WriteLine($"e {nextIndex} of {max} (on threadId: {Thread.CurrentThread.ManagedThreadId})");
            }
        }
    
        if(throwException)
        {
            throw new Exception("");
        }
    
        return "hello";
    }
}

When we run this sample with the following arguments:

AsyncStackSample.exe 2000 false

We get a StackOverflowException and this is the last message we see in the console:

e 331 of 2000 (on threadId: 4)

When we change the arguments into

AsyncStackSample.exe 2000 true

We end with this message

e 831 of 2000 (on threadId: 4)

So the StackOverflowException occurs on the unwinding of the stack (not really sure if we should call it that, but the StackOverflowException occurs after the recursive call in our sample, in synchronous code, a StackOverflowException will always occur on the nested method call). In the case that we throw an exception, the StackOverflowException occurs even earlier.

We know we can solve this by calling Task.Yield() in the finally block, but we have a few questions:

  1. Why does the Stack grow on the unwinding path (in comparison to a method that doesn't cause a thread switch on the await)?
  2. Why does the StackOverflowException occurs earlier in the Exception case than when we don't throw an exception?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source