Skip to content

network: bound half-closed haproxy sessions on the virtual router - #6

Merged
Kukunin merged 1 commit into
4.22-testingfrom
haproxy-fin-timeouts
Nov 14, 2026
Merged

Kukunin merged 1 commit into
4.22-testingfrom
haproxy-fin-timeouts

Conversation

@Kukunin

@Kukunin Kukunin commented Nov 14, 2026

Copy link
Copy Markdown

Problem

network.loadbalancer.haproxy.idle.timeout=0 blanks both timeout client and timeout server (see the idleTimeout == 0 branch). That zero is deliberate where long-lived connections must not be cut — a Postgres backend behind a TCP LB rule, for example — so the timeouts can't simply be restored.

With no session timeout at all, a client that half-closes leaves its session held indefinitely. haproxy keeps the fd, so the socket sits in FIN_WAIT2/CLOSE_WAIT and the kernel can't reap it: tcp_fin_timeout only applies to orphaned sockets, and these aren't.

Enough of them reach maxconn, which is a per-process budget shared by every VIP the router serves. Past that, the kernel still completes handshakes from the backlog for sessions haproxy never services — TCP connects, TLS never starts — and every service behind that router stops answering at once while its backends remain healthy.

Observed

A production VR reached 4096/4096 with ~4,100 half-closed sessions from a single client:

4493 CLOSE-WAIT   4080 FIN-WAIT-2   79 ESTAB
haproxy worker: 8208 fds   Maxconn: 4096   CurrConns: 4096

Two unrelated applications in separate namespaces went down together, for ~25 minutes, until haproxy was restarted. Backend health checks stayed UP throughout, because the backends were fine.

Fix

timeout client-fin / timeout server-fin arm only after a FIN has been seen, so they bound the half-closed case while leaving fully open idle connections untouched. That keeps the idle.timeout=0 use case working.

Appended rather than inserted: the defaults list is fixed-size and indices 7, 9 and 10 are addressed positionally later in the method.

Testing

timeout client-fin 30s validates cleanly against the haproxy shipped in the systemvm (2.6.12):

$ haproxy -c -f /tmp/t.cfg
Configuration file has no error but will not start (no listener) => exit(2).

🤖 Generated with Claude Code

timeout client and timeout server are the only session timeouts the configurator emits,
and network.loadbalancer.haproxy.idle.timeout=0 removes both. Deployments need that zero
when long-lived connections must survive an idle period - a Postgres backend reached
through a TCP load balancer rule, for instance - so the timeouts cannot simply be put
back.

With no timeout at all a client that half-closes leaves its session held forever. haproxy
keeps the fd, so the socket stays in FIN_WAIT2 or CLOSE_WAIT and the kernel cannot reap it
under tcp_fin_timeout, which only applies to orphaned sockets. Enough of them reach
maxconn, a per-process budget shared by every VIP the router serves, and the router then
completes TCP handshakes from the backlog for connections it never services. Every service
behind that router stops answering at once while its backends stay healthy.

client-fin and server-fin arm only once a FIN has been seen, so they bound the half-closed
case and leave fully open idle connections untouched.

Appended rather than inserted: the defaults list is fixed size and indices 7, 9 and 10 are
addressed positionally further down the method.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Kukunin
Kukunin merged commit 04c3861 into 4.22-testing Nov 14, 2026
12 of 14 checks passed
@Kukunin
Kukunin deleted the haproxy-fin-timeouts branch November 14, 2026 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant