Skip to content

Fix AppContext switch race in unit tests - #4495

Open
paulmedynski wants to merge 1 commit into
mainfrom
dev/paul/multi-subnet-failover-race
Open

Fix AppContext switch race in unit tests#4495
paulmedynski wants to merge 1 commit into
mainfrom
dev/paul/multi-subnet-failover-race

Conversation

@paulmedynski

@paulmedynski paulmedynski commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes a unit-test race where SqlConnectionOptionsTest temporarily enables the process-wide cached EnableMultiSubnetFailoverByDefault switch while simulated-server failover tests run concurrently.

  • Adds non-parallel xUnit collections for tests that mutate cached AppContext switches and for simulated-server tests that include switch-mutating scenarios.
  • Assigns all LocalAppContextSwitchesHelper users to the AppContext isolation collection.
  • Explicitly sets MultiSubnetFailover = false in TransientFault_ShouldConnectToPrimary so the scenario does not inherit the process-wide default.
  • No public API or product behavior changes.

Issues

Investigated from CI-SqlClient-Package run 21207.

Testing

  • Full regular .NET 8 unit suite: 984 passed, 9 skipped, 0 failed.
  • Original concurrent reproducer: passed 12 consecutive runs after failing 12/12 before the fix.
  • Focused final run: 11 passed, 0 failed.

Checklist

  • Tests added or updated
  • Public API changes documented (not applicable; no public API changes)
  • Verified against customer repro (not applicable; verified against CI failure)
  • Ensure no breaking changes introduced

Copilot AI review requested due to automatic review settings July 31, 2026 11:09
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Jul 31, 2026
@paulmedynski paulmedynski added this to the 7.1.0-preview3 milestone Jul 31, 2026
@paulmedynski paulmedynski moved this from To triage to In progress in SqlClient Board Jul 31, 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

This pull request addresses an xUnit parallelism race in the UnitTests project where tests temporarily mutating cached, process-wide AppContext switch values could be observed by concurrently running simulated-server failover tests.

Changes:

  • Introduces two non-parallel xUnit collections to isolate (1) AppContext switch mutation and (2) simulated-server tests that may trigger switch-mutation scenarios.
  • Assigns LocalAppContextSwitchesHelper users to the AppContext isolation collection to prevent cross-test interference.
  • Makes TransientFault_ShouldConnectToPrimary explicitly opt out of inheriting a process-wide default by setting MultiSubnetFailover = false.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/tests/UnitTests/TestIsolationCollections.cs Adds new xUnit collection definitions to disable parallelization for AppContext-switch tests and simulated-server tests.
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionFailoverTests.cs Moves the simulated-server suite onto the new non-parallel collection and pins MultiSubnetFailover=false for a specific scenario.
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlConnectionOptionsTest.cs Assigns test class to AppContext switch isolation collection.
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlConnectionInternalTimeoutTests.cs Assigns test class to AppContext switch isolation collection.
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/LocalAppContextSwitchesTest.cs Assigns test class to AppContext switch isolation collection.
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/WaitHandleDbConnectionPoolIdleTimeoutTest.cs Assigns test class to AppContext switch isolation collection.
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/WaitHandleDbConnectionPoolBudgetTest.cs Assigns test class to AppContext switch isolation collection.
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/ChannelDbConnectionPoolWarmupTest.cs Assigns test class to AppContext switch isolation collection.
src/Microsoft.Data.SqlClient/tests/UnitTests/ConnectionPool/ChannelDbConnectionPoolTest.cs Assigns test class to AppContext switch isolation collection.

@paulmedynski
paulmedynski marked this pull request as ready for review July 31, 2026 15:24
@paulmedynski
paulmedynski requested a review from a team as a code owner July 31, 2026 15:24
@paulmedynski
paulmedynski enabled auto-merge (squash) July 31, 2026 15:24
@paulmedynski paulmedynski moved this from In progress to In review in SqlClient Board Jul 31, 2026
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.81%. Comparing base (25dba6b) to head (12638f3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4495      +/-   ##
==========================================
- Coverage   64.67%   62.81%   -1.87%     
==========================================
  Files         288      283       -5     
  Lines       44046    66979   +22933     
==========================================
+ Hits        28488    42072   +13584     
- Misses      15558    24907    +9349     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 62.81% <ø> (?)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

4 participants