Skip to content

Reset pooled session isolation level before reuse (fixes #96) - #4330

Merged
priyankatiwari08 merged 29 commits into
dotnet:mainfrom
priyankatiwari08:feature/sqltransaction-isolation-leak
Oct 7, 2026
Merged

priyankatiwari08 merged 29 commits into
dotnet:mainfrom
priyankatiwari08:feature/sqltransaction-isolation-leak

Conversation

@priyankatiwari08

@priyankatiwari08 priyankatiwari08 commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

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

Copilot AI lite review requested due to automatic review settings June 1, 2026 13:59
@priyankatiwari08
priyankatiwari08 requested a review from a team as a code owner June 1, 2026 13:59
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Jun 1, 2026

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 encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.
@priyankatiwari08
priyankatiwari08 force-pushed the feature/sqltransaction-isolation-leak branch from 4e8ed47 to 9f67fb1 Compare June 1, 2026 14:19
@priyankatiwari08
priyankatiwari08 requested a review from Copilot June 1, 2026 14:20
@priyankatiwari08 priyankatiwari08 added this to the 7.0.2 milestone Jun 1, 2026

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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

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

Can you look into this test failure?
Seems related to the changes

@github-project-automation github-project-automation Bot moved this from To triage to Waiting for customer in SqlClient Board Jun 1, 2026
@apoorvdeshmukh apoorvdeshmukh self-assigned this Jun 1, 2026
- 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.
@priyankatiwari08 priyankatiwari08 moved this from Waiting for customer to In progress in SqlClient Board Jun 3, 2026

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

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?

@github-project-automation github-project-automation Bot moved this from In progress to Waiting for customer in SqlClient Board Jun 3, 2026
@apoorvdeshmukh apoorvdeshmukh added the Author attention needed PRs that require author to respond or make updates to PR. label Jun 3, 2026
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.
Copilot AI review requested due to automatic review settings June 3, 2026 11:45
@priyankatiwari08
priyankatiwari08 marked this pull request as draft June 3, 2026 11:47

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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

@mdaigle mdaigle modified the milestones: 7.0.2, 7.1.0-preview2 Jun 9, 2026
This was referenced Nov 28, 2026
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.

SqlTransaction and TransactionScope leak isolation level

8 participants