2

I have an ASP.NET app running on .NET 4.8. I am getting a StackOverflowException. I have an action method that calls another method CallBackMethod where using ConfigureAwait(false) in a statement that triggers a stack overflow exception. But the stack overflow exception occurs somewhere after completion of the task which affects this behaviour.

public static async Task CallBackMethod<T>() where T : Meta
{
    // Changing to ConfigureAwait(true) ensures no Stackoverflow exception
    var test = await asyncMethod1().ConfigureAwait(false);
    // Custom Exception is thrown from action.Invoke()
    await asyncMethod2().ConfigureAwait(false);
    test++;
}

For a specific scenario, a nested method deep within this asyncMethod2 call at line 2 throws a custom exception which is expected. But when it does that, my app throws stack overflow exception and exits.

If I change the first line in the method to

var test = await asyncMethod1().ConfigureAwait(true)

then the stack overflow exception is not thrown. Only the expected custom exception is thrown and handled.

I thought there could be some recursive call in my code but If I double the default stack size for my app then the stack overflow exception is not thrown.

I am not able to figure out what could be causing the stack overflow exception. I wanted to understand what is changing between ConfigureAwait(false) and true that the stack overflow exception is not being thrown.

Minimal repro example in this repo:

https://github.com/riteshksriv/TestSO

5
  • Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your source code as a working minimal reproducible example, which can be compiled and tested by others to provide an answer faster. Commented Feb 15 at 15:13
  • this is presumably old framework and not asp.net core? what synchronization context do you have? legacy/new? Commented Feb 15 at 15:32
  • Yes, this is old framework. Microsoft.AspNet.Mvc (version 5.2.3) and Microsoft.AspNet.WebApi (version 5.2.3) our setup runs on the legacy ASP.NET SynchronizationContext. Commented Feb 15 at 15:39
  • @RKS couldn't reproduce with your repo. FWIW I get a much bigger stack with true which is somewhat more logical Commented Feb 15 at 17:57
  • That's what I was also expecting but I get similar stack in both and Stackoverflow just after 90 methods in the chain for ConfigureAwait(false). Interestingly the ConfigureAwait(true) version also starts to throw SO Exception if I increase the number of methods in the chain to 95 or more. Commented Feb 16 at 6:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.