Skip to content

Fix Windows netonly connection pool identity and replenishment - #4814

Draft
mdaigle wants to merge 3 commits into
dotnet:mainfrom
mdaigle:mdaigle-dev/automation/fix-netonly-pool-identity
Draft

mdaigle wants to merge 3 commits into
dotnet:mainfrom
mdaigle:mdaigle-dev/automation/fix-netonly-pool-identity

Conversation

@mdaigle

@mdaigle mdaigle commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Description

Windows network-only logons retain the process account's local SID while using different outbound credentials. Pooling by SID alone can therefore reuse connections authenticated under another logon session or cache its login failures, including during background replenishment.

Include the token's AuthenticationId in Windows pool identity equality, hashing, and identity-cache lookup for both native and managed networking. The existing wait-handle pool replenishment guard now distinguishes logon sessions; the channel pool gains the equivalent guard before background connection creation. Token-query failures surface as Win32 errors.

Duplicated tokens from the same logon session still share a pool. Separate logon sessions for the same account now use separate pools. Background replenishment skips mismatched identities, so Min Pool Size is not guaranteed across impersonation boundaries; opens under the owning identity can create connections on demand. There are no public API changes. Documentation and an impersonation sample describe the behavior and tradeoffs.

Issues

Fixes #4717

Testing

Added Windows unit coverage for distinct network-only identities, pool selection, mismatched replenishment in both directions, sync/async checkout, native/managed networking, and invalid token handles.

  • Pool unit suites passed: 428 tests on net8.0, 428 on net10.0, and 421 on net462.
  • Driver built for net9.0; the sample compiled.
  • Added real-server tests for invalid-credential pool isolation and login-failure blocking, plus alternate-account login reversion and replenishment.
  • Against the local SQL Server over named pipes, all 12 invalid-credential cases passed on net8.0 and net462 with the fix. Temporarily reverting the pool fix made all 12 fail on net8.0; restoring it made them pass again.
  • Alternate-account test groups compiled but were skipped: a second Windows account and a TCP-enabled SQL Server were not available. These successful alternate-login scenarios remain to be validated.

Guidelines

  • Contributing

  • Code of Conduct

  • Best Practices

  • Coding Style

  • Review Process

  • Tests added or updated

  • Public API changes documented (no public API changes)

  • Verified against customer repro (related local real-server regression reproduced; exact customer setup not run)

  • Ensure no breaking changes introduced (intentional pool partitioning and replenishment behavior changes documented above)

Include the token logon session in Windows pool identities for both networking modes and prevent channel pool warmup under a mismatched identity. Add regression coverage for both pools, sync and async checkout, and real-server invalid-credential isolation, with documentation and an impersonation sample.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 18:06
@mdaigle mdaigle added this to the 8.0.0-preview1 milestone Oct 9, 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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

5 open findings
What changed in this PR

Fixes Windows netonly impersonation pooling so connection pools (and background replenishment) are keyed by logon session (AuthenticationId) rather than only by local SID, preventing cross-session reuse and poisoned pools.

Changes:

  • Add AuthenticationId to DbConnectionPoolIdentity equality/hash/caching and use Win32 token queries on Windows.
  • Add a logon-session guard to channel pool warmup to skip background creation under mismatched identities.
  • Add unit + manual regression coverage and documentation/sample for impersonation + pooling behavior.
File Description
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​ConnectionPool/​DbConnectionPoolIdentityTest.cs Adds Windows unit tests validating identity distinction, pool selection, replenishment behavior, and token-query error surfacing.
src/​Microsoft.Data.SqlClient/​tests/​ManualTests/​SQL/​IntegratedAuthenticationTest/​PoolIdentityImpersonationTest.cs Adds real-server manual regression tests for invalid netonly credentials and replenishment/login reversion scenarios.
src/​Microsoft.Data.SqlClient/​tests/​Common/​WindowsImpersonationHelper.cs Adds helper to create NEW_CREDENTIALS (netonly) tokens for tests without storing credentials.
src/​Microsoft.Data.SqlClient/​src/​Microsoft/​Data/​SqlClient/​ConnectionPool/​DbConnectionPoolIdentity.cs Extends identity to include AuthenticationId and switches Windows identity acquisition to token-statistics based approach.
src/​Microsoft.Data.SqlClient/​src/​Microsoft/​Data/​SqlClient/​ConnectionPool/​ChannelDbConnectionPool.cs Adds identity check to prevent warmup under the wrong logon session.
src/​Microsoft.Data.SqlClient/​src/​Interop/​Windows/​Kernel32/​Kernel32.cs Adds P/Invoke for SetLastError used to disambiguate IsTokenRestricted failures.
src/​Microsoft.Data.SqlClient/​src/​Interop/​Windows/​Advapi32/​Advapi32.cs Adds token-statistics and restricted-token queries with Win32 error propagation.
doc/​samples/​ConnectionPool_Impersonation.cs Adds sample demonstrating how to open a pooled integrated-security connection within impersonation scope.
doc/​connection-pooling-impersonation.md Documents pooling identity behavior under impersonation and replenishment tradeoffs; links to tests/sample.

🧠 Review effort: Lite


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Microsoft.Data.SqlClient/tests/Common/WindowsImpersonationHelper.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Interop/Windows/Advapi32/Advapi32.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/tests/Common/WindowsImpersonationHelper.cs Outdated
Guard failed netonly token creation, name the native token/logon constants, include all identity fields in the hash, and query the warmup identity once per pass.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 18:31

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.

CI agents accept placeholder network-only credentials over loopback, so a successful open cannot prove pool sharing there. Verify the precondition with an unpooled open and skip when it does not hold.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 22:54

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.

🔵 Needs a closer look

Windows token interop and both pool implementations are affected, while alternate-account success and replenishment scenarios remain unvalidated.

0 open findings

🧠 Review effort: Balanced

@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 29.72973% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.73%. Comparing base (6f6a0d2) to head (ecb7215).
⚠️ Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
...SqlClient/src/Interop/Windows/Advapi32/Advapi32.cs 0.00% 16 Missing ⚠️
...lClient/ConnectionPool/DbConnectionPoolIdentity.cs 41.66% 7 Missing ⚠️
...qlClient/ConnectionPool/ChannelDbConnectionPool.cs 66.66% 3 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (6f6a0d2) and HEAD (ecb7215). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (6f6a0d2) HEAD (ecb7215)
CI-SqlClient 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4814      +/-   ##
==========================================
- Coverage   72.18%   64.73%   -7.45%     
==========================================
  Files         292      287       -5     
  Lines       45341    68627   +23286     
==========================================
+ Hits        32728    44425   +11697     
- Misses      12613    24202   +11589     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.73% <29.72%> (?)

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.

This branch has not been deployed

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

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

Connection pooling/Windows Impersonation issue under replenishment

2 participants