Repository navigation
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the change that bounds half-closed HAProxy sessions while preserving idle-timeout behavior.
No open issues 🎉
What changed in this PR
This PR bounds half-closed HAProxy sessions on CloudStack virtual routers without restoring idle timeouts for fully open connections, helping prevent connection exhaustion and stalled worker shutdowns.
Changes:
- Adds unconditional 30-second
client-finandserver-fintimeouts while preserving existing positional defaults. - Updates tests to distinguish session timeouts from FIN timeouts and verify FIN timeouts remain enabled when
idleTimeout=0.
| File | Description |
|---|---|
| core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java | Refines idle-timeout assertions and checks that FIN timeouts remain present. |
| core/src/main/java/com/cloud/network/HAProxyConfigurator.java | Appends FIN timeouts to bound half-closed sessions. |
Review effort: Balanced
bf94e31 to
4b18529
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The fixed 30-second FIN timeout can prematurely terminate valid TCP half-close exchanges regardless of the idle timeout, so it needs independent configurability to preserve longer response waits.
1 open issue
Review effort: Balanced
| private static String[] defaultsSection = {"defaults", "\tlog global", "\tmode tcp", "\toption dontlognull", "\tretries 3", "\toption redispatch", | ||
| "\toption forwardfor", "\toption httpclose", "\ttimeout connect 5000", "\ttimeout client 50000", "\ttimeout server 50000"}; | ||
| "\toption forwardfor", "\toption httpclose", "\ttimeout connect 5000", "\ttimeout client 50000", "\ttimeout server 50000", | ||
| "\ttimeout client-fin 30s", "\ttimeout server-fin 30s"}; |
timeout client and timeout server are the only session timeouts the configurator emits. When the fin timeouts are absent haproxy falls back to them for half-closed connections, so a zone on the 50000 default already bounds those at 50s. Two cases are not covered. network.loadbalancer.haproxy.idle.timeout=0 blanks both, leaving nothing to reap a half-closed session. 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 sessions it never services: connections establish, no data is returned, and every service behind that router stops answering while its backends stay healthy. Seamless reloads compound it, since an old worker is retired only once its sessions close, so each reload leaves another worker resident. A zone that raised the idle timeout for a long lived protocol holds half-closed connections for just as long, which is the FIN_WAIT buildup the directive documents for RDP and WebSocket. Derive the value from the one already configured: min(idleTimeout, 50000), with idleTimeout=0 treated as unbounded and therefore capped. A half-closed connection is then never held longer than 50000, the value hardcoded for every deployment before apache#12586 made the session timeouts configurable, and never outlives an active one, since a larger timeout would leave a dying connection more budget than a working one. Zones at or below 50s are unchanged. The idleTimeout=0 assertions tested for the absence of "\ttimeout server" and "\ttimeout client", which are prefixes rather than whole directives. Match one character further, up to the space separating directive from value: the session timeouts render space padded while the fin timeouts have a hyphen immediately after the name. The entries are appended rather than inserted because the defaults list is fixed size and indices 7, 9 and 10 are addressed positionally further down the method. Indices 11 and 12 are assigned on every path: Arrays.asList wraps the static field without copying, so a set() writes through and persists for the life of the JVM. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
4b18529 to
8a1d2a9
Compare
| dSection.set(11, "\ttimeout client-fin " + Long.toString(finTimeout)); | ||
| dSection.set(12, "\ttimeout server-fin " + Long.toString(finTimeout)); |
|
This pull request has merge conflicts. Dear author, please fix the conflicts and sync your branch with the base branch. |


Description
Derives the half-closed connection timeout from the one already configured, rather than leaving it unbounded in one case and over-long in another:
with
idleTimeout = 0treated as unbounded and therefore capped.Why
HAProxyConfiguratoremits only two session timeouts,timeout clientandtimeout server. When the fin timeouts are absent, haproxy falls back to those for half-closed connections:So a zone on the 50000 default already bounds them at 50s. Two cases are not covered.
idleTimeout = 0blanks both, leaving nothing to reap a half-closed session. haproxy keeps the fd, so the socket stays inFIN_WAIT2/CLOSE_WAITand the kernel cannot reap it either —tcp_fin_timeoutonly applies to orphaned sockets.Enough of them reach
maxconn, which is a per-process budget shared by every VIP the router serves. Past that, the kernel still completes TCP handshakes from the backlog for sessions haproxy never services: connections establish, no data is ever returned, and every service behind that router stops answering while its backends stay healthy. Seamless reloads compound it — an old worker is retired only once its sessions close, so each reload leaves another resident.We hit this in production. One client accumulated ~4,100 half-closed sessions; the router sat at 4096/4096 and two unrelated applications behind different VIPs went down together for ~25 minutes, with backend health checks green throughout. The same router had six haproxy workers, the oldest a week old, one holding 254 fds.
show inforeports only the newest worker, so the obvious metric read zero the whole time.A zone that raised
idleTimeoutfor a long-lived protocol holds half-closed connections just as long — the FIN_WAIT buildup the directive is documented for:Why derive rather than hardcode
An earlier revision of this PR set a flat 30s. That was wrong: it tightened every deployment, including ones that had deliberately widened the bound, with no way to restore it. Thanks to @copilot-pull-request-reviewer for catching it.
min(idleTimeout, 50000)follows two rules instead:idleTimeout— a half-closed connection should not outlive an active one.idleTimeout03600000(1hr)50000(default)1234< 0Everything at or below 50s is untouched, so most deployments see no change. This also means no new global setting: the value tracks the one operators already set, and the only zones affected are those currently unbounded or held above the historical maximum.
Implementation notes
The entries are appended rather than inserted, because the defaults list is fixed-size and indices 7, 9 and 10 are addressed positionally further down the method. Indices 11 and 12 are assigned on every path —
Arrays.asListwraps theprivate staticfield without copying, so aset()writes through and persists for the life of the JVM.The
idleTimeout = 0assertions tested for the absence of"\ttimeout server"/"\ttimeout client", which are prefixes rather than whole directives. They now match one character further, up to the space separating directive from value: the session timeouts render space-padded, while the fin timeouts have a hyphen immediately after the name.Types of changes
Feature/Enhancement Scale or Bug Severity
Bug Severity
How Has This Been Tested?
HAProxyConfiguratorTestpasses againstmain(5 tests, 0 failures), covering0 → 50000,1234 → 1234and3600000 → 50000.Deployed to production. The zone runs
idleTimeout = 0, so it takes the capped branch, and the regenerated config carriestimeout client-fin 50000/server-fin 50000withtimeout client/timeout serverstill absent — the zero is still honoured. Half-closed sockets now turn over instead of accumulating: on a busy router, sampling the same socket identities 75 seconds apart showed 0 of 332 still present, against unbounded growth to ~4,100 before. A quieter router went from a steady 13FIN_WAIT2/ 13CLOSE_WAITto 0/0, and long-lived connections through a:5432rule were unaffected.One operational note for existing zones: a router needs its haproxy restarted, not just reloaded. Workers started before the change keep running their original config and never drain.
🤖 Generated with Claude Code