Skip to content

Don't wait in SniTcpHandle.CheckConnection - #5071

Open
RoccoPanunzio wants to merge 3 commits into
dotnet:mainfrom
RoccoPanunzio:fix/check-connection-no-wait
Open

RoccoPanunzio wants to merge 3 commits into
dotnet:mainfrom
RoccoPanunzio:fix/check-connection-no-wait

Conversation

@RoccoPanunzio

@RoccoPanunzio RoccoPanunzio commented Nov 14, 2026 •

Copy link
Copy Markdown

Description

SniTcpHandle.CheckConnection polled the socket with a 100 µs timeout. On a healthy, idle connection the poll
waits the whole timeout, and Windows rounds it up to the system timer tick (15.6 ms by default), so every
open from the pool over TCP with the managed network layer cost about 15 ms once the connection had been idle
for more than CheckConnectionWindow (5 ms).

The poll now uses a zero timeout. Detection of a broken connection does not change: a connection closed by
the peer is already readable, so Poll(0, SelectMode.SelectRead) reports it, and SelectRead with the
Available == 0 check stays as dotnet/corefx#31631 left it for Linux.

Measured on Windows 11 against a local SQL Server 2025 over TCP, 200 opens from the pool, each after 10 ms idle:

Build Median p90
Native layer 0.19 ms 0.36 ms
Managed layer, before 15.2 ms 15.7 ms
Managed layer, after 0.18 ms 0.34 ms

No API change. Only the managed TCP handle changes, so .NET Framework and the native layer are unaffected.

Suggested release note: Fixed a ~15 ms delay on every pooled connection open over TCP with the managed
network layer on Windows.

Issues

Fixes #5070

Testing

  • CheckConnection_PeerClosed_ReturnsError (unit test, real loopback TCP connection without SQL Server): after
    the peer closes, the check reports SNI_ERROR. With the poll removed from the condition it fails, so it covers
    the detection this change must keep.

  • New benchmark ConnectionPoolIdleCheckout (performance tests): a one-connection pool is left idle for 10 ms
    outside the measurement, past CheckConnectionWindow, then a single open is timed, sync and async. Locally,
    with UseManagedSniOnWindows, legacy pool:

    Build Sync Async
    Poll(100, …) 13.4 ms 13.8 ms
    Poll(0, …) 0.19 ms 0.16 ms

    With UseConnectionPoolV2 and Poll(0, …): 0.15 ms sync, 0.13 ms async.

The liveness check polled the socket with a 100 µs timeout. On a healthy,
idle connection the poll waits the whole timeout, and Windows rounds it up
to the system timer tick (15.6 ms by default), so every open from the pool
over TCP with the managed network layer cost about 15 ms once the
connection had been idle longer than CheckConnectionWindow.

A connection closed by the peer is already readable, so a zero timeout
detects it just the same; SelectRead and the Available check stay as they
were for Linux.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@RoccoPanunzio

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@mdaigle

mdaigle commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

@mdaigle

mdaigle commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

I checked the history behind the 100 µs timeout. In dotnet/corefx#31631, the managed SNI check changed from:

_socket.Poll(0, SelectMode.SelectError)

to:

_socket.Poll(100, SelectMode.SelectRead) && _socket.Available == 0

That fixed detection of closed connections on Linux. The important distinction is read readiness versus exceptional conditions: a graceful peer close makes the socket readable once EOF is observable, but does not necessarily trigger SelectError. Checking Available == 0 distinguishes that condition from buffered application data. The original PR explains this change, but does not explain why a positive timeout was needed.

Using zero preserves that read-side check. There is a small behavioral tradeoff: a positive timeout can catch a FIN/RST arriving during the wait, while zero checks only the state observable immediately. Connection liveness checks are always best effort. The connection could always break right after we check it, even with a wait.

I reproduced the Windows timing effect separately: an idle socket's Poll(100, SelectRead) took about 15 ms median, versus 0.017 ms for Poll(0, SelectRead). .NET 10's HTTP connection pool also uses zero-timeout read polling on its synchronous plain-TCP reuse path.

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

Looks good, thanks for finding this! My only request is that we move the timing-based test to the perf tests so that we can accurately measure the performance of the flow. We watch the perf tests for regressions.

@github-project-automation github-project-automation Bot moved this from To triage to Waiting for customer in SqlClient Board Nov 16, 2026
The liveness probe runs only on a connection that has been idle for more
than 5 ms, and a successful probe restarts that window, so the churn
runner probes at most one checkout in every 5 ms and averages its cost
away. ConnectionPoolIdleCheckoutRunner leaves a one-connection pool idle
for 10 ms outside the measurement, then times a single open, sync and
async, so every measured checkout pays for the probe. With managed SNI on
Windows it reads about 13.5 ms per checkout with Poll(100) and about
0.2 ms with Poll(0).

The timing assertion leaves the unit tests, which keep the peer-closed case.
mdaigle
mdaigle previously approved these changes Nov 16, 2026

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

There are a couple merge conflicts for you to address.

…n-no-wait

# Conflicts:
#	src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandleTests.cs
@RoccoPanunzio

Copy link
Copy Markdown
Author

Merged main and resolved the conflict: #5051 had added a SniTcpHandleTests.cs too, so both tests now live in the one file. Thanks for the review!

@mdaigle

mdaigle commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

@mdaigle mdaigle added this to the 8.1.0-preview1 milestone Nov 16, 2026
@mdaigle mdaigle added Hotfix 8.0.0 PRs that must be backported to release/8.0 Area\Managed SNI Issues that are targeted to the Managed SNI codebase. labels Nov 16, 2026

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

Area\Managed SNI Issues that are targeted to the Managed SNI codebase. Hotfix 8.0.0 PRs that must be backported to release/8.0

Projects

Status: Waiting for customer

Development

Successfully merging this pull request may close these issues.

Managed SNI on Windows: every pooled open over TCP waits ~15 ms in SniTcpHandle.CheckConnection

2 participants