Skip to content

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

Open
Kukunin wants to merge 1 commit into
apache:mainfrom
Kukunin:haproxy-bound-half-closed-sessions
Open

Kukunin wants to merge 1 commit into
apache:mainfrom
Kukunin:haproxy-bound-half-closed-sessions

Conversation

@Kukunin

@Kukunin Kukunin commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

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:

timeout client-fin  min(idleTimeout, 50000)
timeout server-fin  min(idleTimeout, 50000)

with idleTimeout = 0 treated as unbounded and therefore capped.

Why

HAProxyConfigurator emits only two session timeouts, timeout client and timeout server. When the fin timeouts are absent, haproxy falls back to those for half-closed connections:

"By default it is not set, so half-closed connections will use the other timeouts (timeout.client or timeout.tunnel)."

So a zone on the 50000 default already bounds them at 50s. Two cases are not covered.

idleTimeout = 0 blanks both, leaving nothing to reap a half-closed session. haproxy keeps the fd, so the socket stays in FIN_WAIT2/CLOSE_WAIT and the kernel cannot reap it either — tcp_fin_timeout only 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 info reports only the newest worker, so the obvious metric read zero the whole time.

A zone that raised idleTimeout for a long-lived protocol holds half-closed connections just as long — the FIN_WAIT buildup the directive is documented for:

"particularly useful to avoid keeping connections in FIN_WAIT state for too long… particularly common long connections such as RDP or WebSocket."

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:

  1. Never longer than 50000 — the value hardcoded for every deployment before HAProxy Configuration: network.loadbalancer.haproxy.idle.timeout #12586 made the session timeouts configurable. No zone ends up above the bound it had historically.
  2. Never longer than idleTimeout — a half-closed connection should not outlive an active one.
idleTimeout half-closed bound today after
0 unbounded 50000
3600000 (1hr) 1hr 50000 — capped
50000 (default) 50s 50s — unchanged
1234 1234 1234 — unchanged
< 0 50000 50000 — unchanged

Everything 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.asList wraps the private static field without copying, so a set() writes through and persists for the life of the JVM.

The idleTimeout = 0 assertions 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

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

How Has This Been Tested?

HAProxyConfiguratorTest passes against main (5 tests, 0 failures), covering 0 → 50000, 1234 → 1234 and 3600000 → 50000.

Deployed to production. The zone runs idleTimeout = 0, so it takes the capped branch, and the regenerated config carries timeout client-fin 50000 / server-fin 50000 with timeout client / timeout server still 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 13 FIN_WAIT2 / 13 CLOSE_WAIT to 0/0, and long-lived connections through a :5432 rule 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

Copilot AI balanced review requested due to automatic review settings November 14, 2026 18:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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-fin and server-fin timeouts 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

@Kukunin
Kukunin force-pushed the haproxy-bound-half-closed-sessions branch from bf94e31 to 4b18529 Compare November 14, 2026 18:54
Copilot AI balanced review requested due to automatic review settings November 14, 2026 18:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
@Kukunin
Kukunin force-pushed the haproxy-bound-half-closed-sessions branch from 4b18529 to 8a1d2a9 Compare November 14, 2026 22:03
Copilot AI balanced review requested due to automatic review settings November 14, 2026 22:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Command-specific writes mutate the shared static defaults array, allowing concurrent router configurations to overwrite FIN timeout values and emit incorrect directives.

2 open issues

Review effort: Balanced

Comment on lines +656 to +657
dSection.set(11, "\ttimeout client-fin " + Long.toString(finTimeout));
dSection.set(12, "\ttimeout server-fin " + Long.toString(finTimeout));
@github-actions

Copy link
Copy Markdown

This pull request has merge conflicts. Dear author, please fix the conflicts and sync your branch with the base branch.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants