Skip to content

Async Generic Helpers#4334

Merged
paulmedynski merged 17 commits into
mainfrom
dev/russellben/async-helpers-generic
Jun 23, 2026
Merged

Async Generic Helpers#4334
paulmedynski merged 17 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 75.04873% with 128 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.73%. Comparing base (bfbdd30) to head (9e2309f).
⚠️ Report is 30 commits behind head on main.

Files with missing lines Patch % Lines
.../Microsoft/Data/SqlClient/Utilities/AsyncHelper.cs 88.62% 48 Missing ⚠️
...Client/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs 9.09% 30 Missing ⚠️
.../src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs 0.00% 18 Missing ⚠️
.../Microsoft/Data/SqlClient/SqlCommand.Encryption.cs 0.00% 10 Missing ⚠️
...rc/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs 0.00% 7 Missing ⚠️
...ent/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs 12.50% 7 Missing ⚠️
...qlClient/src/Microsoft/Data/SqlClient/TdsParser.cs 33.33% 4 Missing ⚠️
...c/Microsoft/Data/SqlClient/TdsParserStateObject.cs 55.55% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4334      +/-   ##
==========================================
- Coverage   66.69%   63.73%   -2.96%     
==========================================
  Files         284      281       -3     
  Lines       43238    66492   +23254     
==========================================
+ Hits        28836    42380   +13544     
- Misses      14402    24112    +9710     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 63.73% <75.04%> (?)

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
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs Outdated
@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.

@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Jun 12, 2026
mdaigle
mdaigle previously approved these changes Jun 12, 2026
Copilot AI review requested due to automatic review settings June 18, 2026 23:39
@benrr101 benrr101 moved this from Waiting for customer to In review in SqlClient Board Jun 18, 2026
@benrr101
benrr101 requested a review from paulmedynski June 18, 2026 23:40

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 9 comments.

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs Outdated

@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.

I agree with some of the Copilot feedback.

@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Jun 19, 2026
Copilot AI review requested due to automatic review settings June 22, 2026 18:50
@benrr101
benrr101 requested a review from paulmedynski June 22, 2026 18:51
@benrr101 benrr101 moved this from Waiting for customer to In review in SqlClient Board Jun 22, 2026
@benrr101
benrr101 requested a review from mdaigle June 22, 2026 18:52

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 4 comments.

@paulmedynski
paulmedynski merged commit 83412c7 into main Jun 23, 2026
355 checks passed
@paulmedynski
paulmedynski deleted the dev/russellben/async-helpers-generic branch June 23, 2026 23:25
@github-project-automation github-project-automation Bot moved this from In review to Done in SqlClient Board Jun 23, 2026
cheenamalhotra pushed a commit that referenced this pull request Jul 14, 2026
* Extract AsyncHelper from SqlUtil.cs into the utilities namespace

# 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

* Implement AsyncHelpers with generic state, add unit tests

* Change existing async helper calls in SqlBulkCopy.cs

* Change existing async helper calls in TdsParser.cs

* Change existing async helper calls in TdsParser.cs

* Change existing async helper calls in SqlCommand

* Remove old tests

* Comments from copilot round 1

* Address feedback from wraith - including constraining state types to class types.

* Fix tests for last change
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: Done

Development

Successfully merging this pull request may close these issues.

ExecuteNonQueryAsync occur UnobservedTaskException

5 participants