diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index 6f9dd58dd..32696995f 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -33,7 +33,9 @@ sealed class SessionPoolSingleton : IDisposable } ~SessionPoolSingleton() { - ClearAllPools(); + // Use async for the finalizer due to possible deadlock + // when waiting for the CloseResponse task while closing the session + ClearAllPoolsAsync(); } public void Dispose() @@ -203,6 +205,16 @@ internal void ClearAllPools() } } + internal async void ClearAllPoolsAsync() + { + s_logger.Debug("SessionPool::ClearAllPoolsAsync"); + foreach (SFSession session in _sessionPool) + { + await session.CloseAsync(CancellationToken.None).ConfigureAwait(false); + } + _sessionPool.Clear(); + } + public void SetMaxPoolSize(int size) { _maxPoolSize = size;