Repository navigation
fix(mux): stop truncating a slow client's response when its backend closed - #1820
Merged
Merged
Conversation
…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]>
…uncates-response 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]>
…uncates-response Signed-off-by: Florentin Dubois <[email protected]>
Signed-off-by: Florentin Dubois <[email protected]>
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.
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 oftls_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.rsonmain)::3218:HUP | ERRORare always in a backend's interest. A backend that is dead but kept for its buffered bytes (:3113-3115,has_buffer_pressure) therefore hasfilter_interest() == HUP, soall_backends_readiness_are_emptystays false.MAX_LOOP_ITERATIONSwithin oneready()call.:3425/:3435: the budget branch only consults the connection-levelhas_pending_write(). That is false for a plain-TCP H1 frontend, and for an H2 connection whose DATA is still inback.out. The branch returnsClose, and the body tail is never sent.Instrumentation at the budget branch, from the unit repro below: frontend
I(RWEH-) R(-----), backendI(R-EH-) R(---H-)withhas_buffer_pressure == true,frontend.has_pending_write() == false, one block inback.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_readingwakes it. A HUP sozu raises itself mid-pass (force_disconnectafter 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 awarn!with the frontend and backend readiness. If output is still queued (connection level, or in an open stream'sback.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 sharedhas_pending_write_including_streamsH1 arm is deliberately left unchanged, because widening it would also change every delayed-close and timeout path.mux/LIFECYCLE.md(close triggers, §7.4 step 6, invariant 12) anddoc/lifetime_of_a_session.mdnow 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_closedanda_half_closed_slow_client_...: the H1 frontend socket would block (small send/receive buffers), and the backend wrote 160 KiB and closed. Onmainthe first pass returnsCloseand 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 oldhas_pending_write()-only check.the_loop_limit_closes_a_session_with_nothing_queuedis the guard: a recycled slot's leftover bytes do not keep a session.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. Onmain: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.test_idle_h2_backend_sending_data_on_stream_zero_is_answered_at_onceandtest_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 H1
force_disconnectpath (ConnectionH1::end_stream) is not a regression: it clears the backend's interest before it raises HUP, sofilter_interest()was already empty onmain, and nothing counted that HUP before this PR either. Withhas_loop_workit now counts, so that backend is closed in the same pass rather than on the next event.Validation
cargo +nightly fmt --check: OKcargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings: OKRUSTDOCFLAGS="-D warnings" cargo +1.93.1 doc --no-deps --locked -p sozu-lib: OKcargo +1.93.1 test -p sozu-lib --lib --all-features --locked: 1597 passed--features opentelemetry,splice,simd: 625 passedcheck_doc_citations.py --root . --base origin/main: OKtest_plain_client_half_close_after_request_receives_large_response, 150 runs undertaskset -c 0with 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 onmain.Fixes #1819