Skip to content

fix(agent): bound idle connections and per-connection buffers - #136

Merged
bpalermo merged 1 commit into
mainfrom
fix/connection-leak-idle-timeout
Jun 11, 2026
Merged

bpalermo merged 1 commit into
mainfrom
fix/connection-leak-idle-timeout

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

The leak (measured live on talos-main, post-#135)

#135 silenced the CDS churn, but proxy RSS kept growing ~80–100 Mi/min on nodes with loadgen traffic. Envoy /memory on the worker-02 proxy showed 3.2 GiB of live heap (allocated ≈ heap_size, so real objects, not fragmentation) with only 5,216 stats. /stats found the objects:

cluster.svc-1.upstream_cx_active: 13900
cluster.svc-2.upstream_cx_active: 13882
cluster.svc-3.upstream_cx_active: 13873        ← ~41k live upstream mTLS conns
http.loadgen-….downstream_cx_total: 20099
http.loadgen-….downstream_cx_active: 1         ← downstream conns close fine
http.inbound_svc-….downstream_cx_active: ~7400 ← peers' leaked upstreams, mirrored

Root cause

Service clusters use connection_pool_per_downstream_connection: true (required: a source pod's mTLS connection must never be reused for another source). Every non-keepalive downstream connection therefore creates a dedicated pool + fresh upstream mTLS H2 connection. When the downstream closes, the orphaned pool can never be selected again — and the only mechanism that reclaims its upstream connection is the common HTTP idle timeout, which Envoy defaults to 1 hour. Under burst traffic that plateaus at rate×3600 leaked connections per proxy (≈75 KiB each), far past 4-core node capacity. The resulting saturation is what cascaded into kubelet flaps, agent informer-desync liveness kills (13×) and proxy admin-watchdog kills (14×) — previously attributed entirely to the #135 churn.

Changes

  1. Upstream 30s idle timeout (config.UpstreamIdleTimeout) on the HTTP/1+HTTP/2 protocol options used by service clusters — orphaned pools reclaimed in seconds; a live downstream just re-establishes an idle upstream on its next request. Active HC connections probe every 5s and never idle out.
  2. Downstream 5m backstop on the shared per-pod HCM builder (inbound mesh conns + app-side outbound conns), kept well above the upstream timeout so the client side always disconnects first.
  3. per_connection_buffer_limit_bytes: 32 KiB (Envoy edge-hardening guidance) on every generated listener and cluster — the 1 MiB/connection/direction default turns connection-count incidents into memory incidents; flow control handles larger payloads, request/response sizes are not capped.

Testing

  • TestUpstreamIdleTimeoutSet, TestHCMDownstreamIdleTimeout (incl. downstream > upstream ordering), TestPerConnectionBufferLimits (all six generated resource kinds)
  • Full suite bazel test //...: 38/38 pass

Post-deploy validation: upstream_cx_active plateaus at ~rate×30 (~1.5k at current load) instead of climbing; proxy RSS flat over hours.

🤖 Generated with Claude Code

Live measurement on talos-main found the node proxy leaking upstream mTLS
connections: ~41k active upstream connections (13.9k per service cluster)
and 3.2 GiB of live heap within minutes of startup, while the outbound
listeners showed only 1-2 active downstream connections.

Service clusters use connection_pool_per_downstream_connection for
per-source mTLS, so every non-keepalive downstream connection creates a
dedicated pool with a fresh upstream mTLS H2 connection. When the
downstream connection closes, the orphaned pool can never be selected
again — and the only mechanism that reclaims its upstream connection is
the common HTTP idle timeout, which Envoy defaults to 1 HOUR. Under
non-keepalive traffic that plateaus at rate×3600 leaked connections per
proxy, far past node capacity; the resulting saturation cascaded into
kubelet flaps, agent informer-desync liveness kills and proxy
admin-watchdog kills (the steady-state degradation observed alongside,
and previously attributed to, the CDS churn fixed in #135).

Changes:
- Upstream: 30s idle timeout on the HTTP/1 and HTTP/2 protocol options
  used by service clusters. Orphaned pools are reclaimed in seconds; live
  downstream connections just re-establish an idle upstream on the next
  request.
- Downstream backstop: 5m idle timeout on the shared per-pod HCM builder
  (inbound mesh conns + app-side outbound conns), well above the upstream
  timeout so the client side always disconnects first.
- Per-connection buffer limits: 32 KiB (Envoy edge-hardening guidance) on
  every generated listener and cluster, replacing the 1 MiB/connection/
  direction default that turns connection-count incidents into memory
  incidents.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@bpalermo
bpalermo merged commit e6b4af5 into main Jun 11, 2026
10 checks passed
@bpalermo
bpalermo deleted the fix/connection-leak-idle-timeout branch June 11, 2026 12:10
bpalermo added a commit that referenced this pull request Jun 11, 2026
* feat(proxy): overload manager with health-gateway bypass

Third layer of the memory-incident defense, after leak prevention
(#135/#136) and containment (#137's 1Gi limit): graceful degradation
before the limit's OOM kill. A fixed_heap monitor (default 768Mi = 75% of
the 1Gi container limit; heap excludes stats/code//dev/shm) drives a
shedding ladder:

  0.80 shrink_heap
  0.90 disable_http_keepalive          (sheds idle downstream conns; their
                                        per-downstream upstream pools then
                                        idle out via the 30s timeout)
  0.85-0.95 reduce_timeouts            (scales HTTP downstream idle toward
                                        2s - the dynamic version of #136)
  0.95 stop_accepting_requests         (503s new streams, which client
                                        routes retry on a DIFFERENT
                                        endpoint - the overloaded node
                                        sheds load to other replicas)
  0.98 stop_accepting_connections

The agent-generated health-gateway listener sets bypass_overload_manager:
delegated liveness must report APP truth, not proxy state. Without the
bypass an overloaded proxy would 503 the agent's liveness probes and flap
every local pod's registry health cluster-wide on transient overload;
shedding belongs to the data-plane listeners (503+retry, client active
HC), not the liveness channel. The gateway is a node-local UDS with a
handful of agent connections, so the exemption frees no meaningful
memory.

Inert in normal operation: steady-state heap sits far below the first
rung. Rendered bootstrap validated with `envoy --mode validate` against
the pinned image digest (configuration OK). Gated by
proxy.overload.enabled (default true).

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* feat(proxy): drive the overload ladder from cgroup_memory, not fixed_heap

The fixed_heap monitor measures tcmalloc heap, but the OOM killer enforces
the cgroup limit on the whole container working set — the supervisor
process, kernel socket buffers (charged to the cgroup, and correlated with
exactly the overload scenario) and thread stacks are all invisible to a
heap monitor, which forced a ~75% heap budget of guesswork and a values
knob that had to be bumped in lockstep with the memory limit.

The cgroup_memory monitor reads memory.current / memory.max directly, so
the thresholds sit against the real kill line and the knob disappears
(optional maxMemoryBytes only to engage the ladder below the cgroup
limit). Verified end-to-end against the pinned Envoy image: listed in the
binary's compiled-in resource monitors, --mode validate OK, and a live
run under docker --memory=256m reports pressure tracking the limit with
zero failed_updates.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
bpalermo added a commit that referenced this pull request Jun 11, 2026
Steady-state proxy RSS measured 67-148Mi post-#136 (e2e 2026-06-11), so
512Mi keeps ~3.5x headroom while halving the node memory reservation
(request == limit, Guaranteed) and engaging the overload ladder earlier
on a real leak. Heap budget scales with it: 384Mi = 75% of the limit
(top rung 0.98x384 ~= 376Mi + non-heap extras stays under the cgroup
kill line).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
bpalermo added a commit that referenced this pull request Jun 11, 2026
… 512Mi (#139)

* fix(proxy): overload manager back to fixed_heap — cgroup_memory is fatal in privileged pods

Deploying #138 (rev 64) took the entire data plane down for ~13 minutes:
every proxy aborted at bootstrap with "No supported cgroup memory
implementation found" and crash-looped until the overload manager was
disabled via values (rev 65).

Root cause: the cgroup_memory monitor reads
/sys/fs/cgroup/memory.{current,max}. That works under docker — private
cgroup namespace, the container sees its own leaf cgroup, which is where
the pre-merge validation passed — but the aether-proxy DaemonSet is a
PRIVILEGED pod and gets the HOST cgroup namespace: /sys/fs/cgroup is the
host root cgroup, which has no memory.max. Envoy treats the failed probe
as a fatal bootstrap error.

The outage was cluster-wide rather than canaried because the supervisors
watch the bootstrap config (fsnotify) and hot-restarted every node into
the poisoned ConfigMap at once, bypassing the DaemonSet rollout safety
entirely. A follow-up hardening (supervisor pre-validates new bootstrap
config with `envoy --mode validate` on the node before hot-restarting,
keeping the old config serving on failure) is planned separately — node-
local validation WOULD have caught this, unlike the docker-side check.

fixed_heap is tcmalloc-internal (no cgroup access), so its validation is
environment-independent. The 768Mi = 75%-of-limit budget and its
rationale are restored, and the ConfigMap now documents exactly why
cgroup_memory must not be reintroduced while the proxy is privileged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* feat(proxy): default proxy memory limit 512Mi

Steady-state proxy RSS measured 67-148Mi post-#136 (e2e 2026-06-11), so
512Mi keeps ~3.5x headroom while halving the node memory reservation
(request == limit, Guaranteed) and engaging the overload ladder earlier
on a real leak. Heap budget scales with it: 384Mi = 75% of the limit
(top rung 0.98x384 ~= 376Mi + non-heap extras stays under the cgroup
kill line).

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
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