Skip to content

Don't wait in SniTcpHandle.CheckConnection - #5071

Merged
paulmedynski merged 4 commits into
dotnet:mainfrom
RoccoPanunzio:fix/check-connection-no-wait
Nov 17, 2026
Merged

paulmedynski merged 4 commits into
dotnet:mainfrom
RoccoPanunzio:fix/check-connection-no-wait

Conversation

@RoccoPanunzio

@RoccoPanunzio RoccoPanunzio commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

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
Contributor 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
Contributor 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 previously approved these changes Nov 16, 2026
@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
cheenamalhotra
cheenamalhotra previously approved these changes Nov 17, 2026

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

Change looks good, just asking for an expanded explanation.

@RoccoPanunzio
RoccoPanunzio dismissed stale reviews from cheenamalhotra and mdaigle via 7f9273c November 17, 2026 18:01
@paulmedynski
paulmedynski enabled auto-merge (squash) November 17, 2026 18:49
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@paulmedynski paulmedynski moved this from Waiting for customer to In review in SqlClient Board Nov 17, 2026
@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 17, 2026 •

Copy link
Copy Markdown
Contributor

Confirmed that the new idle pooled-checkout benchmarks run successfully on Windows with managed SNI enabled: perf pipeline run 192630.

Both benchmarks completed 50 measured iterations with one checkout per iteration and a 10 ms idle delay outside the measurement:

Benchmark Mean Median
OpenIdlePooledConnection 122.66 µs 109.75 µs
OpenIdlePooledConnectionAsync 90.26 µs 86.90 µs

These sub-millisecond results are consistent with removing the approximately 15 ms poll wait previously observed on Windows. The driver and benchmark sources were unchanged from the PR revision tested, e768e817327559b99bf03e94d5dd7ed1bf8a5b93; validation-only pipeline fixes and benchmark selection were applied on a separate branch.

This was a targeted, candidate-only run, not a before/after comparison. BenchmarkDotNet reported short-iteration warnings, so the timings should be treated as confirmation of execution without the timer-tick-scale delay, not precise throughput estimates. Full reports are available in the run's perf-results artifact.

@paulmedynski
paulmedynski merged commit c58251e into dotnet:main Nov 17, 2026
42 of 43 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in SqlClient Board Nov 17, 2026
mdaigle pushed a commit that referenced this pull request Nov 17, 2026
* Don't wait in SniTcpHandle.CheckConnection

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.

* Measure an idle pooled checkout as a benchmark instead of a unit test

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.

* Explain the zero poll timeout as a best-effort health snapshot

---------

Co-authored-by: Rocco Panunzio <panunzio.rocco@gmail.com>
Co-authored-by: Rocco Panunzio <222559735+RoccoPanunzio@users.noreply.github.com>
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: Done

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

4 participants