Repository navigation
Time out the TLS handshake at Connect Timeout - #1
Open
LukeButters wants to merge 2 commits into
Open
LukeButters wants to merge 2 commits into
LukeButters wants to merge 2 commits into
Conversation
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.
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.
Problem
In managed SNI the TLS handshake had no timeout.
SniTcpHandle.ReceivesetsReceiveTimeoutfor the pre-login read and then resets it to infinite, andEnableSslcalledAuthenticateAsClientwith no cancellation token. If the network stalled after pre-login, for example from a half-open TCP connection, the physical open hung forever.Because
WaitHandleDbConnectionPoolcreates connections through aSemaphore(1, 1), and drains queued async opens on a single thread, one hung handshake meant:OpenAsynccalls 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.EnableSslbuilds aCancellationTokenSourcefrom the connection's remaining Connect Timeout (GetTimeoutRemaining()), and passes its token toSniHandle.EnableSsl.AuthenticateAsClientAsyncwith that token. The non-TLS-first branch previously used the syncAuthenticateAsClient.Tests
PendingOpenTlsHandshakeStallTestruns against a real SQL Server (MDS_TCPConnectionString, defaultlocalhost,1433) through Halibut'sOctopus.TestPortForwarder. A test hook (SniTcpHandle.BeforeTlsHandshake) pauses the connection just before the handshake.TdsParserStateObjectManaged.TlsHandshakeTimeoutEnabledswitches 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
NuGet.configmapping and the real-SQL dependency would need replacing before this goes upstream. The intended replacement is the in-repo simulated TDS server.Encrypt=Mandatoryis exercised. Not tested:Strict/TDS 8, named pipes, native SNI (Windows/net462).INVESTIGATION:comments inWaitHandleDbConnectionPoolandSniTcpHandlemark the queueing behaviour.