GeezerVison is showing github.com — Reset pooled session isolation level before reuse (fixes #96) by priyankatiwari08 · Pull Request #4330 · dotnet/SqlClient
Conversation
Fixes #96
Summary
SQL Server does not reset a session's transaction isolation level through sp_reset_connection, so a pooled connection can leak an elevated level to its next user.
This change adds the default-off AppContext switch Switch.Microsoft.Data.SqlClient.EnableTransactionIsolationLevelReset. When enabled, SqlClient tracks non-default transaction isolation levels and resets them to READ COMMITTED on pool checkout when no existing enlistment or active delegated transaction root remains.
The reset adds one round trip only when a changed connection is reused, using the caller's remaining Open timeout budget rather than the user command timeout. Known Azure Synapse dedicated SQL pool endpoints are excluded because they reject this statement; serverless endpoints remain eligible, including Private Link forms.
Tests
Covers SqlTransaction and TransactionScope, synchronous and asynchronous opens and queries, default-off compatibility behavior, same-scope reuse, detached delegated roots, reset timeout budgets through both pools, and Synapse endpoint classification.
- Tests added or updated
- Public API changes documented — no public API change
- Verified against customer repro
- No breaking changes introduced
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
rgroenewoudt and Timovzl reacted with thumbs up emoji
dziegler-oc reacted with rocket emoji
All reactions
-
👍
2 reactions
-
🚀
1 reaction
There was a problem hiding this comment.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
SqlTransaction and TransactionScope leave the underlying SQL Server session with the elevated isolation level after Commit/Rollback. sp_reset_connection does not reset it, so the next user of the pooled physical connection silently inherits it.
This change tracks when a non-default isolation level was set via the TM Begin path and, on pool return, issues a 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED' batch right before sp_reset_connection (piggybacked on the same TDS header, no extra round trip). On failure the connection is doomed instead of returned to the pool.
A new AppContext switch 'Switch.Microsoft.Data.SqlClient.UseLegacyIsolationLevelBehavior' preserves previous behavior for callers that depend on it (default: false).
Adds IsolationLevelLeakTest under ManualTests covering the two repro scenarios plus a negative test for the kill switch.
There was a problem hiding this comment.
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 4 out of 4 changed files in this pull request and generated 3 comments.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
- IsolationLevelLeakTest: switch to LocalAppContextSwitchesHelper so the cached LocalAppContextSwitches value is forced, not just set via AppContext.SetSwitch (production code reads the cache after first use, which made the negative test order-dependent and caused the CI failure).
- Helper restores the previous value on dispose, so global state does not leak to other tests.
- TransactionScope test: set Enlist=true explicitly on the pooled connection string so the test does not depend on the user's local DataTestUtility.TCPConnectionString defaults.
- LocalAppContextSwitchesHelper: expose UseLegacyIsolationLevelBehavior get/set + capture/restore wired through the standard reflection helpers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you look into this failure for LegacySwitch_PreservesOldLeakBehavior?
Tests are mostly failing with this one. You can put the PR into draft till this is addressed.
Is this happening only in CI or do you observe it locally under VS as well?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
The leak only manifests on on-prem SQL Server: Azure SQL DB resets the session isolation level inside sp_reset_connection, so the legacy switch becomes a no-op there and the assertion (expects 'Serializable' to leak) fails on Azure CI legs.
There was a problem hiding this comment.
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 5 out of 5 changed files in this pull request and generated 2 comments.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 27, 2026
Reviewers
mdaigle
mdaigle approved these changes
benrr101
Awaiting requested review from benrr101
+1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
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.
|