尽管屈服

时间:2015-09-05 11:17:33

标签: c# .net multithreading async-await task-parallel-library

为什么以下异步递归会因StackOverflowException而失败,为什么它会在计数器变为零的最后一步发生?

static async Task<int> TestAsync(int c)
{
    if (c < 0)
        return c;

    Console.WriteLine(new { c, where = "before", Environment.CurrentManagedThreadId });

    await Task.Yield();

    Console.WriteLine(new { c, where = "after", Environment.CurrentManagedThreadId });

    return await TestAsync(c-1);
}

static void Main(string[] args)
{
    Task.Run(() => TestAsync(5000)).GetAwaiter().GetResult();
}

输出:

...
{ c = 10, where = before, CurrentManagedThreadId = 4 }
{ c = 10, where = after, CurrentManagedThreadId = 4 }
{ c = 9, where = before, CurrentManagedThreadId = 4 }
{ c = 9, where = after, CurrentManagedThreadId = 5 }
{ c = 8, where = before, CurrentManagedThreadId = 5 }
{ c = 8, where = after, CurrentManagedThreadId = 5 }
{ c = 7, where = before, CurrentManagedThreadId = 5 }
{ c = 7, where = after, CurrentManagedThreadId = 5 }
{ c = 6, where = before, CurrentManagedThreadId = 5 }
{ c = 6, where = after, CurrentManagedThreadId = 5 }
{ c = 5, where = before, CurrentManagedThreadId = 5 }
{ c = 5, where = after, CurrentManagedThreadId = 5 }
{ c = 4, where = before, CurrentManagedThreadId = 5 }
{ c = 4, where = after, CurrentManagedThreadId = 5 }
{ c = 3, where = before, CurrentManagedThreadId = 5 }
{ c = 3, where = after, CurrentManagedThreadId = 5 }
{ c = 2, where = before, CurrentManagedThreadId = 5 }
{ c = 2, where = after, CurrentManagedThreadId = 5 }
{ c = 1, where = before, CurrentManagedThreadId = 5 }
{ c = 1, where = after, CurrentManagedThreadId = 5 }
{ c = 0, where = before, CurrentManagedThreadId = 5 }
{ c = 0, where = after, CurrentManagedThreadId = 5 }

Process is terminated due to StackOverflowException.

我已经安装了.NET 4.6。该项目是一个面向.NET 4.5的控制台应用程序。

我理解Task.Yield的延续可能由ThreadPool.QueueUserWorkItem安排在同一个线程上(如上面的#5),以防线程已经释放到池中 - 就在{await Task.Yield()之后1}},但在实际安排QueueUserWorkItem回调之前。

但是,我不明白为什么以及堆栈仍在深化的地方。这里的延续不应该发生在同一个堆栈帧上,即使它在同一个线程上被调用。

我更进一步,实现了Yield的自定义版本,确保延续不会发生在同一个线程上:

public static class TaskExt
{
    public static YieldAwaiter Yield() { return new YieldAwaiter(); }

    public struct YieldAwaiter : System.Runtime.CompilerServices.ICriticalNotifyCompletion
    {
        public YieldAwaiter GetAwaiter() { return this; }

        public bool IsCompleted { get { return false; } }

        public void GetResult() { }

        public void UnsafeOnCompleted(Action continuation)
        {
            using (var mre = new ManualResetEvent(initialState: false))
            {
                ThreadPool.UnsafeQueueUserWorkItem(_ => 
                {
                    mre.Set();
                    continuation();
                }, null);

                mre.WaitOne();
            }
        }

        public void OnCompleted(Action continuation)
        {
            throw new NotImplementedException();
        }
    }
}

现在,在使用TaskExt.Yield而不是Task.Yield时,线程每次都在翻转,但堆栈溢出仍然存在:

...
{ c = 10, where = before, CurrentManagedThreadId = 3 }
{ c = 10, where = after, CurrentManagedThreadId = 4 }
{ c = 9, where = before, CurrentManagedThreadId = 4 }
{ c = 9, where = after, CurrentManagedThreadId = 5 }
{ c = 8, where = before, CurrentManagedThreadId = 5 }
{ c = 8, where = after, CurrentManagedThreadId = 3 }
{ c = 7, where = before, CurrentManagedThreadId = 3 }
{ c = 7, where = after, CurrentManagedThreadId = 4 }
{ c = 6, where = before, CurrentManagedThreadId = 4 }
{ c = 6, where = after, CurrentManagedThreadId = 5 }
{ c = 5, where = before, CurrentManagedThreadId = 5 }
{ c = 5, where = after, CurrentManagedThreadId = 4 }
{ c = 4, where = before, CurrentManagedThreadId = 4 }
{ c = 4, where = after, CurrentManagedThreadId = 3 }
{ c = 3, where = before, CurrentManagedThreadId = 3 }
{ c = 3, where = after, CurrentManagedThreadId = 5 }
{ c = 2, where = before, CurrentManagedThreadId = 5 }
{ c = 2, where = after, CurrentManagedThreadId = 3 }
{ c = 1, where = before, CurrentManagedThreadId = 3 }
{ c = 1, where = after, CurrentManagedThreadId = 5 }
{ c = 0, where = before, CurrentManagedThreadId = 5 }
{ c = 0, where = after, CurrentManagedThreadId = 3 }

Process is terminated due to StackOverflowException.

1 个答案:

答案 0 :(得分:8)

TPL再次出现再次袭击:

注意,在完成所有迭代后,堆栈溢出发生在函数的末尾。增加迭代次数不会改变它。将其降低到少量可以消除堆栈溢出。

完成方法TestAsync的异步状态机任务时发生堆栈溢出。它不会发生在“下降”。在退出并完成所有async方法任务时会发生这种情况。

让我们先将计数减少到2000,以减少调试器的负担。然后,查看调用堆栈:

enter image description here

当然非常重复而且很长。这是正确的线索。崩溃发生在:

        var t = await TestAsync(c - 1);
        return t;

当内部任务t完成时,会导致外部TestAsync的其余部分执行。这只是返回声明。返回完成外部TestAsync生成的任务。这又会触发另一个t的完成等等。

TPL将一些任务延续概述为性能优化。这种行为已经引起了很多悲痛,Stack Overflow问题证明了这一点。 It has been requested to remove it.这个问题已经很久了,到目前为止还没有收到任何答复。这并没有激发我们最终摆脱TPL重入问题的希望。

TPL有一些堆栈深度检查,以在堆栈变得太深时关闭延续线的内联。由于(我)未知的原因,这里没有这样做。请注意,堆栈上没有TaskCompletionSourceTaskAwaiter利用TPL中的内部函数来提高性能。也许优化的代码路径不执行堆栈深度检查。也许这是一个错误。

我不认为调用Yield与此问题有任何关系,但最好将其放在此处以确保TestAsync的非同步完成。

让我们手动编写异步状态机:

static Task<int> TestAsync(int c)
{
    var tcs = new TaskCompletionSource<int>();

    if (c < 0)
        tcs.SetResult(0);
    else
    {
        Task.Run(() =>
        {
            var t = TestAsync(c - 1);
            t.ContinueWith(_ => tcs.SetResult(0), TaskContinuationOptions.ExecuteSynchronously);
        });
    }

    return tcs.Task;
}

static void Main(string[] args)
{
    Task.Run(() => TestAsync(2000).ContinueWith(_ =>
    {
          //breakpoint here - look at the stack
    }, TaskContinuationOptions.ExecuteSynchronously)).GetAwaiter().GetResult();
}

感谢TaskContinuationOptions.ExecuteSynchronously我们也期望继续内联发生。确实如此,但它不会溢出堆栈:

enter image description here

那是因为TPL阻止了堆栈变得太深(如上所述)。在完成async方法任务时,这种机制似乎不存在。

如果删除ExecuteSynchronously,则堆栈很浅,不会发生内联。 await runs with ExecuteSynchronously enabled.