Repository navigation
fix(mux-h2): wait for an incomplete header block on soft-stop and close with a final GOAWAY - #1654
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 09:59
FlorentinDUBOIS
force-pushed
the
fix/h2-soft-stop-mid-header-block
branch
from
September 28, 2026 10:11
459f0a2 to
316f6dd
Compare
…se with a final GOAWAY A stream whose HEADERS/CONTINUATION block is still being reassembled is StreamState::Idle, and Mux::shutting_down_inner kept a draining session open only for Linked and non-quiesced Unlinked streams. A soft-stop landing while a connection's only stream was mid-block closed the session as soon as the advisory GOAWAY(NO_ERROR, 2^31-1) was flushed: the request was lost and the client never received the final GOAWAY telling it whether the stream had been processed (RFC 9113 section 6.8). Mux::shutting_down_inner now keeps an H2 frontend open while ConnectionH2::peer_header_block_in_progress holds and while a stream is StreamState::Link, the state a CONTINUATION read by Mux::drive_frontend_shutdown_io leaves until Mux::ready links it. Both waits are bounded by h2_graceful_shutdown_deadline_seconds. The request is then served and ConnectionH2::finalize_write sends the final GOAWAY with its stream id. When the budget elapses, ConnectionH2::goaway_before_forced_close queues the final GOAWAY before the forced close, for every draining H2 session; its last_stream_id excludes a stream whose opening block never completed, so the client may retry it. Pinned by test_h2_graceful_drain_waits_for_an_incomplete_header_block and test_h2_graceful_drain_deadline_mid_header_block_sends_final_goaway, both red on ca42a2e, by test_h2_graceful_drain_deadline_with_a_linked_stream_sends_final_goaway, red with goaway_before_forced_close neutralized, and by the unit test a_draining_h2_session_waits_for_a_stream_awaiting_its_link. LIFECYCLE.md gains invariant 31 and rewrites invariant 10, section 8.1 and section 8.3; the pinned h2.rs blocks in doc/h2_mux_internals.md are re-anchored. Closes #1647 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/h2-soft-stop-mid-header-block
branch
from
September 28, 2026 10:28
316f6dd to
6335546
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 28, 2026
…h the request lifecycles Record the work merged from 2026-09-26 to 2026-09-28 that cut the system calls and heap allocations a worker pays per request, and bring the request lifecycle documentation back in line with the code. - doc/hot_path_zero_copy.md (new, linked from doc/README.md): goal and rules, the measurement method (intentrace, an LD_PRELOAD descriptor tracer and allocation counter, crate::test_allocations), one section per change with its before/after figures and pull requests, the correctness and safety fixes found along the way, the invariants and the tests that hold them, and the list of pull requests. - A closing measurement, taken on 2026-09-28 with release binaries at b5169d4 and c086b45 interleaved (crypto-ring, one worker, file:// access log, python backend, curl, N=20, intentrace), per request: H1 26.25-26.70 -> 18.45-18.85 system calls, 83.65-83.85 -> 39.95-40.35 heap operations; H1 keep-alive 14.75 -> 9.65 and 50.80 -> 7.80; H2 over TLS 61.25-61.60 -> 40.15-40.75 and 412.15-413.90 -> 240.60-241.75; H2 multiplexed 25.05 -> 13.55; TCP relay 39.00-39.10 -> 36.05-36.15. - doc/lifetime_of_a_session.md, rewritten: event loop, accept, TLS, PROXY pre-flight, the H1, H2, TCP and UDP lifecycles, close, soft stop and GOAWAY, with the system calls and allocations each step still pays. It corrects three statements the code contradicted: a reused keep-alive backend gets no liveness probe, a TCP session is closed on the first soft-stop pass, and a UDP listener is drained by UdpProxy::notify's SoftStop arm rather than by shutting_down. - The H2 soft stop follows #1654 (#1647): a draining session waits for a header block still arriving or a request awaiting its backend link, and a forced close is preceded by a final GOAWAY. The TCP soft stop is described as it has behaved since be5dd44 (2020-01-10). - The H1 smuggling fixes (#1651, #1653) are described as shipped: kawa 0.7.2 (#1655) fixes both upstream, the guards stay as defense in depth, and their content_length_invalid counters stay at zero unless kawa regresses. - lib/src/protocol/udp/LIFECYCLE.md: drop the SelectBackend / BackendResolved round trip that no longer exists (selection moved into the core), and add a per-datagram cost table. - lib/src/protocol/mux/LIFECYCLE.md: a section on what a request costs on the nominal path; a stale Router::connect reference is now Router::plan_connect. - CHANGELOG, mux LIFECYCLE, h2_mux_internals: an H2 backend connection belongs to one Mux session and carries the streams of one frontend connection only; the #1627, #1631 and #1632 entries no longer call it shared by several clients. A Documentation entry is added. - Line citations in the touched documents (lifetime_of_a_session, the UDP, kawa-H1 and mux LIFECYCLE files) are now symbol citations; pinned code blocks keep their verified line ranges. - Comments only in lib/src (http.rs, https.rs, mux/mod.rs, mux/h2.rs, mux/stream.rs, udp.rs, udp/flow.rs, udp/manager.rs): section references into the rewritten lifetime_of_a_session.md (the close path is now its section 11), the retired UDP SelectBackend / BackendResolved round trip, and the frontend-connection scope of a shared H2 backend connection. doc/udp_simulation.md and doc/testing.md describe the ChangeBackendSet action that replaced BackendResolved in the UDP simulator. Documentation and comments only: every changed .rs line is a comment, no production code, test or dependency change. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 28, 2026
…h the request lifecycles Record the work merged from 2026-09-26 to 2026-09-28 that cut the system calls and heap allocations a worker pays per request, and bring the request lifecycle documentation back in line with the code. - doc/hot_path_zero_copy.md (new, linked from doc/README.md): goal and rules, the measurement method (intentrace, an LD_PRELOAD descriptor tracer and allocation counter, crate::test_allocations), one section per change with its before/after figures and pull requests, the correctness and safety fixes found along the way, the invariants and the tests that hold them, and the list of pull requests. - A closing measurement, taken on 2026-09-28 with release binaries at b5169d4 and c086b45 interleaved (crypto-ring, one worker, file:// access log, python backend, curl, N=20, intentrace), per request: H1 26.25-26.70 -> 18.45-18.85 system calls, 83.65-83.85 -> 39.95-40.35 heap operations; H1 keep-alive 14.75 -> 9.65 and 50.80 -> 7.80; H2 over TLS 61.25-61.60 -> 40.15-40.75 and 412.15-413.90 -> 240.60-241.75; H2 multiplexed 25.05 -> 13.55; TCP relay 39.00-39.10 -> 36.05-36.15. - doc/lifetime_of_a_session.md, rewritten: event loop, accept, TLS, PROXY pre-flight, the H1, H2, TCP and UDP lifecycles, close, soft stop and GOAWAY, with the system calls and allocations each step still pays. It corrects three statements the code contradicted: a reused keep-alive backend gets no liveness probe, a TCP session is closed on the first soft-stop pass, and a UDP listener is drained by UdpProxy::notify's SoftStop arm rather than by shutting_down. - The H2 soft stop follows #1654 (#1647): a draining session waits for a header block still arriving or a request awaiting its backend link, and a forced close is preceded by a final GOAWAY. The TCP soft stop is described as it has behaved since be5dd44 (2020-01-10). - The H1 smuggling fixes (#1651, #1653) are described as shipped: kawa 0.7.2 (#1655) fixes both upstream, the guards stay as defense in depth, and their content_length_invalid counters stay at zero unless kawa regresses. - lib/src/protocol/udp/LIFECYCLE.md: drop the SelectBackend / BackendResolved round trip that no longer exists (selection moved into the core), and add a per-datagram cost table. - lib/src/protocol/mux/LIFECYCLE.md: a section on what a request costs on the nominal path; a stale Router::connect reference is now Router::plan_connect. - CHANGELOG, mux LIFECYCLE, h2_mux_internals: an H2 backend connection belongs to one Mux session and carries the streams of one frontend connection only; the #1627, #1631 and #1632 entries no longer call it shared by several clients. A Documentation entry is added. - Line citations in the touched documents (lifetime_of_a_session, the UDP, kawa-H1 and mux LIFECYCLE files) are now symbol citations; pinned code blocks keep their verified line ranges. - Comments only in lib/src (http.rs, https.rs, mux/mod.rs, mux/h2.rs, mux/stream.rs, udp.rs, udp/flow.rs, udp/manager.rs): section references into the rewritten lifetime_of_a_session.md (the close path is now its section 11), the retired UDP SelectBackend / BackendResolved round trip, and the frontend-connection scope of a shared H2 backend connection. doc/udp_simulation.md and doc/testing.md describe the ChangeBackendSet action that replaced BackendResolved in the UDP simulator. Documentation and comments only: every changed .rs line is a comment, no production code, test or dependency change. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 28, 2026
…h the request lifecycles (#1656) ## Summary Writes up the hot-path zero-copy and system-call work merged between 2026-09-26 and 2026-09-28, and rewrites the request lifecycle documentation so it describes the code as it now stands. The work covers the access logs, the request id, `TCP_INFO`, `ClusterId`, the H1 header editor, the H2 output queue and HPACK codec, the read and write paths, accept and close, session setup, and the drain loop. The new document says what changed, how it works, why it was done, and what was measured before and after each change. The lifecycle documents now say, for UDP, TCP, H1, H2 and the mux, which system calls and allocations each step still pays on the nominal path. Documentation and comments only: every changed `.rs` line is a comment (`git diff origin/main -- '*.rs' | grep '^[-+]' | grep -vE '^[-+]\s*(//|\*|$)|^(\+\+\+|---)'` prints nothing), and no production code, test or dependency changes. ## Files | File | Why | |---|---| | `doc/hot_path_zero_copy.md` (new) | Campaign document. §1 goal and rules; §2 method (fixed scenario, instruments) and the closing measurement; §3 one section per theme, each with its mechanism, rationale, before/after figures and PRs; §4 correctness and safety fixes found along the way; §5 invariants and the tests that hold them; §6 the 66 PRs merged in the window. | | `doc/lifetime_of_a_session.md` | Rewritten: event loop, accept, TLS, PROXY pre-flight, the H1, H2, TCP and UDP lifecycles, the mux session, close, soft stop and GOAWAY. Each protocol has a table of the system calls and allocations still paid on the nominal path. | | `lib/src/protocol/udp/LIFECYCLE.md` | Removes the `SelectBackend` / `BackendResolved` round trip, which no longer exists, and adds a §13 per-datagram cost table. Citations converted to symbols. | | `lib/src/protocol/mux/LIFECYCLE.md` | Adds §12, what a request costs on the nominal path. Converts the last five line citations outside pinned blocks, corrects the "cross-client" wording, and replaces `Router::connect` with `Router::plan_connect`. | | `lib/src/protocol/kawa_h1/LIFECYCLE.md` | 23 line citations converted to symbols. The §2.1/§2.2 wording from #1655 is kept. | | `doc/h2_mux_internals.md` | Corrects the "cross-client" wording in three places. The pinned blocks are unchanged. | | `CHANGELOG.md` | Corrects the "cross-client" wording in the #1627, #1631 and #1632 entries, and adds a `docs` entry. | | `doc/README.md` | Links the new document. | | `doc/architecture.md`, `CLAUDE.md` | Both still described `file.rs:LINE` citations, and `CLAUDE.md` still pointed at the stale `lifetime_of_a_session.md`. | | `doc/udp_simulation.md`, `doc/testing.md` | Describe the `ChangeBackendSet` action that replaced `BackendResolved` in the UDP simulator. | | comments in `lib/src/{http,https}.rs`, `lib/src/protocol/mux/{mod,h2,stream}.rs`, `lib/src/udp.rs`, `lib/src/protocol/udp/{flow,manager}.rs` | Section references into the rewritten `lifetime_of_a_session.md` (the close path is now §11), the retired UDP `SelectBackend` / `BackendResolved` round trip, and the frontend-connection scope of a shared H2 backend connection. Line counts unchanged, so no pinned block moves. | ## Closing measurement Measured on 2026-09-28. Two release binaries (`--no-default-features --features crypto-ring,opentelemetry,splice,simd`, glibc malloc), one built at `b5169d44` and one at `c086b456`, run interleaved pass by pass with two repetitions each. During the intentrace passes the one-minute load average was 10.7–24.9 in the first repetition and 2.7–8.5 in the second for the scenarios without keep-alive, and 1.5–2.9 for the keep-alive ones. Setup: one worker, a `file://` access log, a `python3 -m http.server` backend on loopback (an HTTP/1.1 variant for the keep-alive rows), `curl`, N = 20. Instruments: system calls with `intentrace -p <worker>`, ANSI codes stripped and counted on the system-call name field, cross-checked by an `LD_PRELOAD` descriptor tracer; allocations with an `LD_PRELOAD` malloc counter. Figures are per request; ranges are the two repetitions. | Scenario | System calls (intentrace) | Heap operations | Bytes requested | |---|---|---|---| | H1, one request per connection | 26.25–26.70 → **18.45–18.85** | 83.65–83.85 → **39.95–40.35** | 84 327–84 445 → **15 793–16 069** | | H1 keep-alive, 20 requests | 14.75 → **9.65** | 50.80 → **7.80** | 8 362 → **1 322** | | H1 keep-alive over TLS | 15.75 → **10.70** | 63.30 → **20.25–20.30** | 10 644–10 680 → **3 638–3 640** | | H2 over TLS, one request per connection | 61.25–61.60 → **40.15–40.75** | 412.15–413.90 → **240.60–241.75** | 137 094–137 857 → **47 559–47 963** | | H2 over TLS, 20 streams on one connection | 25.05 → **13.55** | 83.40–83.90 → **41.15–42.00** | 11 021–11 029 → **4 792–4 825** | | TCP relay (splice) | 39.00–39.10 → **36.05–36.15** | 31.45 → **14.45** | 4 068 → **3 778** | **Baseline limit.** `b5169d44` was the first release-build baseline. It already included #1552, #1553, #1557, #1558, #1561, #1565, #1568, #1578 and #1581, so their gains are not in this table; each is given, with its own figures, in its section of the document. All other per-PR figures come from the PR descriptions. UDP was not traced: its costs are read from the code and labelled as such. The TLS rows include 15 `getrandom(2)` per handshake, which comes from `crypto-ring`. That is the default provider (`bin/Cargo.toml`, `lib/Cargo.toml`, the Dockerfile, the RPM and Arch packages), so default builds pay it; a `crypto-aws-lc-rs` build issues none on this path (§3.13). Earlier reference points: the campaign's first map (2026-09-26, `LD_PRELOAD` interposer on a debug build, `stdout` access log) counted about 25.1 system calls per H1 request and 34.8 per H2 request, and undercounted because it did not interpose `fcntl` or `recv`. The 106.25 → 25.35 figure is the `tcp://` access-log pass of #1552, measured on its base `ca058989`. ## Doc/code contradictions corrected - Reusing a keep-alive backend involves no liveness probe. What exists is a replay on a fresh connection (`ReplayOnFreshBackend`). - `TcpSession::shutting_down` answers `true`: a TCP session is closed on the first soft-stop pass. It has done so since `be5dd44b` (2020-01-10). - The UDP soft stop is `UdpProxy::notify`'s `SoftStop` arm, not `shutting_down`, and it is active: `Drain`, then `close_all_flows` on every listener session, then the listener sockets are deregistered. - An H1 backend EOF is read by a later `readable` since #1606, not together with the last bytes. - `backend.pool.size` is incremented in `Mux::dial_backend`. - A new UDP flow also costs three `fcntl`, a `getsockname` and a `getpeername` in `udp_connect`, even in release builds. - The UDP simulator's `BackendResolved` action is now `ChangeBackendSet`. - The UDP LIFECYCLE described a `SelectBackend` / `BackendResolved` round trip that no longer exists; selection is done in the core. - `Router::connect` (removed) was cited instead of `Router::plan_connect`. Two code comments in `lib/src/protocol/mux/h2.rs` still name it; they are left for a later change. - An H2 backend connection belongs to one `Mux` session and carries only the streams of one frontend connection. The #1627, #1631 and #1632 entries called it shared by several clients. Another user is reachable only behind an intermediary that multiplexes users on one frontend connection. - Drifted citations: `h2.rs:1439` now pointed at an empty `///` line, and `https.rs:1065` no longer pointed at the shutdown write-up. ## Open question **TCP soft stop.** On soft stop, a TCP relay session is closed on the first pass, with whatever bytes it still has in flight and no drain of its own. The documentation states this without calling it a bug; whether it is the intended behaviour is for the maintainers to decide. ## Also reflected - #1654 (#1647): a draining H2 session waits for a header block still arriving or for a request awaiting its backend link, and a forced close is preceded by a final `GOAWAY(NO_ERROR)`. - #1655 (kawa 0.7.2): the #1651/#1653 guards are described as shipped and now defense in depth. Their `content_length_invalid` counters stay at zero unless kawa regresses. ## Validation - `python3 .github/scripts/check_doc_citations.py --self-test` → exit 0 - `python3 .github/scripts/check_doc_citations.py --root .` → exit 0 (54 line citations, 794 test names, 15 pinned blocks) - `python3 .github/scripts/check_doc_citations.py --root . --base origin/main` → exit 0 ("none of the 87 cited lines compared against f5136d6 changed their text") - `python3 .github/scripts/check_doc_citations.py --audit --root .` → exit 0. It reports 3 advisory findings, all in files this PR does not touch (`doc/configure_admin_ops.md`, `lib/src/protocol/proxy_protocol/LIFECYCLE.md`). - `cargo +nightly fmt --all -- --check` → exit 0 - `cargo build --locked` → exit 0 - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked` → exit 0 - Relative markdown links in the touched files were checked with a script: 0 broken. CI has no markdown link checker. 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.
Closes #1647
Mechanism
ConnectionH2::handle_header_statecreates the stream (ConnectionH2::create_stream) as soon as the HEADERS frame header arrives, andConnectionH2::handle_headers_frameparks the connection inH2State::ContinuationHeaderwhile END_HEADERS is clear. The stream staysStreamState::Idle.Mux::shutting_down_inner(lib/src/protocol/mux/mod.rs) kept a draining session open only forLinkedand non-quiescedUnlinkedstreams. Once the advisoryGOAWAY(NO_ERROR, 2^31-1)was flushed, it returnedtrueandServer::shut_down_sessionsclosed the session.ConnectionH2::finalize_writesends the final GOAWAY only when the wire stream table is empty, and the forced-close arm (graceful_shutdown_deadline_elapsed) returnedtruewithout sending one. So the request was lost, and the client received no GOAWAY with a reallast_stream_id(RFC 9113 §6.8).Fix
Mux::shutting_down_innerkeeps an H2 frontend open:ConnectionH2::peer_header_block_in_progressholds (ContinuationHeader/ContinuationFrame);StreamState::Link. This is the state a CONTINUATION read byMux::drive_frontend_shutdown_ioleaves, outsideMux::ready, until the nextreadypass links it. Without it, the same request could still be lost.Both waits are bounded by
h2_graceful_shutdown_deadline_seconds. The request is then served, andConnectionH2::finalize_writesends the final GOAWAY with its stream id.When the budget elapses,
ConnectionH2::goaway_before_forced_closequeues the finalGOAWAY(NO_ERROR)and it is flushed once, best effort, before the close. Visible change: this applies to every draining H2 session, withLinkedstreams included. Before, a forced close was preceded only by the advisory GOAWAY.last_stream_idexcludes a stream whose opening block never completed. When the block's stream is stillIdle, the reported id isstream_id - 2(0 for stream 1). This is safe because:create_streamaccepted has a lower id: RFC 9113 §5.1.1, enforced againstlast_stream_idinhandle_header_state;highest_peer_stream_idcan only be a refused stream.ConnectionH2::goawaynow delegates to a privategoaway_with_last_stream_id. Its other callers are unchanged.Docs:
doc/h2_mux_internals.md: shutdown section updated, and the pinned h2.rs blocks re-anchored.doc/configure.md(the knob), CHANGELOG.Red / green
New e2e tests (
e2e/src/tests/h2_tests.rs), run onca42a2eawithout the fix:With the fix:
test_h2_graceful_drain_deadline_with_a_linked_stream_sends_final_goawaycovers the general forced-close change: a 1 s budget, a streamLinkedto a backend that holds its response, then a soft-stop. Withgoaway_before_forced_closeneutralized (its call removed fromMux::shutting_down_inner, the path main takes):With the fix:
Each new e2e test was run 10 times in isolation: 10/10, 10/10 and 10/10.
The
StreamState::Linkarm has no e2e test. Reaching it needs the CONTINUATION to arrive afterepoll_waitreturns and beforeServer::shut_down_sessionsreads it in the same loop iteration. No command-channel or wire observation can place a write inside that window, so only timing could reach it, and the result would be flaky. The unit test pins that arm instead.The unit test
a_draining_h2_session_waits_for_a_stream_awaiting_its_link(lib/src/protocol/mux/mod.rs) fails when theStreamState::Linkarm is removed ("a draining H2 session must stay open for a stream awaiting its link") and passes with it.Validation (all exit 0)
cargo +nightly fmt --all -- --checkcargo build --lockedcargo build --all-features --lockedcargo clippy --all-targets --locked -- -D warningscargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warningscargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8: e2e 486 passed, lib 1317 passedpython3 .github/scripts/check_doc_citations.py --self-test,--root .,--root . --base origin/main,--audit --root .RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --lockedRebase
Rebased on
c086b456(after #1639 and #1653). LIFECYCLE.md keeps main's invariant 30, then this change's 31. The pinned h2.rs blocks indoc/h2_mux_internals.mdare re-anchored against the rebasedh2.rs. The code diff is unchanged:git patch-id --stableover everything except*.mdand CHANGELOG iscff240bd6ce5b052ea69cf5597b2a1f88d12115dboth before (459f0a2) and after the rebase.Signed-off-by: Florentin Dubois [email protected]