Skip to content

Commit 55ca37c

Browse files
authored
Add try-catch around getting open connection (#3202)
rework retry to avoid lambda capture
1 parent 87b5435 commit 55ca37c

File tree

2 files changed

+56
-32
lines changed

2 files changed

+56
-32
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs

+28-16
Original file line numberDiff line numberDiff line change
@@ -2445,9 +2445,16 @@ long firstAttemptStart
24452445
_cachedAsyncState.ResetAsyncState();
24462446
}
24472447

2448-
_activeConnection.GetOpenTdsConnection().DecrementAsyncCount();
2448+
try
2449+
{
2450+
_activeConnection.GetOpenTdsConnection().DecrementAsyncCount();
24492451

2450-
globalCompletion.TrySetException(e);
2452+
globalCompletion.TrySetException(e);
2453+
}
2454+
catch (Exception e2)
2455+
{
2456+
globalCompletion.TrySetException(e2);
2457+
}
24512458
}
24522459
else
24532460
{
@@ -2464,21 +2471,26 @@ long firstAttemptStart
24642471
TdsParserStaticMethods.GetRemainingTimeout(timeout, firstAttemptStart), true /*inRetry*/,
24652472
asyncWrite);
24662473

2467-
retryTask.ContinueWith(retryTsk =>
2468-
{
2469-
if (retryTsk.IsFaulted)
2470-
{
2471-
globalCompletion.TrySetException(retryTsk.Exception.InnerException);
2472-
}
2473-
else if (retryTsk.IsCanceled)
2474+
retryTask.ContinueWith(
2475+
static (Task<object> retryTask, object state) =>
24742476
{
2475-
globalCompletion.TrySetCanceled();
2476-
}
2477-
else
2478-
{
2479-
globalCompletion.TrySetResult(retryTsk.Result);
2480-
}
2481-
}, TaskScheduler.Default);
2477+
TaskCompletionSource<object> completion = (TaskCompletionSource<object>)state;
2478+
if (retryTask.IsFaulted)
2479+
{
2480+
completion.TrySetException(retryTask.Exception.InnerException);
2481+
}
2482+
else if (retryTask.IsCanceled)
2483+
{
2484+
completion.TrySetCanceled();
2485+
}
2486+
else
2487+
{
2488+
completion.TrySetResult(retryTask.Result);
2489+
}
2490+
},
2491+
state: globalCompletion,
2492+
TaskScheduler.Default
2493+
);
24822494
}
24832495
catch (Exception e2)
24842496
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs

+28-16
Original file line numberDiff line numberDiff line change
@@ -2718,9 +2718,16 @@ private bool TriggerInternalEndAndRetryIfNecessary(CommandBehavior behavior, obj
27182718
{
27192719
_cachedAsyncState.ResetAsyncState();
27202720
}
2721-
_activeConnection.GetOpenTdsConnection().DecrementAsyncCount();
2721+
try
2722+
{
2723+
_activeConnection.GetOpenTdsConnection().DecrementAsyncCount();
27222724

2723-
globalCompletion.TrySetException(e);
2725+
globalCompletion.TrySetException(e);
2726+
}
2727+
catch (Exception e2)
2728+
{
2729+
globalCompletion.TrySetException(e2);
2730+
}
27242731
}
27252732
else
27262733
{
@@ -2735,21 +2742,26 @@ private bool TriggerInternalEndAndRetryIfNecessary(CommandBehavior behavior, obj
27352742
_internalEndExecuteInitiated = false;
27362743
Task<object> retryTask = (Task<object>)retryFunc(behavior, null, stateObject, TdsParserStaticMethods.GetRemainingTimeout(timeout, firstAttemptStart), true/*inRetry*/, asyncWrite);
27372744

2738-
retryTask.ContinueWith(retryTsk =>
2739-
{
2740-
if (retryTsk.IsFaulted)
2741-
{
2742-
globalCompletion.TrySetException(retryTsk.Exception.InnerException);
2743-
}
2744-
else if (retryTsk.IsCanceled)
2745+
retryTask.ContinueWith(
2746+
static (Task<object> retryTask, object state) =>
27452747
{
2746-
globalCompletion.TrySetCanceled();
2747-
}
2748-
else
2749-
{
2750-
globalCompletion.TrySetResult(retryTsk.Result);
2751-
}
2752-
}, TaskScheduler.Default);
2748+
TaskCompletionSource<object> completion = (TaskCompletionSource<object>)state;
2749+
if (retryTask.IsFaulted)
2750+
{
2751+
completion.TrySetException(retryTask.Exception.InnerException);
2752+
}
2753+
else if (retryTask.IsCanceled)
2754+
{
2755+
completion.TrySetCanceled();
2756+
}
2757+
else
2758+
{
2759+
completion.TrySetResult(retryTask.Result);
2760+
}
2761+
},
2762+
state: globalCompletion,
2763+
TaskScheduler.Default
2764+
);
27532765
}
27542766
catch (Exception e2)
27552767
{

0 commit comments

Comments
 (0)