Search code examples
c#stack-overflowasync-awaitc#-5.0

Is it possible for the compiler to create async code that would have a StackOverflowException?


Referenced Answer: https://stackoverflow.com/a/4047607/347172


Basically, since the compiler is creating a state machine that uses recursion, is it possible that a class that has "too many" awaits will cause the compiler to generate code that will throw an exception?


Solution

  • Not in the way you're thinking. The compiler creates a state machine that will schedule itself, not call itself. The scheduling creates a "trampoline" which resets the stack.