Skip to content

Performance | Remove manual state machine from setup phase of SqlBulkCopy - #4687

Merged
mdaigle merged 15 commits into
dotnet:mainfrom
edwardneal:async/sqlbulkcopy-native-async-pr1
Oct 23, 2026
Merged

mdaigle merged 15 commits into
dotnet:mainfrom
edwardneal:async/sqlbulkcopy-native-async-pr1

Conversation

@edwardneal

@edwardneal edwardneal commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

This is the same idea as #4685; strangely enough, it stops at the same boundary. The primary difference is that where #4685 handles the transport of rows, this PR handles the setup phase - metadata retrieval, connection resiliency and so on.

This PR removes the manual state machine orchestration from the outer layers of SqlBulkCopy, replacing it with native async/await. This simplifies those layers, although it leaves behind some modest scar tissue: slightly odd method names.

I've moved commit-by-commit, with a single cleanup layer at the end.

Issues

Contributes to #3459.

Testing

Existing SqlBulkCopy tests continue to pass. Performance benchmarking highlights no inexplicable performance regressions (although there's a little CPU noise - I'm testing against a local SQL Server), plus a few very minor reductions in memory usage.

Benchmark results

All benchmarks are run against .NET 10. A few points of note:

  • When comparing the PR to main, average duration is within the margin of error in both directions.
  • One very large deviation in BulkCopyDeep with UseCompatibilityAsync set to false. This is because I was benchmarking against a local SQL Server - the first test was slow, no matter which test it was.
  • UseCompatibilityAsync covers cases where the UseCompatibilityAsyncBehaviour and the UseCompatibilityProcessSni AppContext switches are set to true or false. I don't expect it to have a bearing here, since we're not making sufficiently heavy use of these code paths during startup.
  • Async bulk copy operations queue two fewer thread pool work items per operation.
Method Job UseCompatibilityAsync Mean Error StdDev Median Ratio RatioSD Gen0 Completed Work Items Lock Contentions Allocated Alloc Ratio
BulkCopyDeep PR False 184.461 ms 2.9670 ms 7.7117 ms 182.618 ms 0.86 0.04 - - - 4408.48 KB 1.00
BulkCopyDeep main False 213.391 ms 2.7853 ms 2.4691 ms 213.117 ms 1.00 0.02 - - - 4408.7 KB 1.00
BulkCopyDeepAsync PR False 182.038 ms 3.6229 ms 4.3127 ms 181.514 ms 0.97 0.03 - - - 4408.67 KB 1.00
BulkCopyDeepAsync main False 187.779 ms 3.7113 ms 3.4716 ms 187.839 ms 1.00 0.03 - 2.0000 - 4409.7 KB 1.00
BulkCopyWide PR False 422.218 ms 7.2473 ms 13.0684 ms 418.610 ms 0.97 0.07 2000.0000 - - 20550.02 KB 1.00
BulkCopyWide main False 438.592 ms 11.6337 ms 33.3792 ms 421.306 ms 1.01 0.10 2000.0000 - - 20550.18 KB 1.00
BulkCopyWideAsync PR False 419.418 ms 7.2186 ms 6.0278 ms 419.564 ms 1.00 0.02 2000.0000 - - 20550.25 KB 1.00
BulkCopyWideAsync main False 419.868 ms 6.9892 ms 6.5377 ms 419.195 ms 1.00 0.02 2000.0000 2.0000 - 20551.26 KB 1.00
BulkCopySmall PR False 3.766 ms 0.2262 ms 0.6526 ms 3.643 ms 1.04 0.29 - - - 39.23 KB 1.00
BulkCopySmall main False 3.786 ms 0.2830 ms 0.8210 ms 3.748 ms 1.05 0.33 - - - 39.4 KB 1.00
BulkCopySmallAsync PR False 3.684 ms 0.2739 ms 0.7901 ms 3.566 ms 1.04 0.32 - - - 39.42 KB 0.98
BulkCopySmallAsync main False 3.729 ms 0.2888 ms 0.8379 ms 3.524 ms 1.05 0.33 - 2.0000 - 40.4 KB 1.00
BulkCopyDeep PR True 185.761 ms 3.5688 ms 7.1272 ms 183.632 ms 1.02 0.04 - - - 482.68 KB 1.00
BulkCopyDeep main True 182.047 ms 3.4531 ms 3.3914 ms 181.379 ms 1.00 0.03 - - - 482.89 KB 1.00
BulkCopyDeepAsync PR True 180.206 ms 3.4210 ms 3.2000 ms 180.283 ms 0.98 0.02 - - - 482.95 KB 1.00
BulkCopyDeepAsync main True 183.391 ms 2.7161 ms 2.5406 ms 182.924 ms 1.00 0.02 - 2.0000 - 483.92 KB 1.00
BulkCopyWide PR True 418.264 ms 7.5490 ms 7.0613 ms 416.899 ms 1.00 0.02 2000.0000 - - 16624.26 KB 1.00
BulkCopyWide main True 419.426 ms 6.3767 ms 5.9647 ms 420.277 ms 1.00 0.02 2000.0000 - - 16624.45 KB 1.00
BulkCopyWideAsync PR True 480.362 ms 10.9795 ms 31.5023 ms 493.396 ms 1.13 0.08 2000.0000 - - 16624.4 KB 1.00
BulkCopyWideAsync main True 423.451 ms 5.5626 ms 8.6603 ms 421.035 ms 1.00 0.03 2000.0000 2.0000 - 16625.51 KB 1.00
BulkCopySmall PR True 3.781 ms 0.3031 ms 0.8795 ms 3.592 ms 1.12 0.34 - - - 15.73 KB 0.99
BulkCopySmall main True 3.495 ms 0.2457 ms 0.7050 ms 3.381 ms 1.04 0.29 - - - 15.89 KB 1.00
BulkCopySmallAsync PR True 3.986 ms 0.2524 ms 0.7241 ms 3.992 ms 1.14 0.30 - - - 15.91 KB 0.94
BulkCopySmallAsync main True 3.632 ms 0.2506 ms 0.7191 ms 3.586 ms 1.04 0.29 - 2.0000 - 16.89 KB 1.00

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@edwardneal
edwardneal marked this pull request as ready for review September 14, 2026 19:55
@edwardneal
edwardneal requested a review from a team as a code owner September 14, 2026 19:55
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@mdaigle mdaigle added this to the 8.0.0-preview1 milestone Sep 18, 2026
@cheenamalhotra cheenamalhotra moved this from To triage to In review in SqlClient Board Sep 23, 2026
@paulmedynski
paulmedynski requested a balanced review from Copilot September 30, 2026 12:54
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

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 review overview

🟡 Changes recommended

The reconnection path retains the obsolete parser and inner connection after recovery.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Refactors SqlBulkCopy setup orchestration to native async/await and adds .NET Framework cancellation support.

Changes:

  • Replaces continuation-based setup with ValueTask async methods.
  • Adds a .NET Framework Task.WaitAsync extension.
  • Simplifies reconnect, cleanup, and lock handling.
File Description
TaskExtensions.netfx.cs Adds cancellable task waiting for .NET Framework.
SqlBulkCopy.cs Modernizes bulk-copy setup and reconnection flow.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Sep 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.40351% with 44 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.43%. Comparing base (ce9f01b) to head (266dd9b).
⚠️ Report is 36 commits behind head on main.

Files with missing lines Patch % Lines
...Client/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs 61.94% 43 Missing ⚠️
...src/System/Threading/Tasks/TaskExtensions.netfx.cs 0.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (ce9f01b) and HEAD (266dd9b). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (ce9f01b) HEAD (266dd9b)
CI-SqlClient 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4687      +/-   ##
==========================================
- Coverage   71.88%   64.43%   -7.46%     
==========================================
  Files         290      286       -4     
  Lines       45056    67899   +22843     
==========================================
+ Hits        32389    43750   +11361     
- Misses      12667    24149   +11482     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.43% <61.40%> (?)

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.

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

Copy link
Copy Markdown
Contributor

/azp run sqlclient-pr

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@benrr101

Copy link
Copy Markdown
Contributor

/azp run sqlclient-pr

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

benrr101
benrr101 previously approved these changes Oct 21, 2026

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

Still a great PR, thanks for reviewing the comments! 🚢

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

One final comment

This provides a simple assurance that if the SqlBulkCopy instance is disposed of or closed during a bulk copy, there's enough state present to manually unlock the connection's parser lock

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.

🟡 Changes recommended

Disposal during a pending copy can cause cleanup to mask cancellation or the original failure with a NullReferenceException.

1 open issue
1 resolved since last review

Review effort: Balanced

mdaigle
mdaigle previously approved these changes Oct 21, 2026
@benrr101

Copy link
Copy Markdown
Contributor

/azp run sqlclient-pr

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@mdaigle

mdaigle commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

/azp run

@mdaigle
mdaigle enabled auto-merge (squash) October 22, 2026 20:36
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

@mdaigle
mdaigle merged commit e012b32 into dotnet:main Oct 23, 2026
51 of 124 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting for customer to Done in SqlClient Board Oct 23, 2026
@edwardneal
edwardneal deleted the async/sqlbulkcopy-native-async-pr1 branch October 23, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants