Repository navigation
Conversation
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
When Azure SQL redirects the login to another node (routing
ENVCHANGEin the LOGINACK, the default for clients inside Azure),tdsopens a new connection to the redirect target but never closes the connection to the gateway.Some time later the gateway closes that abandoned connection. With TLS, the
Tds.Tlsprocess that owns the socket then stops with:tcp_closed:Tds.Tlsis started withGenServer.start_link/3from the connection process, which does not trap exits, so the abnormal exit silently takes down the connection that is happily using the new socket. The pool reconnects, gets redirected again, leaks the gateway socket again, and the cycle repeats.In my production logs this happens every 98–120 seconds, around the clock: about 34 times an hour, each time dropping a healthy pooled connection (and any query running on it).
Fix
Close the gateway socket in the redirect clause of
Tds.Protocol.message(:login, msg_loginack(redirect: …), s)before connecting to the redirect target. A socket closed locally does not deliver:tcp_closedto its owner, soTds.Tlsno longer stops and the connection survives.Tests
Added
test/login_redirect_gateway_socket_test.exs: a local listener plays the gateway, the login is redirected to a closed port, and the test asserts the gateway connection is closed on both ends. It needs no SQL Server; it fails onmasterand passes with the fix. It uses a plain TCP socket; the TLS path goes through the samemod.close/1.Notes
Tds.Tlsprocess of the gateway socket stays idle until the connection process exits, the same as afterdisconnect/2today.{:error, error, state}); whichever is merged second needs a trivial rebase.