Skip to content

fix(mux): release the reservation of a dial abandoned after connect - #1717

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/mux-backend-reservation-leak
Sep 30, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/mux-backend-reservation-leak

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1713

What

Backend selection reserves a connection (active_connections += 1) before Mux::dial_backend connects (#1684/#1709). Before this change, only a failed connect(2) released that reservation (BackendChange::DialFailed). Three exits after a successful connect dropped the connection and kept the reservation:

  • (a) Connection::new_h2_client returns None because the buffer pool is exhausted;
  • (b) Connection::start_stream refuses the stream;
  • (c) the register_socket rollback.

Connection has no Drop, so each hit left the backend one connection too high for the rest of the worker's life. That skews least-loaded, power-of-two and PeakEWMA selection, and it keeps a removed backend from ever draining to Closed.

Fix

  • Every step after the connect moves unchanged into the new Mux::attach_dialed.
  • Mux::dial_backend pushes one BackendChange::ConnectionClosed for whichever exit returns Err. This single release point also covers any exit added later.
  • The release is ConnectionClosed (dec_connections only), not DialFailed. These failures are local to the proxy, so recording them in the backend's failures and backoff would steer traffic away from a healthy backend.

Protocol/security impact: none on the wire. Counters stay correct on local-failure paths (CWE-400 hygiene).

Tests

lib/src/protocol/mux/router.rs: the dial_one harness is now dial_with(address, DialFault). It calls the real Mux::dial_backend against a listening loopback backend with each fault staged:

Each test asserts that the dial failed at its exit and left no router entry. After the ledger drains, active_connections == 0, failures == 0 and the retry policy is still OKAY. All three were seen red, both on unmodified main and with the release disabled (left: 1, right: 0), and green with the fix.

Commands run

  • cargo +nightly fmt --all --check → 0
  • cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings → 0
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked → 0
  • cargo test -p sozu-lib -p sozu-command-lib --locked → 0 (sozu-lib: 1423 passed)
  • RUSTFLAGS='--cfg tokio_unstable' cargo test -p sozu-sim --locked → 0
  • cargo test -p sozu-e2e --locked -- e2e_session_router_connect_failure_no_leak mux_tests protocol_pair_matrix → 0 (67 passed)
  • cd fuzz && cargo +nightly fuzz build → 0
  • check_doc_citations.py with --self-test, --root ., --root . --base origin/main and --audit --root . → 0 for all four. The audit's two advisories are in proxy_protocol/LIFECYCLE.md, which this change does not touch.

Docs

  • lib/src/protocol/mux/LIFECYCLE.md (invariant 14): replaced the "three exits … still do" paragraph and added the new tests to the "Pinned by" list.
  • Doc comments on BackendChange::ConnectionClosed, Mux::dial_backend / Mux::attach_dialed and BackendSelector.
  • CHANGELOG.md: Unreleased / Fixed.

Selection reserves a connection on the chosen backend before
Mux::dial_backend connects, and only a failed connect(2) released it.
Three exits past a successful connect dropped the connection and kept
the reservation: Connection::new_h2_client returning None, a refused
Connection::start_stream, and the register_socket rollback. Each left
active_connections one too high for the worker's lifetime, skewing
least-loaded, power-of-two and PeakEWMA selection and keeping a
removed backend from draining to Closed.

Move every step past the connect into Mux::attach_dialed, and have
dial_backend push one BackendChange::ConnectionClosed for whichever of
its exits returns Err: a single release point a new exit cannot bypass.
It records no failure and arms no backoff, since the failure is local.

Closes #1713

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/mux-backend-reservation-leak branch from d0bb233 to 8e61f98 Compare September 30, 2026 12:01
@FlorentinDUBOIS
FlorentinDUBOIS merged commit ac2789f into main Sep 30, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/mux-backend-reservation-leak branch September 30, 2026 13:11
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.

mux: three Mux::dial_backend exits after connect leak the backend connection reservation

1 participant