Skip to content

[8.0.0 Cherry-pick] Fix SqlTransaction.Dispose racing the close of a broken connection - #5053

Merged
mdaigle merged 1 commit into
release/8.0from
dev/automation/pr-4972-to-8.0.0
Nov 12, 2026
Merged

mdaigle merged 1 commit into
release/8.0from
dev/automation/pr-4972-to-8.0.0

Conversation

@github-actions

Copy link
Copy Markdown

Cherry-pick of #4972 (457ed73) into release/8.0.

…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.
@github-actions
github-actions Bot requested a review from a team as a code owner November 12, 2026 17:59
@github-actions github-actions Bot added this to the 8.0.0 milestone Nov 12, 2026
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 12, 2026
@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 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

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

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.47%. Comparing base (b99cd83) to head (e654f5b).

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     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 67.36% <100.00%> (?)
sqlclient-pr 69.29% <100.00%> (?)

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.

@mdaigle
mdaigle merged commit 463a45c into release/8.0 Nov 12, 2026
208 checks passed
@mdaigle
mdaigle deleted the dev/automation/pr-4972-to-8.0.0 branch November 12, 2026 20:34
@github-project-automation github-project-automation Bot moved this from To triage to Done in SqlClient Board Nov 12, 2026
@mdaigle mdaigle mentioned this pull request Nov 16, 2026
3 of 4 tasks
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.

2 participants