Skip to content

Time out the TLS handshake at Connect Timeout - #1

Open
LukeButters wants to merge 2 commits into
mainfrom
luke/tls-handshake-timeout
Open

LukeButters wants to merge 2 commits into
mainfrom
luke/tls-handshake-timeout

Conversation

@LukeButters

Copy link
Copy Markdown
Owner

Problem

In managed SNI the TLS handshake had no timeout. SniTcpHandle.Receive sets ReceiveTimeout for the pre-login read and then resets it to infinite, and EnableSsl called AuthenticateAsClient with no cancellation token. If the network stalled after pre-login, for example from a half-open TCP connection, the physical open hung forever.

Because WaitHandleDbConnectionPool creates connections through a Semaphore(1, 1), and drains queued async opens on a single thread, one hung handshake meant:

  • no new connection could be created in that pool;
  • queued OpenAsync calls never completed, even after idle connections were returned to the pool.

We hit this in production, where an endpoint stopped dequeuing for 10 hours after a SQL failure.

Fix

  • TdsParserStateObjectManaged.EnableSsl builds a CancellationTokenSource from the connection's remaining Connect Timeout (GetTimeoutRemaining()), and passes its token to SniHandle.EnableSsl.
  • Both handshake branches now use AuthenticateAsClientAsync with that token. The non-TLS-first branch previously used the sync AuthenticateAsClient.
  • In TCP, cancellation is reported as the SNI connection-timeout error.

Tests

PendingOpenTlsHandshakeStallTest runs against a real SQL Server (MDS_TCPConnectionString, default localhost,1433) through Halibut's Octopus.TestPortForwarder. A test hook (SniTcpHandle.BeforeTlsHandshake) pauses the connection just before the handshake. TdsParserStateObjectManaged.TlsHandshakeTimeoutEnabled switches the fix on and off.

  • WhenOpeningADbConnection_AndTheTCPConnectionHangsDuringTLSHandshake_TheOpenAttemptEventuallyFails: passes with the fix, fails without it (by design).
  • WhenATcpConnectionHangsDuringTLSHandshake_ThePoolIsAbleToGrowAfterTheOpenTimesOut: passes with the fix, fails without it (by design).

Notes / not done

  • This is investigation-grade. The test hooks, the nuget.org NuGet.config mapping and the real-SQL dependency would need replacing before this goes upstream. The intended replacement is the in-repo simulated TDS server.
  • After the hung open times out, the pool's blocking period (5s, exponential backoff) still rejects opens that need a new connection.
  • Only managed SNI over TCP with Encrypt=Mandatory is exercised. Not tested: Strict/TDS 8, named pipes, native SNI (Windows/net462).
  • The INVESTIGATION: comments in WaitHandleDbConnectionPool and SniTcpHandle mark the queueing behaviour.

The managed SNI TLS handshake ran with no timeout: the socket's ReceiveTimeout is reset to infinite after
the pre-login read, and AuthenticateAsClient took no cancellation token. A handshake that stalled blocked
the pool's single creation slot, so no connection could be created and queued async opens never completed.

EnableSsl now takes a token that fires at the connection's remaining Connect Timeout, and both handshake
paths use AuthenticateAsClientAsync with it.

Tests run against a real SQL Server through Halibut's TestPortForwarder, pausing the connection just before
the TLS handshake via a test hook. The handshakeTimeoutEnabled=false cases fail by design to show the old
behaviour.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant