Skip to content

Fix managed SNI TLS handshake ignoring Connect Timeout - #5051

Merged
mdaigle merged 2 commits into
dotnet:mainfrom
tomasfil:dev/tomasfil/managed-sni-tls-handshake-timeout
Nov 16, 2026
Merged

mdaigle merged 2 commits into
dotnet:mainfrom
tomasfil:dev/tomasfil/managed-sni-tls-handshake-timeout

Conversation

@tomasfil

Copy link
Copy Markdown
Contributor

Description

On managed SNI (Linux, macOS, and Windows with UseManagedNetworkingOnWindows) the TLS handshake of a new connection doesnt respect Connect Timeout. SniTcpHandle.Receive resets the socket read timeout to infinite after PRELOGIN and EnableSsl calls AuthenticateAsClient with nothing else bounding it, so when the network path stalls right after PRELOGIN, Open sits there until the OS gives up on the socket (like 15 minutes on Linux). With pooling, every other open waits behind it.

TdsParserStateObjectManaged.EnableSsl now creates a token from the remaining login timeout and passes it to SniHandle.EnableSsl. The TCP and named pipe handles keep the sync AuthenticateAsClient and register the token to dispose the transport stream, same as the sync path of ConnectHelper in SocketsHttpHandler. I didnt go with sync over async AuthenticateAsClientAsync, 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.Number is 258 like native SNI's wait timeout. This matters for MultiSubnetFailover, which managed networking turns on for TCP by default: ThrowExceptionAndWarning only 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 UseConnectionPoolV2 should cover the rest of that part.

Suggested release note: Fixed the TLS handshake on managed SNI ignoring Connect Timeout, which could block Open/OpenAsync and 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

TlsHandshakeTimeoutTests runs Open, 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. SniTcpHandleTests and SniNpHandleTests cover EnableSsl returning the timeout error once its token is cancelled, for TLS inside TDS and TLS first. The pipe one also caught SniNpHandle.Dispose throwing after the timeout, since SslStream flushes 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: Open fails after 4.8 s with Connect Timeout 5 instead of hanging, and the pooled opens go through.

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.
@tomasfil
tomasfil requested a review from a team as a code owner November 12, 2026 16:54
Copilot AI balanced review requested due to automatic review settings November 12, 2026 16:54
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 12, 2026
@azure-pipelines

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

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

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

Thanks for addressing this @tomasfil.
I'd like to see the SimulatedServerTest refactored slightly, but otherwise looks good to me!

@github-project-automation github-project-automation Bot moved this from To triage to Waiting for customer in SqlClient Board Nov 12, 2026
@mdaigle mdaigle self-assigned this Nov 12, 2026
@mdaigle

mdaigle commented Nov 12, 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 12, 2026
@mdaigle mdaigle added the Area\Managed SNI Issues that are targeted to the Managed SNI codebase. label Nov 12, 2026
Copilot AI balanced review requested due to automatic review settings November 12, 2026 23:08

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

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

mdaigle commented Nov 13, 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 the Hotfix 8.0.0 PRs that must be backported to release/8.0 label Nov 16, 2026
@mdaigle
mdaigle merged commit 44be992 into dotnet:main Nov 16, 2026
42 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting for customer to Done in SqlClient Board Nov 16, 2026
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]>
@mdaigle mdaigle mentioned this pull request Nov 16, 2026
3 of 4 tasks
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

3 participants