From 8a1d2a9fa742086b3d5947f1bb4cd490484cc41e Mon Sep 17 00:00:00 2001 From: Sergiy Kukunin Date: Sat, 14 Nov 2026 22:03:38 +0000 Subject: [PATCH] network: bound half-closed haproxy sessions on the virtual router 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 #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) --- .../java/com/cloud/network/HAProxyConfigurator.java | 9 ++++++++- .../com/cloud/network/HAProxyConfiguratorTest.java | 13 +++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/cloud/network/HAProxyConfigurator.java b/core/src/main/java/com/cloud/network/HAProxyConfigurator.java index b3f7da1c6d82..d63b20c44f21 100644 --- a/core/src/main/java/com/cloud/network/HAProxyConfigurator.java +++ b/core/src/main/java/com/cloud/network/HAProxyConfigurator.java @@ -50,7 +50,10 @@ public class HAProxyConfigurator implements LoadBalancerConfigurator { "\tuser haproxy", "\tgroup haproxy", "\tstats socket /run/haproxy/admin.sock", "\tdaemon"}; 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", + "", ""}; + + private static final long MAX_FIN_TIMEOUT = 50000; private static String[] defaultListen = {"listen vmops", "\tbind 0.0.0.0:9", "\toption transparent"}; @@ -635,9 +638,11 @@ public String[] generateConfiguration(final LoadBalancerConfigCommand lbCmd) { if (lbCmd.keepAliveEnabled) { dSection.set(7, "\tno option httpclose"); } + long finTimeout = MAX_FIN_TIMEOUT; if (lbCmd.idleTimeout > 0) { dSection.set(9, "\ttimeout client " + Long.toString(lbCmd.idleTimeout)); dSection.set(10, "\ttimeout server " + Long.toString(lbCmd.idleTimeout)); + finTimeout = Math.min(lbCmd.idleTimeout, MAX_FIN_TIMEOUT); } else if (lbCmd.idleTimeout == 0) { // .remove() is not allowed, only .set() operations are allowed as the list // is a fixed size. So lets just mark the entry as blank. @@ -648,6 +653,8 @@ public String[] generateConfiguration(final LoadBalancerConfigCommand lbCmd) { // default HAProxy timeout values from defaultsSection for predictability. logger.warn("Negative idleTimeout ({}) configured; retaining default HAProxy timeouts.", lbCmd.idleTimeout); } + dSection.set(11, "\ttimeout client-fin " + Long.toString(finTimeout)); + dSection.set(12, "\ttimeout server-fin " + Long.toString(finTimeout)); if (logger.isDebugEnabled()) { for (final String s : dSection) { diff --git a/core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java b/core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java index 073f976719b5..052cdf0741cf 100644 --- a/core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java +++ b/core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java @@ -106,13 +106,22 @@ public void testGenerateConfigurationLoadBalancerIdleTimeoutConfigCommand() { LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "4", true, 0L); String result = genConfig(hpg, cmd); - assertTrue("idleTimeout of 0 should not generate 'timeout server' in the resulting haproxy config", !result.contains("\ttimeout server")); - assertTrue("idleTimeout of 0 should not generate 'timeout client' in the resulting haproxy config", !result.contains("\ttimeout client")); + assertTrue("idleTimeout of 0 should not generate 'timeout server' in the resulting haproxy config", !result.contains("\ttimeout server ")); + assertTrue("idleTimeout of 0 should not generate 'timeout client' in the resulting haproxy config", !result.contains("\ttimeout client ")); + assertTrue("idleTimeout of 0 leaves nothing else to bound half-closed connections, so they fall back to 50000", + result.contains("\ttimeout client-fin 50000") && result.contains("\ttimeout server-fin 50000")); cmd = new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "4", true, 1234L); result = genConfig(hpg, cmd); assertTrue("idleTimeout of 1234 should result in 'timeout server 1234' in the resulting haproxy config", result.contains("\ttimeout server 1234")); assertTrue("idleTimeout of 1234 should result in 'timeout client 1234' in the resulting haproxy config", result.contains("\ttimeout client 1234")); + assertTrue("a half-closed connection must not outlive an active one, so the fin timeout tracks a smaller idleTimeout", + result.contains("\ttimeout client-fin 1234") && result.contains("\ttimeout server-fin 1234")); + + cmd = new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "4", true, 3600000L); + result = genConfig(hpg, cmd); + assertTrue("idleTimeout of 3600000 should still be capped to 50000 for half-closed connections", + result.contains("\ttimeout client-fin 50000") && result.contains("\ttimeout server-fin 50000")); } /**