diff --git a/Snowflake.Data/Core/Session/SessionPool.cs b/Snowflake.Data/Core/Session/SessionPool.cs index 5013da99f..1df54811c 100644 --- a/Snowflake.Data/Core/Session/SessionPool.cs +++ b/Snowflake.Data/Core/Session/SessionPool.cs @@ -36,7 +36,9 @@ internal SessionPool() ~SessionPool() { - ClearAllPools(); + // Use async for the finalizer due to possible deadlock + // when waiting for the CloseResponse task while closing the session + ClearAllPoolsAsync(); } public void Dispose() @@ -191,6 +193,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;