fix(providers): detect dead/half-open backend connections via TCP keepalive - #1272
Closed
Aaronontheweb wants to merge 2 commits into
Closed
fix(providers): detect dead/half-open backend connections via TCP keepalive#1272Aaronontheweb wants to merge 2 commits into
Aaronontheweb wants to merge 2 commits into
Conversation
…palive Switch LLM HttpClient from bare HttpClientHandler to SocketsHttpHandler with aggressive TCP keepalive (idle=10s, interval=5s, probes=3) so that a dead or restarted backend is detected in ~25s instead of hanging for the full 20-minute watchdog budget. The resulting IOException lets the existing FailoverChatClient pre-first-chunk path fail over to the configured fallback provider automatically. Also adds ConnectTimeout (10s), PooledConnectionIdleTimeout (60s), and HTTP/2 PING settings for cloud providers that negotiate h2 over TLS. Closes netclaw-dev#1253
Collaborator
Author
|
Closing this because it addresses dead/half-open TCP detection, but the production failure mode is an app-layer no-progress wedge behind Caddy: vLLM still shows a connected client while producing no tokens. TCP keepalive mainly proves Netclaw->Caddy liveness and does not reliably detect the stuck upstream stream. The fix for #1253 should use the existing no-progress signal to trigger pre-first-substantive-output failover, while preserving the no-failover-after-output invariant. |
Aaronontheweb
added a commit
to Aaronontheweb/netclaw
that referenced
this pull request
Aug 13, 2026
A dead or half-open LLM connection can send a few tokens, then go silent. No error appears. The connection stays open. The old per-call watchdog catches this stall only after minutes. A caller then has little time left in its own retry budget. Add StreamStallGuardChatClient. It arms an inactivity timer after the first update in a stream. It cancels the read and throws a TimeoutException when no update arrives within the timeout. The timer does not run before the first update. A self-hosted backend can stay silent for minutes during a cold prefill. The old per-call watchdog still covers that case. Add RetryPolicy.StreamInactivityTimeout. The default value is 45 seconds. Set it to zero to turn off the new guard. The guard sits below RetryingChatClient in the pipeline. The RetryPolicy.ShouldRetry rule already treats a TimeoutException as retryable. No new retry path exists. This change revives the idea in closed PR netclaw-dev#1272. That PR added TCP keepalive to SocketsHttpHandler. The author closed it: keepalive only proves liveness between Netclaw and its reverse proxy. It does not detect a stuck upstream stream behind Caddy. This fix uses an application-layer signal instead.
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.
Summary
HttpClientfrom bareHttpClientHandlertoSocketsHttpHandlerwith aggressive TCP keepalive (idle=10s,interval=5s,probes=3) so a dead or restarted backend is detected in ~25s instead of hanging for the full 20-minute watchdog budgetIOExceptionlets the existingFailoverChatClientpre-first-chunk path fail over to the configured fallback provider automaticallyConnectTimeout(10s),PooledConnectionIdleTimeout(60s), and HTTP/2 PING settings for cloud providers that negotiate h2 over TLSContext
When a self-hosted LLM backend (vLLM, llama.cpp) restarts mid-request, the TCP connection goes half-open — no RST, no data, no exception. The existing
FailoverChatClientonly fails over on exceptions, so the turn hangs for up to 20 minutes (untilProcessingWatchdogcancels) with no failover to the healthy fallback. TCP keepalive probes detect peer death in ~25s, surfacing anIOExceptionthat the existing failover logic already handles.Key insight: TCP keepalive probes peer liveness independently of data flow. A slow-but-alive prefill answers probes (no false kill); a dead/restarted backend doesn't (fast detection → exception → failover).
Verified: vLLM (uvicorn) and llama.cpp (cpp-httplib) are HTTP/1.1 only — HTTP/2 PING frames won't help for self-hosted. TCP keepalive is the primary mechanism.
Closes #1253
Test plan
dotnet build src/Netclaw.Providers/compiles cleanly