Skip to content

Async Generic Helpers#4334

Open
benrr101 wants to merge 14 commits into
mainfrom
dev/russellben/async-helpers-generic
Open

Async Generic Helpers#4334
benrr101 wants to merge 14 commits into
mainfrom
dev/russellben/async-helpers-generic

Conversation

@benrr101

@benrr101 benrr101 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

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.

benrr101 added 6 commits June 2, 2026 13:10
# 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
Copilot AI review requested due to automatic review settings June 3, 2026 03:52
@benrr101 benrr101 added the DO NOT MERGE PRs that are created for test reasons, should not be merged. label Jun 3, 2026
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Jun 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/AsyncHelper implementation with generic-state continuation helpers and timeout helpers, and removed the legacy AsyncHelper previously embedded in SqlUtil.cs.
  • Updated multiple async call sites (e.g., SqlCommand, SqlBulkCopy, TdsParser*) to use the new stateful continuation APIs.
  • Added Moq-based UnitTests for AsyncHelper behaviors 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.

Copilot AI review requested due to automatic review settings June 4, 2026 17:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot stopped reviewing on behalf of benrr101 due to an error June 4, 2026 18:56
@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.64706% with 114 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.30%. Comparing base (bfbdd30) to head (d670d0a).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
.../Microsoft/Data/SqlClient/Utilities/AsyncHelper.cs 89.44% 44 Missing ⚠️
...Client/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs 12.12% 29 Missing ⚠️
.../src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs 22.22% 14 Missing ⚠️
.../Microsoft/Data/SqlClient/SqlCommand.Encryption.cs 0.00% 10 Missing ⚠️
...ent/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs 12.50% 7 Missing ⚠️
...c/Microsoft/Data/SqlClient/TdsParserStateObject.cs 44.44% 5 Missing ⚠️
...qlClient/src/Microsoft/Data/SqlClient/TdsParser.cs 50.00% 4 Missing ⚠️
...rc/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4334      +/-   ##
==========================================
- Coverage   66.69%   64.30%   -2.39%     
==========================================
  Files         284      281       -3     
  Lines       43238    66390   +23152     
==========================================
+ Hits        28836    42695   +13859     
- Misses      14402    23695    +9293     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.30% <77.64%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@benrr101 benrr101 added Code Health 💊 Issues/PRs that are targeted to source code quality improvements. and removed DO NOT MERGE PRs that are created for test reasons, should not be merged. labels Jun 4, 2026
@benrr101 benrr101 marked this pull request as ready for review June 4, 2026 22:50
@benrr101 benrr101 requested a review from a team as a code owner June 4, 2026 22:50
@benrr101 benrr101 added this to the 7.1.0-preview2 milestone Jun 4, 2026
@benrr101 benrr101 changed the title [DRAFT] Async Generic Helpers Async Generic Helpers Jun 4, 2026
onCancellation: static state => ((StrongBox<CancellationTokenRegistration>)state).Value.Dispose(),
exceptionConverter: ex => SQL.BulkLoadInvalidDestinationTable(_destinationTableName, ex)
);
onFailure: (regReconnectCancel2, exception) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be static or is it now capturing instance state? if it's capturing that's a slight memory performance regression.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we're OK here, since I doubt these failure/error conditions are hot paths.

We could make all of these lambdas static if we moved the callback bodies onto the classes as instance methods (or class methods if they really didn't need any object state). That might be an interesting investigation to see how messy it gets with a bunch of small callback methods defined on the classes themselves rather than inline.

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs Outdated
Copilot AI review requested due to automatic review settings June 5, 2026 21:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 8 comments.

@mdaigle mdaigle moved this from To triage to In review in SqlClient Board Jun 5, 2026
@paulmedynski paulmedynski enabled auto-merge (squash) June 8, 2026 09:27

@paulmedynski paulmedynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely a déjà vu PR 😄 . I will look at the tests once my current feedback has been addressed.

onCancellation: static state => ((StrongBox<CancellationTokenRegistration>)state).Value.Dispose(),
exceptionConverter: ex => SQL.BulkLoadInvalidDestinationTable(_destinationTableName, ex)
);
onFailure: (regReconnectCancel2, exception) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we're OK here, since I doubt these failure/error conditions are hot paths.

We could make all of these lambdas static if we moved the callback bodies onto the classes as instance methods (or class methods if they really didn't need any object state). That might be an interesting investigation to see how messy it gets with a bunch of small callback methods defined on the classes themselves rather than inline.

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs Outdated
@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Jun 8, 2026
@Wraith2

Wraith2 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

We could make all of these lambdas static if we moved the callback bodies onto the classes as instance methods (or class methods if they really didn't need any object state). That might be an interesting investigation to see how messy it gets with a bunch of small callback methods defined on the classes themselves rather than inline.

It gets very messy. If we change from a capturing lambda to an instance callback we gain nothing we only change the type of the allocation from a compiler generated async state machine to a delegate per call. In order to gain you have to have a static lambda and a static target so the delegate is one-time instantiated by the compiler and you have to have your instance as a state parameter which burns the (Task, object) state parameter of the default continuation shape forcing you to pack any additional state into a compound object, which has to be a ref type. This is why the weird use of Tuple<,> and not valuetuple exists in the current code.

In practice I found that moving the callbacks into static trampolines that call a specific instance method on the state param makes the code more difficult to follow so it is only used when it is on hot paths where the allocation reduction is significant enough to outweigh the readability concerns.

@benrr101 benrr101 moved this from Waiting for customer to In review in SqlClient Board Jun 9, 2026

@paulmedynski paulmedynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking better - just 2 outstanding comments.

@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Jun 11, 2026
Remove `!` that wasn't necessary because of Debug.Assert above
Add documentation as per @paulmedynski's comments
Copilot AI review requested due to automatic review settings June 11, 2026 20:40
@benrr101 benrr101 moved this from Waiting for customer to In review in SqlClient Board Jun 11, 2026
@benrr101 benrr101 requested a review from paulmedynski June 11, 2026 20:41

@mdaigle mdaigle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one testing request

_ = task.Exception;
},
CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use TaskContinuationOptions.ExecuteSynchronously because the work is so light here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I wasn't super sure on this either, since these were 🤖 suggestions. Here's the explanation I got:

ExecuteSynchronously

Despite its name, ExecuteSynchronously does not guarantee synchronous execution. It asks the selected scheduler to run the continuation inline on the thread that
completes the antecedent task, when the scheduler considers that safe.

Without it:

  1. Task.Delay completes.
  2. The continuation is queued to the ThreadPool.
  3. Another ThreadPool work item eventually runs the continuation.

With it:

  1. Task.Delay completes.
  2. The timeout continuation can immediately run on the completing thread.

Benefits:

  • Avoids an additional ThreadPool queue operation.
  • Reduces allocations/scheduling overhead.
  • Reduces latency between the timeout expiring and the exception being set.

Risks:

  • The completing thread must execute the continuation.
  • Blocking or expensive continuation work delays that thread.
  • Inline execution introduces reentrancy and ordering differences.
  • Completing the TaskCompletionSource may itself synchronously execute downstream continuations because these sources do not use RunContinuationsAsynchronously.
  • Long chains of synchronous continuations can increase stack depth.

For the timeout helpers, the continuation appears small:

  if (!task.IsCanceled && !taskCompletionSource.Task.IsCompleted)
  {
      taskCompletionSource.TrySetException(onTimeout());
  }

However, onTimeout() and TrySetException() can trigger more work than is immediately visible. Consequently, ExecuteSynchronously is a reasonable optimization, but it is
not required for correctness and is less obviously beneficial than TaskScheduler.Default.

@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Jun 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 5 comments.

@benrr101

Copy link
Copy Markdown
Contributor Author

👨‍🦱 comments were also addressed :) (@paulmedynski @mdaigle )

@benrr101 benrr101 moved this from Waiting for customer to In review in SqlClient Board Jun 11, 2026

@paulmedynski paulmedynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just looking for some doc updates. Code looks good.

/// * Will try to set exception on <paramref name="taskCompletionSource"/>
/// * Cancelled
/// * <paramref name="onCancellation"/> is called (if provided)
/// * Any exception thrown by <paramref name="onCancellation"/> will be logged and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same goes for onFailure too.

mockOnCancellation.VerifyNeverCalled();
}

[Fact]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get some docs on each test method that explains what it's testing? Sorry I didn't notice this sooner.

@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Code Health 💊 Issues/PRs that are targeted to source code quality improvements.

Projects

Status: Waiting for customer

Development

Successfully merging this pull request may close these issues.

ExecuteNonQueryAsync occur UnobservedTaskException

5 participants