Repository navigation
fix(http): give every keep-alive request its own request id - #1635
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 27, 2026 22:39
On an HTTP/1.1 keep-alive connection every request carried the first request's ULID: the generated X-Request-Id, Sozu-Id on the request and on the response, %REQUEST_ID in default answers and the access log's request_id were constant for the whole connection. Measured on a release build: 20 keep-alive requests through curl produced one distinct Sozu-Id, one distinct X-Request-Id at the backend and one distinct request id in the access log. doc/configure.md and kawa_h1/LIFECYCLE.md both said the id was per request. Cause: HttpContext::reset, the only per-request reset of an H1 connection's slot (called from the keep-alive branch of ConnectionH1::writable), cleared the request-scoped fields but left HttpContext::id alone, and its postcondition asserted `debug_assert_eq!(self.id, id_before, "reset() must preserve the request id")`. That assertion, and the matching `assert_eq!(ctx.id, original_id)` of test_reset_preserves_connection_state, encoded the bug: the id is request-scoped, only session_id is connection-scoped. Both are inverted: reset now asserts the id it installs differs from the previous one, and the test asserts the id changed to the one passed in. Fix: reset takes the next request's id as an argument, and the keep-alive branch mints it with Context::next_request_id, the source that already numbers H2 streams: a ULID composed from the pass's wall-clock snapshot (Context::now_wall_ms) and the per-session seeded StdRng. HttpContext owns neither a clock nor an RNG, and the mux keeps its single-clock-sample discipline. No syscall is added (no getrandom, no clock_gettime per request) and nothing allocates: Ulid is Copy and the RNG draws in place. H2 was not affected: ConnectionH2::create_stream already mints one id per stream. A client-supplied X-Request-Id is still forwarded verbatim, and Sozu-Id still carries Sōzu's own id. A retried request is not reset and keeps its id. Red before the fix, green after: - editor.rs test_reset_preserves_connection_state (assert_ne on the id) and header_editing_output_is_byte_exact_across_keep_alive_requests (which pinned one id across three keep-alive requests; it now takes each request's id and requires them distinct); - e2e test_keep_alive_rotates_request_id: three requests on one H1 connection, the third with a client X-Request-Id; before the fix the second request reused the first one's Sozu-Id. - e2e test_h2_streams_carry_distinct_request_ids is green before and after; it goes red when Context::next_request_id returns the session ULID, which also proves its two streams share one connection. Visible change: X-Request-Id (when Sōzu generates it) and Sozu-Id now change on every request of a keep-alive connection. Docs: CHANGELOG (Fixed), kawa_h1/LIFECYCLE.md point 4, mux/LIFECYCLE.md section 2.5, doc/configure.md (sozu_id_header and request-id propagation). The lines added to mux/mod.rs, mux/h1.rs, kawa_h1/editor.rs and doc/configure.md shift 27 line citations in mux/LIFECYCLE.md, kawa_h1/LIFECYCLE.md, doc/testing.md and doc/configure_admin_ops.md; each is renumbered to the line it cited. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/request-id-per-request
branch
from
September 27, 2026 23:41
e00de29 to
48dd5dd
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 28, 2026
) ## What `X-Forwarded-For`, `Forwarded`, `X-Real-IP` and `X-Forwarded-Port` depend only on the protocol, the public address and the peer address — all connection-scoped — yet every request of a keep-alive connection rendered them again. `HttpContext` now keeps a `ForwardingHop` (`lib/src/protocol/kawa_h1/editor.rs`): - rendered by the first request of the connection, shared by the next ones through `kawa::Store::Shared` (a reference-count bump, no copy); - keyed on its three inputs and rendered again by `HttpContext::forwarding_hop` whenever one differs (`reset` keeps it; a changed `session_address`, `public_address` or `protocol` invalidates it); - `X-Real-IP` shares the `X-Forwarded-For` rendering. Each rendered hop starts with `", "`: the same bytes serve an appended hop and, past the separator (the `Shared` start offset), a synthesised value. A **client-supplied** `X-Forwarded-For` or `Forwarded` chain is request-scoped and never cached: the rendered hop is appended to it in one exact-size copy (`extended_chain`), so the shared scratch buffer is gone too. The request id (per request since #1635) is untouched. ## Measurements Release, `--no-default-features --features crypto-aws-lc-rs` (glibc malloc), one curl keep-alive connection of 20 `GET`s behind a Python `http.server`, heap operations per request from an `LD_PRELOAD` malloc counter (malloc + calloc + realloc + memalign), two interleaved runs each, base = `6eef0ba1`: | Scenario | Before | After | Δ | |---|---|---|---| | H1 plaintext keep-alive | 11.45 / 11.50 | 7.65 / 7.70 | −3.80 | | H1 over TLS keep-alive | 27.55 / 27.55 | 23.75 / 23.75 | −3.80 | −3.80 = 4 per request (scratch, `X-Forwarded-For`, `Forwarded`, `X-Forwarded-Port`) on 19 of 20 requests; the first renders the hop. curl sends no forwarding header and the listener does not set `send_x_real_ip`, so the `X-Real-IP` share does not show here. Unit (`crate::test_allocations`, header editing only): | Request | Before | After | |---|---|---| | bare, first of the connection | 7 | 7 | | bare, later, `send_x_real_ip` | 8 | 3 | | bare, later | 7 | 3 | | client `X-Forwarded-For` + `Forwarded`, later | 8 | 6 | (+1 under `opentelemetry` for the synthesised `traceparent`, `SYNTHESISED_TRACEPARENT`.) **Syscalls** (intentrace, jemalloc build): the same syscall classes, and identical `getsockopt`/`epoll_ctl`/`accept4`/`close`/`connect` counts, on every pass; a plaintext pair traced to the same 210 syscalls. The residual difference between passes is the `epoll_wait`/`writev`/`recvfrom` triplet moving together with timing (base alone spans 210–222 plaintext, 216–228 TLS). No syscall added. ## Tests (seen red before the change) - `keep_alive_requests_reuse_the_connection_forwarding_hop` — red on `6eef0ba1`: `left: 8, right: 7`; mutation "rebuild the hop on every call": `left: 7, right: 3`. - `a_client_chain_costs_one_exact_copy_per_extended_header` — red on `6eef0ba1`: `left: 8, right: 6`; mutation "shared 128-byte scratch again": `left: 7, right: 6`. - `a_client_chain_never_leaks_into_the_next_request` — byte-exact, IPv6 peer and public address over HTTPS, `send_x_real_ip`, bare → client chains → bare twice across `reset`: a client chain is extended, never inherited by the next request. - `a_changed_forwarding_input_renders_the_hop_again` — byte-exact after changing the peer alone, then the public address alone, then the protocol alone (HTTP → HTTPS); mutation "drop `protocol` from the `ForwardingHop` key" goes red on the last step (stale `Forwarded: proto=http`). - `header_editing_output_is_byte_exact_across_keep_alive_requests` (existing) unchanged and green. - `a_bare_request_costs_one_scratch_and_one_copy_per_forwarding_header` now documents the first-request cost; its red recipes are updated. All pass in the `--no-default-features --features <crypto>,opentelemetry,splice,simd` cells. ## Scope and conflicts - `lib/src/protocol/mux/{answers,h1,h2}.rs`: one line each, `forwarding_hop: None,` in the `HttpContext` struct literals of their test helpers — required by the new field. - `lib/src/protocol/kawa_h1/LIFECYCLE.md`: §2 describes the hop, invariant 4 lists it as preserved by `reset`; the `editor.rs:NNN` citations of the TE guard and the pinned block in `doc/h2_mux_internals.md` are refreshed. - Independent of the sibling PR `perf(h1): capture a standard reason phrase without a copy`, which touches the same struct region, `CHANGELOG.md` and the same citations: whichever merges second needs a rebase and its `editor.rs:NNN` citations refreshed. - No dependency, no `async fn`, no syscall added. ## Commands run - `cargo build --locked`, `cargo build --all-features --locked` - `cargo clippy --all-targets --locked -- -D warnings` and with `--no-default-features --features crypto-ring,opentelemetry,splice,simd` - `cargo +nightly fmt --all -- --check` - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked` - `cargo test -p sozu-lib --lib --locked --no-default-features --features <crypto>,opentelemetry,splice,simd` for `crypto-ring`, `crypto-aws-lc-rs`, `crypto-openssl`, `fips` - `cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8` - the four `check_doc_citations.py` runs (self-test, broken fixture exits 1, `--base origin/main`, plain) 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.
Problem
On an HTTP/1.1 keep-alive connection every request carried the first request's ULID. The generated
X-Request-Id,Sozu-Idon the request and on the response,%REQUEST_IDin default answers and the access log'srequest_idstayed constant for the whole connection.doc/configure.mdandlib/src/protocol/kawa_h1/LIFECYCLE.mdboth say the id rotates per request.Cause
HttpContext::resetis the per-request reset of an H1 connection's slot, called from the keep-alive branch ofConnectionH1::writable. It cleared the request-scoped fields but leftHttpContext::iduntouched, and its postcondition asserted that behaviour:debug_assert_eq!(self.id, id_before, "reset() must preserve the request id"). The unit testtest_reset_preserves_connection_statepinned it too, withassert_eq!(ctx.id, original_id). Both assertions encoded the bug, because the id is request-scoped. Onlysession_idis connection-scoped. They are now inverted.H2 was not affected:
ConnectionH2::create_streamalready mints one id per stream throughContext::next_request_id.Fix
HttpContext::reset(&mut self, request_id: Ulid)installs the next request's id. It asserts that the new id differs from the previous one and that it is the one it was given.Context::next_request_id, the source that already numbers H2 streams. It builds a ULID from the pass's wall-clock snapshot (Context::now_wall_ms) and the per-session seededStdRng.HttpContextowns no clock and no RNG, and the mux keeps its one-clock-sample-per-pass discipline.X-Request-Idis still forwarded verbatim, andSozu-Idstill carries Sōzu's own id. This is unchanged and matchesdoc/configure.md. A retried request is never reset, so it keeps its id.Visible change:
X-Request-Id(when Sōzu generates it) andSozu-Idnow change on every request of a keep-alive connection.Red → green
editor.rstest_reset_preserves_connection_state(nowassert_ne!on the id)assertion left != right failed, sameUlidon both sideseditor.rsheader_editing_output_is_byte_exact_across_keep_alive_requests(it pinned one id across 3 keep-alive requests; it now requires them distinct)request 1 of the connection reuses the id of an earlier one: 01M3JB2VCVQPFGT815KEV120AEtest_keep_alive_rotates_request_id(3 requests on one H1 connection, the third with a clientX-Request-Id)request 1: Sozu-Id 01M3JB4R8FM1YQG3NNDZPX813E was already used on this connectiontest_h2_streams_carry_distinct_request_ids(2 streams on one H2 connection)The H2 test was seen red through sabotage: with
Context::next_request_idreturningself.session_ulid, it printsSozu-Id per stream: [Some("01M3JC2EGD43T1RQ0VP929RTJE"), Some("01M3JC2EGD43T1RQ0VP929RTJE")]and fails. This also shows that its two streams share one connection.Measurements
Release binaries built on a tree carrying #1628 (then unmerged, based on
826a59ae), before and after the fix. The fix's diff is unchanged by the later rebase ontomain(c974237c, #1628 merged). The setup is 1 worker, an HTTP listener, and a Python HTTP/1.1 keep-alive backend. A warm-up connection comes first. Thenintentrace -c -p <worker>traces onecurlinvocation that sends 20 GETs on one connection. Two runs each:Sozu-IdSozu-IdX-Request-Id == Sozu-Idepoll_wait/writev/recvfrom/getsockoptgetrandom/clock_gettimeThe OpenTelemetry
traceparentis not derived from the request id:random_id()draws it per request. It is unaffected, andcargo test -p sozu-lib --lib --features opentelemetry -- kawa_h1::editorpasses (39 tests).The syscall set is identical. The ±1 differences are event-loop wakeup noise. No
getrandomappears, because the RNG is seeded once per session inContext::new. Noclock_gettimeappears either: the wall-clock snapshot is the oneMuxalready takes per pass, through the vDSO. Minting the id allocates nothing, becauseUlidisCopyandStdRngdraws in place. The allocation-count tests of the header editor (a_response_shares_the_request_id_renderingand the keep-alive byte-exact test) stay green.Commands run
cargo build --locked,cargo 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 +nightly fmt --all -- --checkcargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8check_doc_citations.py:--self-test, the broken fixture tree (exit 1 with a failure report), a plain run, and--base origin/mainDocs
CHANGELOG.md(Fixed)lib/src/protocol/kawa_h1/LIFECYCLE.mdpoint 4lib/src/protocol/mux/LIFECYCLE.md§2.5doc/configure.md: thesozu_id_headersection, plus the request-ID propagation section, which now states the priority of a clientX-Request-IdSigned-off-by: Florentin Dubois [email protected]