Skip to content

http: a stale keep-alive upstream connection is answered with 502 instead of being retried on another backend #1442

Closed

Description

@FlorentinDUBOIS

What happens

When a backend that served a keep-alive request is stopped, the next request routed to the same cluster is written onto the back connection whose peer has already closed. sozu reads EOF and answers 502 Bad Gateway instead of retrying on a surviving backend.

Measured over 401 trials of test_issue_806 on main, with the assertions hardened: 25 failures, a 6.2% per-trial rate, reproducible on 25 of 25 runs.

The 502 body names the stopped backend:

backend1 aggregator: Some(SimpleAggregator { requests_received: 1, responses_sent: 1 })
backend2 aggregator: Some(SimpleAggregator { requests_received: 0, responses_sent: 0 })
reconnect: 651.979µs
the request after the backend stop was answered "HTTP/1.1 502 Bad Gateway", not "HTTP/1.1 200 OK"

with "backend_id": "cluster_0-0" — the backend that was stopped — and "parsing_phase": "Error".

Note reconnect: 651.979µs. This is a fast wrong answer, well inside any timing budget, so a test that only measures latency cannot see it.

Why it is not a test artefact

The mock backend closes gracefully: stop_and_get_aggregator drops the accepted streams on thread exit. This is the ordinary stale-keep-alive-upstream case that every reverse proxy has to handle — the peer FIN'd, sozu had not yet noticed, and the request was already written.

Cross-tabulated over the same 401 trials, the correspondence is exact with zero disagreement:

backend2 aggregator status trials
(1, 1) 200 376
(0, 0) 502 25

Where the retry is missing

CONN_RETRIES exists only in lib/src/tcp.rs. The HTTP path has no equivalent: a write onto a back connection that turns out to be closed is not retried against another backend from the load balancer.

Suggested resolution

On EOF from an upstream connection where no response bytes have yet been received, the request is idempotent with respect to the backend — nothing was observed, so re-issuing it on a fresh connection from the load balancer is safe. That is the standard boundary: retry only before any response byte arrives.

What I verified, and what I did not

Verified at source and by measurement: the 6.2% rate, the exact cross-tabulation, the stopped backend named in the 502 body, the graceful close in the mock, and the absence of CONN_RETRIES outside lib/src/tcp.rs.

Not verified: whether an existing retry path elsewhere in the HTTP state machine was intended to cover this and is simply not reached, and whether the right boundary is "no response bytes yet" or something narrower for non-idempotent methods.

Found while hardening test_issue_806 for #1427: the test previously asserted != State::Fail, which Undecided satisfied, and did not check the response status at all, so these 502s passed as successes.

Activity

  1. added 5 commits that reference this issue on Sep 22, 2026
    e4d7aca
    b387798
    fd8a6f8
    4d87d9c
    2e0f36d
  2. added 6 commits that reference this issue on Sep 23, 2026
    d8b8546
    9a2ebcd
    18c3e00
    1b0c7cc
    16e26b7
    eac0409
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