Repository navigation
Don't wait in SniTcpHandle.CheckConnection - #5071
RoccoPanunzio wants to merge 3 commits into
Conversation
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: There may be pipelines that require an authorized user to comment /azp run to run. |
|
@dotnet-policy-service agree |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. |
|
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 == 0That 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 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 |
mdaigle
left a comment
There was a problem hiding this comment.
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.
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
left a comment
There was a problem hiding this comment.
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
|
Merged main and resolved the conflict: #5051 had added a |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. |
Description
SniTcpHandle.CheckConnectionpolled the socket with a 100 µs timeout. On a healthy, idle connection the pollwaits 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, andSelectReadwith theAvailable == 0check 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:
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): afterthe peer closes, the check reports
SNI_ERROR. With the poll removed from the condition it fails, so it coversthe detection this change must keep.
New benchmark
ConnectionPoolIdleCheckout(performance tests): a one-connection pool is left idle for 10 msoutside the measurement, past
CheckConnectionWindow, then a single open is timed, sync and async. Locally,with
UseManagedSniOnWindows, legacy pool:Poll(100, …)Poll(0, …)With
UseConnectionPoolV2andPoll(0, …): 0.15 ms sync, 0.13 ms async.