gpt4 book ai didi

c# - 使用 SynchronizationContext 时异步/等待死锁

转载 作者:太空狗 更新时间:2023-10-29 17:43:07 25 4
gpt4 key购买 nike

根据 this link :

When you are awaiting on a method with await keyword, compiler generates bunch of code in behalf of you. One of the purposes of this action is to handle synchronization with the UI thread. The key
component of this feature is the SynchronizationContext.Current which gets the synchronization context for the current thread.
SynchronizationContext.Current is populated depending on the
environment you are in. The GetAwaiter method of Task looks up for
SynchronizationContext.Current. If current synchronization context is not null, the continuation that gets passed to that awaiter will get posted back to that synchronization context.

When consuming a method, which uses the new asynchronous language features, in a blocking fashion, you will end up with a deadlock if
you have an available SynchronizationContext.
When you are consuming such methods in a blocking fashion (waiting on the Task with Wait method or taking the result directly from the Result property of the Task), you will block the main thread at the same time. When eventually the Task completes inside that method in the threadpool, it is going to invoke the continuation to post back to the main thread because SynchronizationContext.Current is available and captured. But there is a problem here: the UI thread is blocked and you have a deadlock!

    public class HomeController : Controller
{
public ViewResult CarsSync()
{
SampleAPIClient client = new SampleAPIClient();
var cars = client.GetCarsInAWrongWayAsync().Result;
return View("Index", model: cars);
}
}

public class SampleAPIClient
{
private const string ApiUri = "http://localhost:17257/api/cars";
public async Task<IEnumerable<Car>> GetCarsInAWrongWayAsync()
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(ApiUri);

// Not the best way to handle it but will do the work for demo purposes
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<IEnumerable<Car>>();
}
}
}

我无法理解上面语句的粗体 部分,但是当我测试上面的代码时,它按预期发生了死锁。但是我还是不明白为什么UI线程会被阻塞?

在这种情况下,可用的 SynchronizationContext 是什么?是 UI 线程吗?

最佳答案

我对此进行了完整解释 in my own blog post , 但在这里重申......

await默认情况下将捕获当前“上下文”并恢复其 async该上下文中的方法。此上下文是 SynchronizationContext.Current除非它是 null , 在这种情况下它是 TaskScheduler.Current .

一次一个线程时可能会发生死锁 SynchronizationContext并且您阻止代表异步代码的任务(例如,使用 Task.WaitTask<T>.Result )。请注意,导致死锁的是阻塞,而不仅仅是 SynchronizationContext。 ;适当的解决方案(几乎总是)是使调用代码异步(例如,将 Task.Wait/Task<T>.Result 替换为 await )。在 ASP.NET 上尤其如此。

But I still can't understand why the UI thread is blocked?

您的示例在 ASP.NET 上运行;没有 UI 线程。

what is the available SynchronizationContext?

当前SynchronizationContext应该是 AspNetSynchronizationContext 的实例,表示 ASP.NET 请求的上下文。此上下文一次只允许一个线程。


因此,通过您的示例:

当收到此操作的请求时,CarsSync将在该请求上下文中开始执行。它继续到这一行:

var cars = client.GetCarsInAWrongWayAsync().Result;

本质上与此相同:

Task<IEnumerable<Car>> carsTask = client.GetCarsInAWrongWayAsync();
var cars = carsTask.Result;

因此,它继续调用 GetCarsInAWrongWayAsync , 它会一直运行直到它到达第一个 await (GetAsync 电话)。此时,GetCarsInAWrongWayAsync捕获其当前上下文(ASP.NET 请求上下文)并返回不完整的 Task<IEnumerable<Car>> .当 GetAsync下载完成,GetCarsInAWrongWayAsync将继续在该 ASP.NET 请求上下文中执行并(最终)完成它已经返回的任务。

但是,一旦GetCarsInAWrongWayAsync返回未完成的任务,CarsSync阻塞当前线程,等待该任务完成。请注意,当前线程位于该 ASP.NET 请求上下文中,因此 CarsSync会阻止 GetCarsInAWrongWayAsync永远不会恢复执行,导致死锁。

最后一点,GetCarsInAWrongWayAsync是一个好的方法。如果用ConfigureAwait(false)就更好了,但这实际上不是错误的CarsSync是导致死锁的方法;它调用Task<T>.Result 错的。适当的修复是更改 CarsSync :

public class HomeController : Controller
{
public async Task<ViewResult> CarsSync()
{
SampleAPIClient client = new SampleAPIClient();
var cars = await client.GetCarsInAWrongWayAsync();
return View("Index", model: cars);
}
}

关于c# - 使用 SynchronizationContext 时异步/等待死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34151179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com