Skip to content

Add try-catch and ensure task exception is set in SqlCommand #3202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2445,9 +2445,16 @@ long firstAttemptStart
_cachedAsyncState.ResetAsyncState();
}

_activeConnection.GetOpenTdsConnection().DecrementAsyncCount();
try
{
_activeConnection.GetOpenTdsConnection().DecrementAsyncCount();

globalCompletion.TrySetException(e);
globalCompletion.TrySetException(e);
}
catch (Exception e2)
{
globalCompletion.TrySetException(e2);
}
}
else
{
Expand All @@ -2464,21 +2471,26 @@ long firstAttemptStart
TdsParserStaticMethods.GetRemainingTimeout(timeout, firstAttemptStart), true /*inRetry*/,
asyncWrite);

retryTask.ContinueWith(retryTsk =>
{
if (retryTsk.IsFaulted)
{
globalCompletion.TrySetException(retryTsk.Exception.InnerException);
}
else if (retryTsk.IsCanceled)
retryTask.ContinueWith(
static (Task<object> retryTask, object state) =>
{
globalCompletion.TrySetCanceled();
}
else
{
globalCompletion.TrySetResult(retryTsk.Result);
}
}, TaskScheduler.Default);
TaskCompletionSource<object> completion = (TaskCompletionSource<object>)state;
if (retryTask.IsFaulted)
{
completion.TrySetException(retryTask.Exception.InnerException);
}
else if (retryTask.IsCanceled)
{
completion.TrySetCanceled();
}
else
{
completion.TrySetResult(retryTask.Result);
}
},
state: globalCompletion,
TaskScheduler.Default
);
}
catch (Exception e2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2718,9 +2718,16 @@ private bool TriggerInternalEndAndRetryIfNecessary(CommandBehavior behavior, obj
{
_cachedAsyncState.ResetAsyncState();
}
_activeConnection.GetOpenTdsConnection().DecrementAsyncCount();
try
{
_activeConnection.GetOpenTdsConnection().DecrementAsyncCount();

globalCompletion.TrySetException(e);
globalCompletion.TrySetException(e);
}
catch (Exception e2)
{
globalCompletion.TrySetException(e2);
}
}
else
{
Expand All @@ -2735,21 +2742,26 @@ private bool TriggerInternalEndAndRetryIfNecessary(CommandBehavior behavior, obj
_internalEndExecuteInitiated = false;
Task<object> retryTask = (Task<object>)retryFunc(behavior, null, stateObject, TdsParserStaticMethods.GetRemainingTimeout(timeout, firstAttemptStart), true/*inRetry*/, asyncWrite);

retryTask.ContinueWith(retryTsk =>
{
if (retryTsk.IsFaulted)
{
globalCompletion.TrySetException(retryTsk.Exception.InnerException);
}
else if (retryTsk.IsCanceled)
retryTask.ContinueWith(
static (Task<object> retryTask, object state) =>
{
globalCompletion.TrySetCanceled();
}
else
{
globalCompletion.TrySetResult(retryTsk.Result);
}
}, TaskScheduler.Default);
TaskCompletionSource<object> completion = (TaskCompletionSource<object>)state;
if (retryTask.IsFaulted)
{
completion.TrySetException(retryTask.Exception.InnerException);
}
else if (retryTask.IsCanceled)
{
completion.TrySetCanceled();
}
else
{
completion.TrySetResult(retryTask.Result);
}
},
state: globalCompletion,
TaskScheduler.Default
);
}
catch (Exception e2)
{
Expand Down
Loading