Skip to content

fix(tcp): charge refused connects to the backend the TCP session dialed - #1661

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/tcp-backend-accounting
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/tcp-backend-accounting

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1658

CAUSE

TcpSession::connect_to_backend (lib/src/tcp.rs) receives the selected Rc<RefCell<Backend>> from BackendMap::backend_from_cluster_id, copies its backend_id and drops it, so TcpSession::backend stays None → TcpSession::fail_backend_connection (HUP on a connecting socket in TcpSession::ready_inner, i.e. a non-blocking connect refused after EINPROGRESS), TcpSession::set_back_connected and TcpSession::remove_backend all skip their if let Some(backend) = self.backend body → SYMPTOM: a refusing TCP backend never has failures bumped nor its retry_policy armed, stays in round-robin selection, is never marked down, backend.connections.error / backend.down / backend.up / backend.available are never emitted for TCP, and Backend::active_connections (incremented by Backend::try_connect) is never decremented.

The mux keeps its handle in its BackendRegistry and charges the same failure in Mux::ready.

Fix

One line in TcpSession::connect_to_backend: self.backend = Some(Rc::clone(&backend));. The existing accounting then runs as written.

Per-backend byte counters and connections_per_backend do not change value: they are keyed by metrics.backend_id, which was already set. What changes: backend.connections.error (and on down/up transitions backend.down, backend.up, backend.available, BackendDown/BackendUp) are now emitted for TCP backends; Backend::active_connections is released on close; the PeakEWMA connection time is fed; and, from reading impl Drop for Backend, a removed TCP backend's RemovedBackendHasNoConnections now waits for its last session (not covered by a test).

Reproduction, red then green

test_tcp_refused_backend_is_accounted_and_avoided (e2e/src/tests/tcp_tests.rs): two backends, the first on a closed port, sixteen sequential sessions, all served in both cases.

The avoidance bound comes from the retry policy, not from timing. ExponentialBackoffPolicy::fail arms at least one second of back-off per recorded failure, and a backend in back-off fails Backend::can_open (no fail-open while the live backend is a candidate), so over the measured duration of the sessions the refused backend can be dialed at most 1 + floor(elapsed) times. Without the accounting, round-robin over the constant pair makes every session but possibly the first dial the refused backend, at least SESSIONS - 1 = 15 times. The test requires errors <= 1 + floor(elapsed) and that bound to be strictly below 15.

  • Rebased tree with the fix line removed: backend.connections.error = None, sessions took 21.5ms, back-off allows at most 1, round-robin without it makes at least 15, test result: FAILED. 0 passed; 1 failed. With RUST_LOG=debug: the refused backend dialed 16 times for 16 sessions.
  • With the fix: Some(1) in each iteration, test result: ok. 1 passed; 10 runs out of 10 green (7 idle, 3 with 40 busy yes loops on 20 cores, load average 37-48, sessions took 12-200 ms, count always 1).

Validation (all exit 0, on the tree rebased onto origin/main 34d5e71, after #1657)

  • cargo +nightly fmt --all -- --check
  • cargo build --locked
  • cargo build --all-features --locked with CC=clang CXX=clang++ ASM=clang AR=llvm-ar RANLIB=llvm-ranlib (aws-lc-fips-sys fails with gcc on this host; its stale CMake cache was cleared with cargo clean -p aws-lc-fips-sys)
  • cargo clippy --all-targets --locked -- -D warnings
  • cargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warnings
  • cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8 (e2e: 491 passed; lib: 1334 passed)
  • e2e suites by name: tcp_tests 10 passed, tcp_sni_tests 19 passed, proxy_protocol_local_tests 2 passed, protocol_pair_matrix 24 passed
  • python3 .github/scripts/check_doc_citations.py --self-test, --root ., --root . --base origin/main, --audit --root .
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked

Signed-off-by: Florentin Dubois [email protected]

TcpSession::connect_to_backend dropped the Backend handle that
BackendMap::backend_from_cluster_id returned, so TcpSession::backend
stayed None for the whole session and every step reading it was a no-op
on the raw TCP proxy:

- TcpSession::fail_backend_connection, which handles a non-blocking
  connect refused after EINPROGRESS (a HUP on the connecting socket),
  neither bumped Backend::failures nor armed Backend::retry_policy nor
  counted backend.connections.error, so a refusing backend stayed in
  selection and was never marked down;
- TcpSession::set_back_connected(Connected) never reset the retry
  policy nor fed the PeakEWMA connection time;
- TcpSession::remove_backend never released the connection that
  Backend::try_connect counted, so Backend::active_connections only grew.

The session now keeps the handle, as the mux keeps its own in its
BackendRegistry. Per-backend byte counters and connections_per_backend
are unchanged: they are keyed by the backend id the session already
recorded.

test_tcp_refused_backend_is_accounted_and_avoided runs sixteen
sequential sessions against a cluster whose first backend refuses
connections: on main the refused backend is dialed in all sixteen and
backend.connections.error is absent; with the fix it is dialed once and
the counter reads 1. The avoidance bound comes from the retry policy, not
from timing: ExponentialBackoffPolicy::fail arms at least one second of
back-off per failure, so over the measured duration the refused backend
can be dialed at most 1 + floor(elapsed) times, and the test requires that
bound to stay below the SESSIONS - 1 dials round-robin makes without the
accounting.

Closes #1658

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/tcp-backend-accounting branch from 5b2479b to 266bd16 Compare September 28, 2026 17:15
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 9009733 into main Sep 28, 2026
8 of 23 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/tcp-backend-accounting branch September 28, 2026 17:17
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.

fix(tcp): the raw TCP session never keeps its backend, so connect failures are not charged to it

1 participant