1. 程式人生 > >又踩.NET Core的坑:在同步方法中呼叫非同步方法Wait時發生死鎖(deadlock)

又踩.NET Core的坑:在同步方法中呼叫非同步方法Wait時發生死鎖(deadlock)

之前在將 Memcached 客戶端 EnyimMemcached 遷移 .NET Core 時被這個“坑”坑的刻骨銘心(詳見以下連結),當時以為只是在建構函式中呼叫非同步方法(注:這裡的非同步方法都是指基於Task的)才會出線死鎖(deadlock)問題。

StackExchange.Redis 中死鎖問題發生在下面的程式碼:

private static ConnectionMultiplexer ConnectImpl(Func<ConnectionMultiplexer> multiplexerFactory, TextWriter log)
{
    IDisposable killMe 
= null; try { var muxer = multiplexerFactory(); killMe = muxer; // note that task has timeouts internally, so it might take *just over* the regular timeout var task = muxer.ReconfigureAsync(true, false, log, null, "connect"); if (!task.Wait(muxer.SyncConnectTimeout(true
))) { task.ObserveErrors(); if (muxer.RawConfig.AbortOnConnectFail) { throw ExceptionFactory.UnableToConnect("Timeout"); } } if (!task.Result) throw ExceptionFactory.UnableToConnect(muxer.failureMessage); killMe
= null; return muxer; } finally { if (killMe != null) try { killMe.Dispose(); } catch { } } }

ConnectImpl() 是一個同步方法,muxer.ReconfigureAsync() 是一個 async 非同步方法。在 Linux 上執行時, task.Wait(muxer.SyncConnectTimeout(true)) 會因為等待超時而返回 false ,從而出現下面的錯誤:

StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. Timeout
   at StackExchange.Redis.ConnectionMultiplexer.ConnectImpl(Func`1 multiplexerFactory, TextWriter log)

如果改為 task.Wait() ,在 ASP.NET Core 程式中呼叫時,請求會因為死鎖而卡死。

這是一個典型的同步方法呼叫非同步方法在 Wait 時的死鎖問題(詳見 Don't Block on Async Code),通常的解決方法是使用 .ConfigureAwait(false); (非同步任務執行完成時不獲取SynchronizationContext)。StackExchange.Redis 的開發者當然知道這一點,在 muxer.ReconfigureAsync()  中呼叫每一個非同步方法時都加上了 .ConfigureAwait(false); 。但我們遇到的實際情況顯示,這一招在 .NET Framework 中管用,在 .NET Core 中卻不管用,這可能與 .NET Core 在非同步機制上的改變有關,比如在非同步方法中 System.Threading.SynchronizationContext.Current 的值總是為 null ,詳見 ASP.NET Core 1.0 SynchronizationContext 。

這個問題在 Liunx 上很容易出現,而在 Windows 上需要一定的併發請求才會出現。

public IReadOnlyCollection<XElement> GetAllElements()
{
    var blobRef = CreateFreshBlobRef();

    // Shunt the work onto a ThreadPool thread so that it's independent of any
    // existing sync context or other potentially deadlock-causing items.

    var elements = Task.Run(() => GetAllElementsAsync(blobRef)).GetAwaiter().GetResult();
    return new ReadOnlyCollection<XElement>(elements);
}

參考上面的程式碼,在 StackExchange.Redis 中的 ConnectImpl() 方法中改為 Task.Run() 呼叫非同步方法,死鎖問題依舊。