fix: don't let one unreachable server stall the whole listener - #7
Merged
Conversation
AcceptConnection constructed the TDSConnection inline and only re-armed BeginAcceptTcpClient afterwards. Constructing a connection dials the far server with a blocking Connect, so a server that swallows SYNs held the single in-flight accept for the OS connect timeout - over two minutes on Linux - and every other client sat in the backlog for exactly as long. Observed against a legacy SQL Server that is blackholed nightly: one connection served per 135 seconds, for every client, all night. Three changes: - Re-arm the listener before setting up the connection, so a slow or dead far server no longer serializes accepts. - Bound the dial with a 10 second timeout rather than the OS SYN retry sequence, so a thread is not parked for minutes per attempt. - Roll back construction when the dial fails. The Stopping subscription is what keeps a TDSConnection alive, and it is taken before the dial, so a failed attempt lingered - still holding the accepted client socket - until the service stopped. That is the origin of the hundreds of NullReferenceExceptions from Dispose at shutdown, one per stranded attempt; _insideStream is null-guarded there as well. Keep-alive (#6) does not cover this case: it needs a connection that was established, and here none ever is. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019FMY262wiaqabAsPuQ9b8G
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.
The problem
TDSListener.AcceptConnectioncreated theTDSConnectioninline and only re-armedBeginAcceptTcpClientafter it returned:The constructor dials the far server with a blocking
Connect. When that server swallows SYNsrather than refusing them, the dial runs the full OS retry sequence — ~135 s on Linux — and for
that entire time there is no accept in flight, so every client waits in the backlog.
Seen in production against a legacy SQL Server that is blackholed for part of each night: the
proxy accepted one connection per 135 s, for all clients, until the far side came back.
Why keep-alive (#6) does not cover it
Keep-alive detects a peer that vanishes from an established connection. Here no connection is
ever established — the failure is in the dial itself.
Changes
Re-arm the listener before setting up the connection. A slow or dead far server no longer
serializes accepts. Failures during accept itself return without constructing anything.
Bound the dial at 10 s (
ConnectWithTimeout) instead of leaving it to the OS SYN retries,so a thread is not parked for minutes per attempt.
Roll back construction when the dial fails (
AbandonBeforeConnected).service.Stopping += service_Stoppingis taken before the dial and is what keeps aTDSConnectionalive, so afailed attempt lingered — still holding the accepted client socket, still counted in
ActiveConnectionCount— until the service stopped. This is the origin of the hundreds ofat shutdown: one per stranded attempt,
_insideStreamnever assigned. It is null-guarded nowas well, matching the
_insideSSL?.Close()next to it.Verification
dotnet build -c Releaseagainstdotnet/sdk:6.0-bullseye-slim(the image this is deployedfrom): clean, 0 warnings.
The behavioural check is at the far end: with an unreachable server, the log must show a failure
per attempt with
Accepted connectionentries continuing at the clients' own pace, instead of oneaccept per connect-timeout.
🤖 Generated with Claude Code