Repository navigation
Conversation
On managed SNI the TLS handshake ran with no bound: Receive leaves the socket read timeout infinite and AuthenticateAsClient got no token, so a server that stalls after PRELOGIN blocked Open until the OS dropped the socket. EnableSsl now gets a token from the remaining Connect Timeout that closes the transport stream, and reports wait timeout 258 so MultiSubnetFailover keeps retrying within the full timeout.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Transport-disposal cancellation requires cross-platform runtime validation, especially for TLS interruption on Windows named pipes, which could not be performed in this review.
No open issues 🎉
What changed in this PR
This PR bounds managed SNI TLS handshakes by the remaining login timeout, addressing the stalls described in #5045 and #4834 without changing pool waiter behavior.
Changes:
- Propagates a timeout token through managed SNI and closes TCP or named-pipe transports when it expires.
- Reports timeout error 258 to preserve login retries and tolerates already-closed pipes during cleanup.
- Adds stalled-handshake regression tests for synchronous and asynchronous opens, MultiSubnetFailover, strict encryption, and transport-level cancellation.
| File | Description |
|---|---|
src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/TlsHandshakeTimeoutTests.cs |
Adds connection-level timeout tests using a stalling TCP server. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandleTests.cs |
Tests TCP handshake cancellation and timeout error identity. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniReceiveTests.cs |
Updates the test handle’s EnableSsl signature. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniPacketTests.cs |
Updates the test handle to accept a cancellation token. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniNpHandleTests.cs |
Tests named-pipe handshake cancellation and timeout reporting. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Parser/TdsParserStateObjectManaged.netcore.cs |
Creates the token from the remaining login timeout. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs |
Aborts timed-out TCP handshakes and reports error 258. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniNpHandle.netcore.cs |
Aborts timed-out pipe handshakes and handles closed-pipe cleanup. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniMarsHandle.netcore.cs |
Forwards the cancellation token to the MARS connection. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniMarsConnection.netcore.cs |
Passes the token to the underlying transport handle. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniHandle.netcore.cs |
Extends and documents the internal TLS cancellation contract. |
Review effort: Balanced
mdaigle
requested changes
Nov 12, 2026
Contributor
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. |
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
TLS timeout enforcement depends on transport-disposal interruption and cleanup across platforms, especially Windows named pipes, which could not be validated without current-head runtime test results.
No open issues 🎉
Review effort: Balanced
mdaigle
approved these changes
Nov 13, 2026
Contributor
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. |
3 of 4 tasks
mdaigle
pushed a commit
that referenced
this pull request
Nov 16, 2026
* Fix managed SNI TLS handshake timeout On managed SNI the TLS handshake ran with no bound: Receive leaves the socket read timeout infinite and AuthenticateAsClient got no token, so a server that stalls after PRELOGIN blocked Open until the OS dropped the socket. EnableSsl now gets a token from the remaining Connect Timeout that closes the transport stream, and reports wait timeout 258 so MultiSubnetFailover keeps retrying within the full timeout. * Stall TLS handshake via TdsServer Co-authored-by: Tomáš Filip <[email protected]>
This was referenced Nov 18, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On managed SNI (Linux, macOS, and Windows with
UseManagedNetworkingOnWindows) the TLS handshake of a new connection doesnt respect Connect Timeout.SniTcpHandle.Receiveresets the socket read timeout to infinite after PRELOGIN andEnableSslcallsAuthenticateAsClientwith nothing else bounding it, so when the network path stalls right after PRELOGIN,Opensits there until the OS gives up on the socket (like 15 minutes on Linux). With pooling, every other open waits behind it.TdsParserStateObjectManaged.EnableSslnow creates a token from the remaining login timeout and passes it toSniHandle.EnableSsl. The TCP and named pipe handles keep the syncAuthenticateAsClientand register the token to dispose the transport stream, same as the sync path ofConnectHelperinSocketsHttpHandler. I didnt go with sync over asyncAuthenticateAsClientAsync, because disposing the stream unblocks the sync call just fine and it keeps the existing code path.The timeout is reported as SNI error 11 with native error 258, so
SqlException.Numberis 258 like native SNI's wait timeout. This matters for MultiSubnetFailover, which managed networking turns on for TCP by default:ThrowExceptionAndWarningonly retries the login on -2 or 258, so with any other number the open failed on the first attempt's slice of the timeout (around 160 ms with Connect Timeout 2).The provider in the message is TCP Provider, not SSL Provider like native, because thats how the managed handles already report the receive timeout and the other errors from
EnableSsl.The pool part of #5045 (waiters queued behind one stuck creation) is not touched here. With the handshake bounded, the stuck creation now fails at Connect Timeout instead of holding the pool, and
UseConnectionPoolV2should cover the rest of that part.Suggested release note: Fixed the TLS handshake on managed SNI ignoring Connect Timeout, which could block
Open/OpenAsyncand the connection pool for minutes when the server or network stalled during the handshake.Issues
Fixes #5045
Fixes #4834
Thanks to LukeButters for the analysis in #4834.
Testing
TlsHandshakeTimeoutTestsrunsOpen,OpenAsync(with and without MultiSubnetFailover) and strict encryption against a local listener that answers PRELOGIN and never answers the ClientHello. They fail on main after 30 s and pass in about 2 s with the fix.SniTcpHandleTestsandSniNpHandleTestscoverEnableSslreturning the timeout error once its token is cancelled, for TLS inside TDS and TLS first. The pipe one also caughtSniNpHandle.Disposethrowing after the timeout, sinceSslStreamflushes the pipe it leaves open, so that dispose now ignores the already closed pipe.I also ran the repro from #5045 against SQL Server 2022 in Docker with the branch build, on Linux and on Windows with managed networking:
Openfails after 4.8 s with Connect Timeout 5 instead of hanging, and the pooled opens go through.