Repository navigation
fix(mux): yield a progressing session at the loop budget instead of closing it - #1918
Merged
Merged
Conversation
With `rewrite_host` on a frontend, `apply_request_rewrites_and_headers` (`lib/src/protocol/mux/router.rs`) rewrote the request authority and then added a synthetic `Host` header block. The HTTP/1.1 converter (`kawa::protocol::h1::converter`) already writes `Host: <authority>` right after the request line, so an HTTP/1.1 backend received two `Host` headers, and servers that reject a duplicate (Go's `net/http`) answered 400. The HTTP/2 converter filters every `host` block, so the block was useless there. An operator request header named `Host` had the same root cause and worse: the backend received the client's authority and the operator's value as two different `Host` lines, the smuggling primitive the code comment claimed to prevent. The authority is now the only carrier of the backend `Host`: no `Host` header block is ever inserted. A non-empty `Host` request header edit replaces the authority (last one wins) unless `rewrite_host` is set, which keeps precedence; an empty one still drops header blocks only. An HTTP/2 backend therefore now receives that edit as `:authority`. The H1→H1 rewrite test now requires exactly one `Host`; new e2e tests cover the operator `Host` edit (H1→H1) and `rewrite_host` from an HTTP/2 frontend to an HTTP/1.1 backend. Fixes #1908 Signed-off-by: Florentin Dubois <[email protected]>
…losing it A large Content-Length response was cut at a random offset (the client reported "Premature end of Content-Length delimited message body"), on plaintext and TLS listeners alike; the ALPN list played no part. CAUSE: `Mux::ready_inner` counts every inner-loop iteration against `MAX_LOOP_ITERATIONS` (10,000), whether or not it moved bytes. When the client and the backend both keep up, no socket answers WouldBlock, so one `ready()` call reaches the budget after about 160 MiB with 16 KiB buffers. `Mux::wait_for_client_at_loop_limit` then finds nothing queued, because the frontend has just drained, and returns `SessionResult::Close`: a clean close in the middle of the body. 2.2.1 has the same defect, without a log at the default level. A new `Context::stream_bytes_out` counts the stream bytes written to either peer (`ConnectionH1::writable`, `H2Shell::write_streams`). A call that reaches the budget after it moved now yields instead. It keeps every readiness bit, sets a flag, and counts `http.mux.loop_budget_yield`. The HTTP/HTTPS sessions report the flag through a new `ProxySession::yielded` (default `false`). The server queues the session and resumes it after its next poll with no new event (edge-triggered epoll would not deliver one), using a non-blocking poll while anything is queued. A session that closed in between is skipped by weak-reference and slab-identity checks. A fast transfer is therefore neither cut nor allowed to hold the single-threaded worker for more than one budget per turn. A call that forwarded nothing keeps the existing `http.infinite_loop.error` path. Tests: a mux unit test drives the resumed passes with no `update_readiness` and fails on the old close; server unit tests cover resumption and a session closed before it; e2e `large_response_tests` (256 MiB downloads/uploads, plaintext and TLS with ALPN h2,http/1.1) cut 4/40, 5/60 and 10/20 trials on main and none with the fix. Fixes #1906 Signed-off-by: Florentin Dubois <[email protected]>
The yield at `MAX_LOOP_ITERATIONS` fired when any stream byte moved during the call. A spin that trickled a few bytes would then yield on every event-loop turn instead of reaching the spin path, burning a full budget of CPU per turn. That fails open. `progressed_through_loop_budget` now requires the call to have moved at least one stream byte per budgeted iteration (wrapping difference of `Context::stream_bytes_out`). A real transfer forwards up to a buffer per iteration, so it still yields; a trickle falls back to `Mux::wait_for_client_at_loop_limit` and `http.infinite_loop.error`. Refs #1906 Signed-off-by: Florentin Dubois <[email protected]>
…nse up `back_timeout` and `front_timeout` bound the inactivity of one socket each, yet both cut a response held up by the other peer: - A client that paused longer than `back_timeout` left Sōzu holding response bytes, so Sōzu stopped reading the backend itself; the backend timer then fired and force-terminated the response (`backend_response_timeout`). - A backend that paused longer than `front_timeout` left a client that had taken every byte waiting; the frontend timer then took the "response fully proxied" arm, because kawa's `is_completed` only means nothing is queued, and the session closed silently mid-body. `response_waits_on_client` (stream bytes queued, or the H1 frontend's own pending write) now decides. The backend timeout defers to the frontend while it holds, re-arming only the backend and returning before the shared tail, which would re-arm the frontend. The frontend timeout defers to the backend while it does not, and the "fully proxied" arm requires a terminated or errored response. The two conditions are complementary, so a client that stops reading still meets `front_timeout` and a silent backend still meets `back_timeout`. Unit tests cover both deferrals and the frontend complement; two e2e tests pause 4 s against 2 s timeouts and were cut at 4.0 and 6.0 of 8 MiB before the fix. Refs #1906 Signed-off-by: Florentin Dubois <[email protected]>
`cargo audit --deny warnings` (CI "cargo audit") fails on main's lockfile with two findings published after its last green run: - RUSTSEC-2026-0433: pulldown-cmark 0.13.5, quadratic parsing of nested image wikilinks with ENABLE_WIKILINKS. Fixed in 0.13.6. A build dependency only, through prost-build. - async-executor 1.14.0 is yanked. Moved to 1.14.1. Lockfile-only: both are patch updates within the existing requirements. Signed-off-by: Florentin Dubois <[email protected]>
`cargo audit --deny warnings` (CI "cargo audit") fails on main's lockfile with two findings published after its last green run: - RUSTSEC-2026-0433: pulldown-cmark 0.13.5, quadratic parsing of nested image wikilinks with ENABLE_WIKILINKS. Fixed in 0.13.6. A build dependency only, through prost-build. - async-executor 1.14.0 is yanked. Moved to 1.14.1. Lockfile-only: both are patch updates within the existing requirements. Signed-off-by: Florentin Dubois <[email protected]>
Brings in #1919 (lockfile) and #1914 (command channel error) and keeps both CHANGELOG slots, each followed by a blank line. Signed-off-by: Florentin Dubois <[email protected]>
Stacks this branch on #1915, itself up to date with main after #1919 (lockfile) and #1914, so CI runs on the tree this PR lands. Keeps every CHANGELOG slot, each followed by a blank line. Signed-off-by: Florentin Dubois <[email protected]>
This was referenced Nov 3, 2026
main now carries #1915 as a squash commit, whose CHANGELOG insertion conflicts with the stacked original. Keeps every CHANGELOG slot; the tree is unchanged. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Nov 3, 2026
main now carries #1918 as a squash commit, whose CHANGELOG insertion conflicts with the stacked original. Keeps every CHANGELOG slot; the tree is unchanged. 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.
Fixes #1906.
Root cause
The download is not cut by a timer, and the listener's ALPN list plays no part. HTTP/1.1 over plaintext and TLS is exposed, and HTTP/2 frontends run the same loop.
Mux::ready_innercounts every inner-loop iteration againstMAX_LOOP_ITERATIONS(10,000), whether the iteration moved bytes or not. When the client and the backend both keep up, no socket ever answersWouldBlock, so oneready()call reaches the budget after about 10,000 × one buffer, roughly 160 MiB with 16 KiB buffers.Mux::wait_for_client_at_loop_limitthen finds nothing queued, because the frontend has just drained, and returnsSessionResult::Close. The result is a clean close in the middle of aContent-Lengthbody:Premature end of Content-Length delimited message body. The cut offset depends on buffer size and on how long both peers keep up, which explains the "random" offsets. 2.2.1 has the same defect, with no log at the default level. On 2.3.x it logsMux loop reached 10000 iterations, closing, and both versions incrementhttp.infinite_loop.error.The fixes shipped after 2.2.1 (#1820, #1789, #1787, #1795, #1773) handle spins with no progress, half-close and stalled TLS readers. None of them covers a session that makes progress on every iteration.
Fix
Context::stream_bytes_outcounts the stream bytes written to either peer (ConnectionH1::writable,H2Shell::write_streams; H2 control frames excluded).progressed_through_loop_budget, wrapping difference) yields. A spin that trickles a few bytes therefore still reaches the spin path instead of yielding every turn. This threshold was added after an automated security review flagged the earlier "any byte moved" rule as failing open, and its unit testonly_a_byte_per_budgeted_iteration_counts_as_progress_at_the_loop_budgetwas seen red under the old rule. It keeps every readiness bit, sets a flag, logs atdebug!, and counts the newhttp.mux.loop_budget_yieldcounter. A call that forwarded nothing keeps the existinghttp.infinite_loop.errorpath unchanged. A spin that follows some progress is caught on the next call, because the baseline resets on entry.ProxySession::yielded()(defaultfalse) is overridden byHttpSessionandHttpsSessionwhile their state isMux. Pipe, TCP and UDP are untouched.Serverqueues a yielded session (at most once) and resumes it withready()after the next poll's events, with no new event: edge-triggered epoll would never re-deliver the readiness that was left unread. While anything is queued, the poll is non-blocking (Duration::ZERO), so other sessions get their turn first. A session that closed in between, or whose token was reused, is skipped by aWeak+Rc::ptr_eqslab check, the same pattern asdrive_closing_sessions.A first version of this fix only reset the budget whenever bytes moved. That stops the cut, but it lets a single fast transfer hold the single-threaded worker for the whole transfer. The yield bounds every session to one budget per event-loop turn.
Tests
a_transfer_progressing_at_the_loop_limit_yields_and_resumes_without_a_new_event: the first pass is notCloseand sets the yield flag, and later passes run with noupdate_readinessand deliver the whole body. Seen red with the yield arm replaced byreturn SessionResult::Close(the transfer must not be closed while it progresses).yielded_sessions_tests: resumption until the session stops yielding and the zero poll timeout; a yielded session closed before resumption, with its slab key reused, is not resumed (seen red without the identity check).large_response_tests.rs: 256 MiB plaintext download, TLS download (ALPNh2,http/1.1, HTTP/1.1 client) and TLS upload. Cut rate onmain: 4/40, 5/60 and 10/20 trials. With the fix: 0 cuts over 15 runs × 3 trials each. With the yield arm reverted to close:6 passed; 3 failed.Commands run
cargo +nightly fmt --all -- --check: exit 0cargo clippy --all-targets --locked -- -D warnings: exit 0cargo +1.93.1 check --workspace --all-targets --locked(MSRV): exit 0cargo build --all-features --locked: exit 0python3 .github/scripts/check_doc_citations.py --root .: exit 0RUSTDOCFLAGS=-D warnings cargo +1.93.1 doc --no-deps --all-features --locked: exit 0cargo test --workspace --locked -- --skip tests::fuzz_tests::: exit 0, 3427 passed, 0 failed (e2e 677 passed), on the final headSecond mechanism: a timeout charged to the wrong peer (fixed in
cee25c1f)The same symptom, a response cut mid-body, also came from the timers.
back_timeoutandfront_timeoutbound the inactivity of one socket each (doc/configure.md), yet:back_timeoutleft Sōzu holding response bytes, so Sōzu stopped reading the backend itself, and the backend timer then force-terminated the response (backend_response_timeout);front_timeoutleft a client that had taken every byte waiting, and the frontend timer took the "response fully proxied" arm. kawa'sis_completed()only means nothing queued, so the session closed silently mid-body.response_waits_on_client(stream bytes queued, or the H1 frontend's own pending write) now decides which timer may cut. The backend timeout defers to the frontend while it holds; it re-arms only the backend and returns before the shared tail, which would otherwise re-arm the frontend. The frontend timeout defers to the backend while it does not, and its "fully proxied" arm now requires a terminated or errored response. The two conditions are complementary, so a client that stops reading still meetsfront_timeout, and a silent backend still meetsback_timeout.a_backend_timeout_leaves_no_linked_stream_behind_on_the_close_path. The backend deferral was seen red with its arm disabled; the frontend deferral was seen red before the arm fix (CloseSession).test_large_download_client_pause_longer_than_back_timeout_plainandtest_large_download_backend_pause_longer_than_front_timeout_plainuse 4 s pauses against 2 s timeouts. Without the fix they were cut at 4.0 and 6.0 of 8 MiB with a clean EOF; with it they pass (3 trials each).cee25c1f: clippy at 1.99.1 and at 1.93.1 (--features opentelemetry,splice,simd), rustdoc-D warnings, and doc citations all exit 0;cargo test --workspace --locked -- --skip tests::fuzz_tests::: 3432 passed, 0 failed (e2e 679).Confirming on the reporter's side
On the affected version: one
http.infinite_loop.errorincrement per cut. On 2.3.x, the warningMux loop reached 10000 iterations, closing. With a 387 MB cut,buffer_sizeshould be around 387 MB / 10,000 ≈ 38.7 KB.Docs
lib/src/protocol/mux/LIFECYCLE.md,doc/lifetime_of_a_session.md,doc/h2_mux_internals.md(citations),doc/configure.md(new counter), CHANGELOGUnreleased› Fixed.Dependencies
Stacked on #1915 (this branch merges it), so CI runs on exactly the tree this PR lands on
main. Until #1915 merges, the commit list and diff also show its changes; the squash merge carries only this PR's own. #1919 (lockfile) and #1914 are already onmain. Merge order: #1915 → #1918 → #1916 → #1917.