Skip to content

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

Closed

Description

@FlorentinDUBOIS

Summary

On the raw TCP proxy (lib/src/tcp.rs), TcpSession::backend is never set: it is initialised to None in both constructors and no code path assigns it. TcpSession::connect_to_backend gets the selected Rc<RefCell<Backend>> from BackendMap::backend_from_cluster_id, uses it only to copy backend_id into the session metrics, and drops it.

Every per-backend bookkeeping step that reads self.backend is therefore a no-op on TCP:

  • TcpSession::fail_backend_connection (called from TcpSession::ready_inner when a Connecting backend socket reports HUP): no Backend::failures += 1, no retry_policy.fail(), no backend.connections.error, no backend.down / backend.available / BackendDown event.
  • TcpSession::set_back_connected(Connected): no retry_policy.succeed(), no failures = 0, no set_connection_time (the PeakEWMA input), no backend.up / BackendUp on recovery.
  • TcpSession::remove_backend: no Backend::dec_connections(), while Backend::try_connect did inc_connections() on every successful dial, so Backend::active_connections only grows on TCP clusters.

The mux does charge the backend: it keeps the handle in its BackendRegistry and runs the same failure accounting in Mux::ready for a Position::Client(.., BackendStatus::Connecting(_)).

A synchronous connect(2) error is already charged by Backend::try_connect itself. The gap is the usual case of a non-blocking connect: connect(2) returns EINPROGRESS and the refusal arrives later as a HUP on the registered socket, which only fail_backend_connection can account.

Impact

  • A TCP backend that refuses connections is never taken out of selection by the retry policy and never marked down: round-robin keeps dialing it, and each session that lands on it pays a failed connect and a retry before reaching a live backend (or exhausts CONN_RETRIES when several backends are dead).
  • backend.connections.error, backend.down, backend.up, backend.available and the BackendDown / BackendUp events are never emitted for raw TCP backends, and the cluster availability surface described in doc/health_checks.md ("each TCP connect failure on the data path arms Backend::retry_policy") does not hold for the TCP proxy without an active health check.
  • Backend::active_connections leaks upward, which skews connection-based load balancing (LeastLoaded, PowerOfTwo with the connections metric).

Reproduction

A TCP cluster with two backends, the first on a closed port, the second listening, round-robin; 8 sequential sessions through the TCP listener. All 8 sessions are served in both cases (each failed connect is retried on the live backend inside the session), but with RUST_LOG=debug the worker's Connecting cluster_0 -> (addr, ..) lines show:

dials to the refused backend backend.connections.error (cluster row)
main (b3f2c78) 8 of 8 sessions absent
with the session keeping its backend handle 1 (then excluded for its back-off window) 1

The e2e test test_tcp_refused_backend_is_accounted_and_avoided (e2e/src/tests/tcp_tests.rs) in the linked pull request encodes this scenario and fails on main.

Fix

Store the selected handle in TcpSession::connect_to_backend (self.backend = Some(Rc::clone(&backend))), so that the existing accounting in fail_backend_connection, set_back_connected and remove_backend runs as written.

Activity

  1. added 3 commits that reference this issue on Sep 28, 2026
    509705f
    5b2479b
    266bd16
  2. added a commit that references this issue on Sep 28, 2026
    9009733
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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