From ecaa4bc038f619cca0782fc5632eefa102754815 Mon Sep 17 00:00:00 2001 From: dengel Date: Thu, 14 Nov 2019 13:58:34 -0800 Subject: [PATCH 1/2] Fix connection resiliency check --- .../netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs index 20e32310f4..2e8d5d9cf7 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -1135,8 +1135,7 @@ internal Task ValidateAndReconnect(Action beforeDisconnect, int timeout) catch (SqlException) { } - // use Task.Factory.StartNew with state overload instead of Task.Run to avoid anonymous closure context capture in method scope and avoid the unneeded allocation - runningReconnect = Task.Factory.StartNew(state => ReconnectAsync((int)state), timeout, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); + runningReconnect = Task.Run(() => ReconnectAsync(timeout)); // if current reconnect is not null, somebody already started reconnection task - some kind of race condition Debug.Assert(_currentReconnectionTask == null, "Duplicate reconnection tasks detected"); _currentReconnectionTask = runningReconnect; From c2dd8e47df391a7f79a006c7c4b2adf8a7d905dc Mon Sep 17 00:00:00 2001 From: dengel Date: Thu, 14 Nov 2019 16:33:00 -0800 Subject: [PATCH 2/2] Revert fix and add Unwrap() to StartNew instead --- .../netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs index 2e8d5d9cf7..e2c6feedfa 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -1135,7 +1135,8 @@ internal Task ValidateAndReconnect(Action beforeDisconnect, int timeout) catch (SqlException) { } - runningReconnect = Task.Run(() => ReconnectAsync(timeout)); + // use Task.Factory.StartNew with state overload instead of Task.Run to avoid anonymous closure context capture in method scope and avoid the unneeded allocation + runningReconnect = Task.Factory.StartNew(state => ReconnectAsync((int)state), timeout, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default).Unwrap(); // if current reconnect is not null, somebody already started reconnection task - some kind of race condition Debug.Assert(_currentReconnectionTask == null, "Duplicate reconnection tasks detected"); _currentReconnectionTask = runningReconnect; @@ -1160,7 +1161,7 @@ internal Task ValidateAndReconnect(Action beforeDisconnect, int timeout) OnError(SQL.CR_UnrecoverableServer(ClientConnectionId), true, null); } } // ValidateSNIConnection - } // sessionRecoverySupported + } // sessionRecoverySupported } // connectRetryCount>0 } else