Repository navigation
Performance | Remove manual state machine from setup phase of SqlBulkCopy - #4687
Conversation
This is a misnamed method on SqlBulkCopy which calls a misnamed method on SqlConnection. The only thing it does is call RemoveWeakReference.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The reconnection path retains the obsolete parser and inner connection after recovery.
Review effort: Balanced
Findings: 1
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
ValueTaskasync methods. - Adds a .NET Framework
Task.WaitAsyncextension. - 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 Report❌ Patch coverage is
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
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:
|
|
/azp run sqlclient-pr |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
/azp run sqlclient-pr |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
benrr101
left a comment
There was a problem hiding this comment.
Still a great PR, thanks for reviewing the comments! 🚢
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
|
/azp run sqlclient-pr |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. |


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:
BulkCopyDeepwithUseCompatibilityAsyncset tofalse. This is because I was benchmarking against a local SQL Server - the first test was slow, no matter which test it was.UseCompatibilityAsynccovers cases where theUseCompatibilityAsyncBehaviourand theUseCompatibilityProcessSniAppContext 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.