Skip to content

fix(mux): stop truncating a slow client's response when its backend closed - #1820

Merged
FlorentinDUBOIS merged 6 commits into
mainfrom
fix/mux-loop-limit-truncates-response
Oct 3, 2026
Merged

FlorentinDUBOIS merged 6 commits into
mainfrom
fix/mux-loop-limit-truncates-response

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A slow client could get a truncated response, ending in a clean close, when the backend had already written the whole response and closed. Nothing was logged at the default level; the only trace was http.infinite_loop.error. This is the intermittent failure of tls_tests::test_plain_client_half_close_after_request_receives_large_response (CI run 37019159928, 8,284,694 of 8,388,608 body bytes).

Cause (lib/src/protocol/mux/mod.rs on main):

  • :3218: HUP | ERROR are always in a backend's interest. A backend that is dead but kept for its buffered bytes (:3113-3115, has_buffer_pressure) therefore has filter_interest() == HUP, so all_backends_readiness_are_empty stays false.
  • When the client's socket would block (or an H2 stream window is exhausted), nothing can progress, and the inner loop runs to MAX_LOOP_ITERATIONS within one ready() call.
  • :3425/:3435: the budget branch only consults the connection-level has_pending_write(). That is false for a plain-TCP H1 frontend, and for an H2 connection whose DATA is still in back.out. The branch returns Close, and the body tail is never sent.

Instrumentation at the budget branch, from the unit repro below: frontend I(RWEH-) R(-----), backend I(R-EH-) R(---H-) with has_buffer_pressure == true, frontend.has_pending_write() == false, one block in back.out. This matches the readiness that PR #1818 captured in the failing CI-shaped runs.

Fix

  • Connection::has_loop_work: a backend counts as loop work for READABLE or WRITABLE, or for a HUP or ERROR, unless it is kept for buffered bytes (has_buffer_pressure). Only a dead backend waiting for the client to drain the stream buffer stops counting; try_resume_reading wakes it. A HUP sozu raises itself mid-pass (force_disconnect after an H2 backend protocol error) still brings the loop back to close the backend and answer the client.
  • Mux::wait_for_client_at_loop_limit: reaching the budget logs a warn! with the frontend and backend readiness. If output is still queued (connection level, or in an open stream's back.out/back.blocks), the session keeps WRITABLE interest, drops the stale event, arms the frontend timeout, and waits for the next writable edge. A session with nothing queued is closed, and so is one that reaches the limit again with no byte written to the client since, so a spin that recurs on every wake-up costs at most two budgets. The outer loop no longer re-enters a spent budget. The budget stays as the safety bound. The shared has_pending_write_including_streams H1 arm is deliberately left unchanged, because widening it would also change every delayed-close and timeout path.
  • Corrected the stale comment that said interest never holds HUP/ERROR. mux/LIFECYCLE.md (close triggers, §7.4 step 6, invariant 12) and doc/lifetime_of_a_session.md now describe the exit condition and the budget. CHANGELOG entry added.

This is a different defect from #1640, where a backend FIN in the middle of the body was forwarded as a complete response. Here the backend sends the response whole and the proxy dropped its tail.

Tests (red on main, green here)

  • a_slow_client_receives_the_whole_response_of_a_backend_that_closed and a_half_closed_slow_client_...: the H1 frontend socket would block (small send/receive buffers), and the backend wrote 160 KiB and closed. On main the first pass returns Close and the client gets 32,805 of 163,883 bytes. With only the spin fix reverted: ran 9999 iterations.
  • the_loop_limit_waits_for_the_client_while_a_stream_holds_its_response: red with the old has_pending_write()-only check. the_loop_limit_closes_a_session_with_nothing_queued is the guard: a recycled slot's leftover bytes do not keep a session.
  • e2e test_h2_window_stalled_client_receives_the_whole_response_of_a_closed_backend: the H2 client keeps the default 65,535-byte windows until the backend has closed. On main: http.infinite_loop.error=1 reopened=false body=65535/131072. Here: http.infinite_loop.error=0 body=131072/131072 ended=true.

Limitation: the H1 frontend has no dedicated e2e. The kernel autotunes sōzu's frontend send buffer and no listener setting controls it, so a client that only reads slowly does not reach the state deterministically. I measured this: with the client stalled on a 4 MiB response, the frontend was still writable when the backend's FIN arrived, and stepped reads of 8 MiB never went red on main. H1 is covered by the two unit tests and by the stress run below. Flow control stalls the H2 frontend whatever the buffer sizes.

  • e2e test_idle_h2_backend_sending_data_on_stream_zero_is_answered_at_once and test_idle_h2_backend_sending_headers_on_an_idle_stream_is_answered_at_once: an idle H2 backend sends a bad frame and keeps its socket open. With 5000055 (the first version of this PR, which ignored every backend HUP): answered=false ending=timeout elapsed=12.0s. Here: a 5xx in about 0.51 s.
  • a_second_loop_limit_without_progress_closes_the_session: pins the bounded wait.
  • The slow-client unit tests count limit hits with a test-only counter, so they also check in release builds. Red in release with the fix reverted: 284 and 262 hits.

The H1 force_disconnect path (ConnectionH1::end_stream) is not a regression: it clears the backend's interest before it raises HUP, so filter_interest() was already empty on main, and nothing counted that HUP before this PR either. With has_loop_work it now counts, so that backend is closed in the same pass rather than on the next event.

Validation

  • cargo +nightly fmt --check: OK
  • cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings: OK
  • RUSTDOCFLAGS="-D warnings" cargo +1.93.1 doc --no-deps --locked -p sozu-lib: OK
  • cargo +1.93.1 test -p sozu-lib --lib --all-features --locked: 1597 passed
  • full e2e --features opentelemetry,splice,simd: 625 passed
  • check_doc_citations.py --root . --base origin/main: OK
  • test_plain_client_half_close_after_request_receives_large_response, 150 runs under taskset -c 0 with 4 busy loops pinned to CPU 0: 0/150 failures (450 exchanges) with the first version, and 0/100 with the current head. The same setup gave 1/30, 1/60, 1/80 and 2/150 on main.

Fixes #1819

…losed

A backend that had written its whole response and closed before a slow
client drained it was kept by the dead-backend check for its buffered
bytes, but its HUP, always in its interest, kept `filter_interest()`
non-empty and counted as work for the inner loop of `Mux::ready_inner`.
With the client's socket full, or an HTTP/2 stream window exhausted,
nothing could progress and the loop spun to `MAX_LOOP_ITERATIONS` in one
pass. The budget branch only consulted the connection-level
`has_pending_write()`, so it closed the session with the rest of the body
still in the stream's buffer: a clean close after a truncated body, with
only `http.infinite_loop.error` recording it.

- `backend_has_work`: a backend counts as work only for READABLE or
  WRITABLE. A dead backend kept for its bytes waits for the client to
  drain the stream buffer, which `try_resume_reading` signals.
- `Mux::wait_for_client_at_loop_limit`: reaching the budget logs a
  warning; a session whose output is still queued, in the connection or
  in an open stream's response buffer, waits for its next writable event
  instead of closing. The budget stays as a safety bound.
- Correct the stale comment claiming interest never holds HUP/ERROR.

Tests: two unit tests drive an HTTP/1.1 frontend whose socket would block
against a backend that wrote 160 KiB and closed, with and without a
client half-close; two unit tests pin the budget branch; an e2e test
stalls an HTTP/2 client on its flow-control window. All but the
nothing-queued budget guard failed on main.

Fixes #1819

Signed-off-by: Florentin Dubois <[email protected]>
…it wait

Review of the previous commit found that ignoring every backend HUP as
loop work also ignored the one sozu raises itself during an iteration:
when an HTTP/2 backend breaks the protocol, `ConnectionH2::force_disconnect`
sets the backend's event to HUP after that iteration's dead-backend check
ran. Main counted it, iterated once more, closed the backend and answered
the client 502; with the previous commit the loop stopped, and a backend
that keeps its socket open left the client waiting for a timeout.

- `Connection::has_loop_work` (replaces `backend_has_work`): READABLE or
  WRITABLE, or a HUP or ERROR unless the backend is kept for buffered
  bytes (`has_buffer_pressure`). Only the backend waiting for the client
  to drain the stream buffer stops counting.
- `Mux::wait_for_client_at_loop_limit`: a second time at the limit with no
  byte written to the client since (`Context::loop_limit_bytes_out`)
  closes the session, so a spin that recurs on every wake-up costs at
  most two budgets. The outer loop no longer re-enters a spent budget,
  which counted and logged the same limit twice.
- Correct the `has_pending_write_including_streams` comment: the H1 arm
  checks the socket only, not the stream's response buffer.
- The slow-client unit tests count limit hits with a test counter that
  works in release builds; the debug history is empty there.

Tests: two e2e tests have an idle HTTP/2 backend send DATA on stream 0 or
HEADERS on a stream sozu never opened while keeping its socket open; with
the previous commit the client got no answer in 12 s, here a 5xx in about
0.5 s. A unit test pins the bounded wait.

Refs #1819

Signed-off-by: Florentin Dubois <[email protected]>
…ount

The loop-limit wait read progress as the sum of every stream's `bout`,
but `SessionMetrics::reset` zeroes it when a stream ends and its slot is
reused, so that sum can decrease or land back on its old value. Progress
is now `Context::client_bytes_out`, which only grows: `ConnectionH1::writable`
and `H2Shell::write_streams` add every byte a server-position write hands
to the client socket.

Once the budget is spent, the outer loop now re-enters the inner one once
more for a stream that sweep just linked (an attached pooled backend is
armed by a synthetic event no epoll edge repeats); that iteration stops at
the spent budget without counting or logging the limit again.

The H2 window-stall and idle-backend e2e tests complete the TLS handshake
under a generous read timeout and only then arm the 250 ms one that paces
their reads, so a loaded host no longer fails their setup with sent=false.

`doc/h2_mux_internals.md` follows the h2.rs lines this shifts.

Tests: `a_stream_slot_reused_between_two_loop_limits_does_not_fake_progress`
is red with the per-stream sum (a reset metric reads as progress), and the
slow-client tests now require every byte the client received to be in the
count, red without the H1 write-path increment.

Refs #1819

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 77e9c69 into main Oct 3, 2026
23 checks passed
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: response truncated to a slow client when the backend closed with data still buffered

1 participant