Repository navigation
[8.0.0 Cherry-pick] Fix SqlTransaction.Dispose racing the close of a broken connection - #5053
Merged
Merged
Conversation
…4972) * Add tests for SqlTransaction.Dispose racing the close of a broken connection Each test breaks the connection while an async command is pending in a transaction. The break goes through ProxyServer, either as a timeout whose attention the server never acknowledges or as a network reset. The driver faults the command first and closes the connection afterwards on another thread. The tests hold that close at the internal connection's close lock. They dispose the transaction either before the close runs, or while it runs. A SqlClient scope-trace listener on the disposing thread places the second case between Dispose's null check and Rollback's dereference. On main, the dispose throws InvalidOperationException ("The connection is closed") or NullReferenceException from SqlInternalTransaction.Rollback(). * Fix SqlTransaction.Dispose racing the close of a broken connection The background close of a broken connection zombies the transaction and releases the parser. SqlInternalTransaction.Rollback() read _innerConnection several times without a lock, so a dispose that raced that close could throw. Rollback() now reads the connection once and returns if the transaction is already zombied. When disposing on a connection that is doomed, broken or closed, it returns without a rollback. Nothing can be sent there, and the server ends the transaction with the session. IsLockedForBulkCopy no longer dereferences a released parser. Fixes #4971 * Skip the transaction dispose race tests on Azure SQL and named instances The tests connect through ProxyServer, which rewrites the data source to 127.0.0.1. On Azure SQL and on the named-instance setup the server certificate never matches that name, so the connection failed in the pre-login handshake before any test code ran. Skip them there, as the other ProxyServer tests do. * Add a test for the connection breaking between Rollback's check and validation After an unacknowledged timeout, the attention-timeout callback faults the command's task and marks the parser broken a few lines later. A dispose started by the fault can pass Rollback()'s broken-connection check and then fail in ValidateConnectionForExecute, which still throws while disposing. Nothing observable runs in that window, so the test uses a new DEBUG-only hook, SqlInternalTransaction.s_afterRollbackConnectionCheck, following the existing test hooks in TdsParserStateObject. The test marks the parser broken from the hook on the disposing thread. On this commit the dispose throws InvalidOperationException ("The connection is closed"). Also unsubscribe BackgroundCloseGate's StateChange handler before its event is disposed. * Fix SqlTransaction.Dispose when the connection breaks before Rollback validates it If the connection breaks after Rollback() has checked it, the validation throws. While disposing, Rollback() now returns in that case too, for the same reason as the check: nothing can be sent, and the server ends the transaction with the session. An explicit Rollback() still throws.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
/azp run |
|
Azure Pipelines: Successfully started running 3 pipeline(s). |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release/8.0 #5053 +/- ##
===============================================
- Coverage 73.90% 69.47% -4.43%
===============================================
Files 314 314
Lines 45844 69742 +23898
===============================================
+ Hits 33879 48456 +14577
- Misses 11965 21286 +9321
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:
|
mdaigle
approved these changes
Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-pick of #4972 (457ed73) into
release/8.0.