From 9237a3da69daf1decee4db037f4d3122a1da2bee Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf <115584722+sfc-gh-ext-simba-lf@users.noreply.github.com> Date: Thu, 26 Oct 2023 08:07:55 -0700 Subject: [PATCH] SNOW-914532: Use async for session pool finalizer (#790) ### Description Regarding issue 648 The PR fixes a hanging issue due to a [possible deadlock](https://github.com/snowflakedb/snowflake-connector-net/blob/127343fb4f8a55597df3bf3f20e64e18975fcb62/Snowflake.Data/Core/RestRequester.cs#L54) by using async instead when the finalizer tries to close all the sessions in the pool ### Checklist - [x] Code compiles correctly - [x] Code is formatted according to [Coding Conventions](../CodingConventions.md) - [x] Created tests which fail without the change (if possible) - [x] All tests passing (`dotnet test`) - [x] Extended the README / documentation, if necessary - [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name --- Snowflake.Data/Core/Session/SessionPool.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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;