Skip to content

fix(providers): detect dead/half-open backend connections via TCP keepalive - #1272

Closed
Aaronontheweb wants to merge 2 commits into
netclaw-dev:devfrom
Aaronontheweb:claude-wt-network-timeout-halfopen
Closed

fix(providers): detect dead/half-open backend connections via TCP keepalive#1272
Aaronontheweb wants to merge 2 commits into
netclaw-dev:devfrom
Aaronontheweb:claude-wt-network-timeout-halfopen

Conversation

@Aaronontheweb

Copy link
Copy Markdown
Collaborator

Summary

  • Switch LLM HttpClient from bare HttpClientHandler to SocketsHttpHandler with 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 budget
  • The resulting IOException lets the existing FailoverChatClient pre-first-chunk path fail over to the configured fallback provider automatically
  • Adds ConnectTimeout (10s), PooledConnectionIdleTimeout (60s), and HTTP/2 PING settings for cloud providers that negotiate h2 over TLS

Context

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 FailoverChatClient only fails over on exceptions, so the turn hangs for up to 20 minutes (until ProcessingWatchdog cancels) with no failover to the healthy fallback. TCP keepalive probes detect peer death in ~25s, surfacing an IOException that 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
  • Existing tests pass (TCP keepalive doesn't affect loopback test connections)
  • Operational validation: restart a vLLM backend mid-streaming-request and confirm failover triggers in ~25s instead of hanging 20 minutes

…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
@Aaronontheweb Aaronontheweb added the reliability Retries, resilience, graceful degradation label Jun 26, 2026
@Aaronontheweb

Copy link
Copy Markdown
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

reliability Retries, resilience, graceful degradation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LLM providers: detect dead/half-open backend connections fast so failover actually triggers

1 participant