Skip to content

A failed backend dial claims the per-(cluster, source-IP) slot until session close, shared across sessions and with the TCP proxy #1521

Closed

Description

@FlorentinDUBOIS

Found while measuring whether the per-(cluster, source-IP) gate could move to the embedder side of the H2 core boundary (#1340, Q6/Q12). The measurement said no, and the reason is a behaviour that is worth stating on its own, because it is not obvious from reading either end.

A backend dial that fails still claims the (cluster, source-IP) slot, and holds it until the session closes.

The ordering, measured on main at 5355cec0

Router::connect checks the gate and then claims the slot:

  • lib/src/protocol/mux/router.rs:368 — cluster_ip_at_limit(...), reject if at limit
  • lib/src/protocol/mux/router.rs:392 — track_cluster_ip(frontend_token, cluster_id, ip)

Four fallible exits sit after that claim, all on the dial path:

router.rs exit
494 Err(MaxSessionsMemory) — the reused token is missing from the backend map
506 Err(MaxSessionsMemory) — start_stream refused
557 ? on backend_from_request — backend selection or connect(2) failed
600 Err(MaxBuffers) — pool exhaustion while building the client

The release is not paired with any of them. untrack_all_cluster_ip runs only from session close — lib/src/http.rs:1842 and lib/src/https.rs:2719. So between a failed dial and the session closing, the slot stays claimed.

Why the blast radius is wider than one session

SessionManager::connections_per_cluster_ip (lib/src/server.rs:263) is a HashMap<String, HashMap<IpAddr, usize>> keyed on (cluster, ip) across every session on the worker. The per-token map beside it, cluster_ip_tracks (:270), is only the idempotency set that keeps H2 streams to the same (cluster, ip) sharing one slot.

Its reader is called from two places, and the second is a different protocol:

  • lib/src/protocol/mux/router.rs:368, described in lib/src/server.rs:252 as the per-request hot path
  • lib/src/tcp.rs:1673, the TCP proxy's own gate — whose surrounding comment already says "Router does the same gate for HTTP/HTTPS sessions; here it runs for raw TCP"

So a slot claimed by a failed H2 dial counts against the budget for other H2 sessions and for raw TCP frontends from the same source IP.

lib/src/tcp.rs has the same lifetime shape — cluster_ip_tracked: bool at :139, set at :1691, released only in close.

The question

Is this intended, and if so is it written down anywhere?

There is a defensible reading where it is: claiming before the dial is fail-closed, and refusing to release on failure means a client cannot spend a worker's connect attempts for free by inducing dial failures in a loop. On that reading the current code is right and the only thing missing is a sentence saying so, because the next person to touch this ordering will see an unpaired claim and read it as an oversight — I did.

There is also a reading where it costs something real. Backends go down, pools exhaust, and connect(2) fails for reasons that have nothing to do with the client. Under a partial backend outage every failed dial consumes a slot that nothing returns until the session closes, and the budget is shared per source IP — so legitimate traffic behind a NAT or a CDN egress, which is the case max_connections_per_ip exists to bound, would be rate-limited by failures the clients did not cause and cannot avoid. The effect is largest exactly when the cluster is already unhealthy.

Which reading is correct depends on how long a session survives a failed dial, and that is the number I do not have. If the session closes promptly on BackendConnectionError, the window is short and this is a non-issue that only needs a comment. If it can persist — H2 in particular, where one connection carries many streams and a failed dial for one stream need not end the connection — the window is the connection's lifetime and the accounting is worth revisiting.

Asked for

  1. Measure the window: after each of the four exits above, does the session close, and how soon? For H2 specifically, does a failed dial on one stream end the connection or leave it serving other streams with the slot still claimed?
  2. Decide and write it down. If claiming on failure is deliberate anti-abuse, say so at track_cluster_ip and in the max_connections_per_ip documentation, so the unpaired claim reads as a decision. If it is not, pair the release with the failure exits.

Do not change this as part of a refactor. Whichever way it goes it is a live rate-limiting change with a security-adjacent shape — releasing on failure loosens a connection limit under precisely the conditions an attacker can induce — so it wants its own change, its own reasoning, and a test that is seen red.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions