Async Generic Helpers#4334
Conversation
# Conflicts: # src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj # src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.netcore.cs # src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs # src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj # src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.netfx.cs # src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs # src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs # src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs # src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs # src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs # src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs # src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs # src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs
There was a problem hiding this comment.
Pull request overview
This PR refactors the internal async continuation/timeout helpers by introducing a new Microsoft.Data.SqlClient.Utilities.AsyncHelper with generic state overloads, updates several product call sites to use the new APIs, and adds new UnitTests coverage (plus a Moq dependency) to validate the helper behaviors.
Changes:
- Added a new internal
Utilities/AsyncHelperimplementation with generic-state continuation helpers and timeout helpers, and removed the legacyAsyncHelperpreviously embedded inSqlUtil.cs. - Updated multiple async call sites (e.g.,
SqlCommand,SqlBulkCopy,TdsParser*) to use the new stateful continuation APIs. - Added Moq-based UnitTests for
AsyncHelperbehaviors plus small Moq helper extensions.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/tests/UnitTests/Utilities/MockExtensions.cs | Adds Moq extension helpers to reduce boilerplate in new unit tests. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/Utilities/AsyncHelperTest.cs | Adds extensive unit coverage for continuation helpers and timeout/unobserved-exception behavior. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj | Introduces Moq PackageReference for unit test projects. |
| src/Microsoft.Data.SqlClient/tests/Directory.Packages.props | Adds central package version for Moq in tests. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Utilities/AsyncHelper.cs | New generic-state async continuation/timeout helper implementation. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs | Converts a continuation callback to use typed state via new helper. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs | Updates continuation usage to new CreateContinuationTaskWithState overloads. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlUtil.cs | Removes legacy internal AsyncHelper previously defined here. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnection.cs | Imports new helper namespace for existing WaitForCompletion usage. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs | Updates async continuations to use new helper parameter naming and typed state patterns. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs | Updates continuations/timeouts to new helper overloads and typed state. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs | Updates continuations to new helper overloads and typed state patterns. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Encryption.cs | Updates continuation usage to new typed-state helper overloads. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs | Updates multiple continuations/timeouts to new helper APIs and typed-state patterns. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs | Imports new helper namespace and adjusts usings/conditionals. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4334 +/- ##
==========================================
- Coverage 66.69% 64.80% -1.89%
==========================================
Files 284 281 -3
Lines 43238 66391 +23153
==========================================
+ Hits 28836 43023 +14187
- Misses 14402 23368 +8966
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| onCancellation: static state => ((StrongBox<CancellationTokenRegistration>)state).Value.Dispose(), | ||
| exceptionConverter: ex => SQL.BulkLoadInvalidDestinationTable(_destinationTableName, ex) | ||
| ); | ||
| onFailure: (regReconnectCancel2, exception) => |
There was a problem hiding this comment.
Can this be static or is it now capturing instance state? if it's capturing that's a slight memory performance regression.
There was a problem hiding this comment.
No this one is not static. There are still improvements that can be made to make more callbacks static. At this point, having a separate overload for a one-off situation where the exception type needs to be changed isn't worth it. It's also worth noting that the success callback is not static, nor is the exception converter itself. As such, I'm not sure it will make much of a difference.
There was a problem hiding this comment.
It depends how often it's called. If it's once per field it could be significant. It's unlikely to cause time problems but it could add to memory thrashing. I only called this one out because it's changed from static to non-static, existing non-statics were already being pinned by some sort of capture.
I did a pass through the whole codebase years ago and made everything that was capable static, that's why there's weird use of Tuple and some TaskCompletionSource state smuggling
| completion: localCompletion, | ||
| taskToContinue: writeTask, | ||
| taskCompletionSource: localCompletion, | ||
| state: Tuple.Create(this, localCompletion), |
There was a problem hiding this comment.
Can this one use the two state parameter form used earlier to avoid the tuple creation again?
There was a problem hiding this comment.
Sure this one can utilize the two state parameter version
| @@ -1272,13 +1269,12 @@ internal Task ExecuteFlush() | |||
| else | |||
| { | |||
| return AsyncHelper.CreateContinuationTaskWithState( | |||
There was a problem hiding this comment.
Unless state is constained to class this pattern could be used with a struct and lead to a hard to find bug. If we don't want the constraint it might be worth an assertion of reference type.
There was a problem hiding this comment.
Fair enough - I'll try to document why the constraint exists
| --> | ||
| <PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" ExcludeAssets="build;buildTransitive" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
| <PackageReference Include="Moq" /> |
There was a problem hiding this comment.
I remember that there was some controversy around Moq that caused a lot of people to not want to use it anymore. Is it worth considering other newer alternatives like https://github.com/guinhx/NimbleMock (MIT)
There was a problem hiding this comment.
It appears the controversy has been resolved, though the goodwill of the project has been damaged. Improved perf is definitely nice, though I'm not familiar enough with NimbleMock to endorse it yet. The other big contender, NSubstitute is an option, but I've presented the pros/cons in the past and I personally prefer the syntax of Moq. There doesn't appear to be any legal reasons to use something else, so for now, let's focus on the functional changes rather than the testing minutia.
| // Act | ||
| // - Run task that will always time out | ||
| TaskCompletionSource<object?> tcs = new(); | ||
| AsyncHelper.WaitForCompletion( | ||
| tcs.Task, | ||
| timeoutInSeconds: 1, | ||
| onTimeout: null, | ||
| rethrowExceptions: true); | ||
|
|
||
| // - Task has timed out, simulate faulting task completion source | ||
| tcs.SetException(new Exception("late failure")); | ||
|
|
||
| // - Force collection of unobserved task | ||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
| GC.Collect(); |
| await RunWithTimeout(taskCompletionSource.Task, RunTimeout); | ||
|
|
||
| // Assert | ||
| // - taskCompletionSource should have been cancelled, regardless of mockOnSuccess throwing |
| // Arrange | ||
| // - Task to continue that is cancelled | ||
| Task taskToContinue = Task.FromException(new Exception()); | ||
| TaskCompletionSource<object?> taskCompletionSource = GetTaskCompletionSource(); | ||
| Mock<Action> mockOnSuccess = new(); | ||
| Mock<Action> mockOnCancellation = new(); | ||
|
|
||
| // Act | ||
| AsyncHelper.ContinueTask( | ||
| taskToContinue, | ||
| taskCompletionSource, | ||
| mockOnSuccess.Object, | ||
| onFailure: null, | ||
| mockOnCancellation.Object); | ||
| await RunWithTimeout(taskCompletionSource.Task, RunTimeout); | ||
|
|
||
| // Assert | ||
| // - taskCompletionSource should have been cancelled | ||
| Assert.Equal(TaskStatus.Faulted, taskCompletionSource.Task.Status); |
| await RunWithTimeout(taskCompletionSource.Task, RunTimeout); | ||
|
|
||
| // Assert | ||
| // - taskCompletionSource should have been cancelled, regardless of mockOnSuccess throwing |
| await RunWithTimeout(taskCompletionSource.Task, RunTimeout); | ||
|
|
||
| // Assert | ||
| // - taskCompletionSource should have been cancelled |
| await RunWithTimeout(taskCompletionSource.Task, RunTimeout); | ||
|
|
||
| // Assert | ||
| // - taskCompletionSource should have been cancelled, regardless of mockOnSuccess throwing |
| await RunWithTimeout(taskCompletionSource.Task, RunTimeout); | ||
|
|
||
| // Assert | ||
| // - taskCompletionSource should have been cancelled |
| public async Task CreateContinuationTask_TaskCancelsNoHandler() | ||
| { | ||
| // Arrange | ||
| // - Task to continue completed successfully |
Description
This is a rebuild of #3705. Very small changes this time. The goal is to make the state object(s) used by the async helpers be generic types, so that the callbacks don't need to unwrap the state object(s) to their type. This provides better type safety for these methods. Additionally, the new versions of the helpers are more consistently written, which will be easier to maintain going forward.
In the previous PR, many of the async helper calls were updated to better utilize the generic state object(s). This made the PR very large and prone to failures. In an effort to get this reviewed more quickly, I've just taken out the most important changes from that PR (the helpers themselves) and made the associated changes smaller (fix calls that were not compiling).
This PR maintains the unobserved exception changes, and should resolve #2104 and #3720
Testing
An entire suite of unit tests for the helpers were added, and the old reflection-based tests were removed.
The PR validation tests have been passing before moving from draft to ready state.