Just facing this curious case, and I wonder whether this pattern will yield any guarantee on the stack usage (i.e. is reliable or not).
NOTE: the counter is just an hypothetical work to do in the "DoIt" function.
class Program
{
static void Main(string[] args)
{
DoIt();
Console.ReadKey();
_flag = true;
}
private static bool _flag;
private static int _count;
private static async void DoIt()
{
await Task.Delay(1000);
_count++;
if (_flag) return;
DoIt();
}
}
The "DoIt" task should call itself forever, until something (i.e. the flag) breaks the recursion. My wonder is whether the stacks fills up or not, since it's against an "async" call, which shouldn't halt the caller.
The main question is: may I expect a StackOverflow exception in some case, or am I guaranteed to have it not?
What if I also remove the delay?
As for me, the pattern should be safe (almost) all the times, but honestly I wouldn't explain how to guarantee it (at least without analyzing the IL code behind). Just a doubt when the loop is very tight so that the async-call gets "slower" than the whole function itself.