Repository navigation
fix(tcp): charge refused connects to the backend the TCP session dialed - #1661
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 14:20
FlorentinDUBOIS
force-pushed
the
fix/tcp-backend-accounting
branch
2 times, most recently
from
September 28, 2026 16:06
509705f to
5b2479b
Compare
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
force-pushed
the
fix/tcp-backend-accounting
branch
from
September 28, 2026 17:15
5b2479b to
266bd16
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1658
CAUSE
TcpSession::connect_to_backend(lib/src/tcp.rs) receives the selectedRc<RefCell<Backend>>fromBackendMap::backend_from_cluster_id, copies itsbackend_idand drops it, soTcpSession::backendstaysNone→TcpSession::fail_backend_connection(HUP on a connecting socket inTcpSession::ready_inner, i.e. a non-blocking connect refused afterEINPROGRESS),TcpSession::set_back_connectedandTcpSession::remove_backendall skip theirif let Some(backend) = self.backendbody → SYMPTOM: a refusing TCP backend never hasfailuresbumped nor itsretry_policyarmed, stays in round-robin selection, is never marked down,backend.connections.error/backend.down/backend.up/backend.availableare never emitted for TCP, andBackend::active_connections(incremented byBackend::try_connect) is never decremented.The mux keeps its handle in its
BackendRegistryand charges the same failure inMux::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_backenddo not change value: they are keyed bymetrics.backend_id, which was already set. What changes:backend.connections.error(and on down/up transitionsbackend.down,backend.up,backend.available,BackendDown/BackendUp) are now emitted for TCP backends;Backend::active_connectionsis released on close; the PeakEWMA connection time is fed; and, from readingimpl Drop for Backend, a removed TCP backend'sRemovedBackendHasNoConnectionsnow 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::failarms at least one second of back-off per recorded failure, and a backend in back-off failsBackend::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 most1 + floor(elapsed)times. Without the accounting, round-robin over the constant pair makes every session but possibly the first dial the refused backend, at leastSESSIONS - 1 = 15times. The test requireserrors <= 1 + floor(elapsed)and that bound to be strictly below 15.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. WithRUST_LOG=debug: the refused backend dialed 16 times for 16 sessions.Some(1)in each iteration,test result: ok. 1 passed; 10 runs out of 10 green (7 idle, 3 with 40 busyyesloops 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/main34d5e71, after #1657)cargo +nightly fmt --all -- --checkcargo build --lockedcargo build --all-features --lockedwithCC=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 withcargo clean -p aws-lc-fips-sys)cargo clippy --all-targets --locked -- -D warningscargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warningscargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8(e2e: 491 passed; lib: 1334 passed)tcp_tests10 passed,tcp_sni_tests19 passed,proxy_protocol_local_tests2 passed,protocol_pair_matrix24 passedpython3 .github/scripts/check_doc_citations.py --self-test,--root .,--root . --base origin/main,--audit --root .RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --lockedSigned-off-by: Florentin Dubois [email protected]