Skip to content

fix(http): give every keep-alive request its own request id - #1635

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/request-id-per-request
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/request-id-per-request

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Problem

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 stayed constant for the whole connection. doc/configure.md and lib/src/protocol/kawa_h1/LIFECYCLE.md both say the id rotates per request.

Cause

HttpContext::reset is the per-request reset of an H1 connection's slot, called from the keep-alive branch of ConnectionH1::writable. It cleared the request-scoped fields but left HttpContext::id untouched, and its postcondition asserted that behaviour: debug_assert_eq!(self.id, id_before, "reset() must preserve the request id"). The unit test test_reset_preserves_connection_state pinned it too, with assert_eq!(ctx.id, original_id). Both assertions encoded the bug, because the id is request-scoped. Only session_id is connection-scoped. They are now inverted.

H2 was not affected: ConnectionH2::create_stream already mints one id per stream through Context::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.
  • The keep-alive branch mints that id with 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 seeded StdRng. HttpContext owns no clock and no RNG, and the mux keeps its one-clock-sample-per-pass discipline.
  • A client-supplied X-Request-Id is still forwarded verbatim, and Sozu-Id still carries Sōzu's own id. This is unchanged and matches doc/configure.md. A retried request is never reset, so it keeps its id.

Visible change: X-Request-Id (when Sōzu generates it) and Sozu-Id now change on every request of a keep-alive connection.

Red → green

Test Before fix After fix
editor.rs test_reset_preserves_connection_state (now assert_ne! on the id) FAILED: assertion left != right failed, same Ulid on both sides ok
editor.rs header_editing_output_is_byte_exact_across_keep_alive_requests (it pinned one id across 3 keep-alive requests; it now requires them distinct) FAILED: request 1 of the connection reuses the id of an earlier one: 01M3JB2VCVQPFGT815KEV120AE ok
e2e test_keep_alive_rotates_request_id (3 requests on one H1 connection, the third with a client X-Request-Id) FAILED: request 1: Sozu-Id 01M3JB4R8FM1YQG3NNDZPX813E was already used on this connection ok
e2e test_h2_streams_carry_distinct_request_ids (2 streams on one H2 connection) ok (H2 was not affected) ok

The H2 test was seen red through sabotage: with Context::next_request_id returning self.session_ulid, it prints Sozu-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 onto main (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. Then intentrace -c -p <worker> traces one curl invocation that sends 20 GETs on one connection. Two runs each:

before-1 before-2 after-1 after-2
distinct response Sozu-Id 1/20 1/20 20/20 20/20
distinct backend Sozu-Id 1/20 1/20 20/20 20/20
distinct access-log request id 1/20 1/20 20/20 20/20
backend X-Request-Id == Sozu-Id 20/20 20/20 20/20 20/20
epoll_wait / writev / recvfrom / getsockopt 66/60/60/40 66/60/60/40 66/59/59/40 67/60/60/40
getrandom / clock_gettime 0 / 0 0 / 0 0 / 0 0 / 0

The OpenTelemetry traceparent is not derived from the request id: random_id() draws it per request. It is unaffected, and cargo test -p sozu-lib --lib --features opentelemetry -- kawa_h1::editor passes (39 tests).

The syscall set is identical. The ±1 differences are event-loop wakeup noise. No getrandom appears, because the RNG is seeded once per session in Context::new. No clock_gettime appears either: the wall-clock snapshot is the one Mux already takes per pass, through the vDSO. Minting the id allocates nothing, because Ulid is Copy and StdRng draws in place. The allocation-count tests of the header editor (a_response_shares_the_request_id_rendering and the keep-alive byte-exact test) stay green.

Commands run

  • cargo build --locked, cargo build --all-features --locked
  • cargo clippy --all-targets --locked -- -D warnings
  • cargo clippy --all-targets --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd -- -D warnings
  • cargo +nightly fmt --all -- --check
  • cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8
  • check_doc_citations.py: --self-test, the broken fixture tree (exit 1 with a failure report), a plain run, and --base origin/main

Docs

  • CHANGELOG.md (Fixed)
  • lib/src/protocol/kawa_h1/LIFECYCLE.md point 4
  • lib/src/protocol/mux/LIFECYCLE.md §2.5
  • doc/configure.md: the sozu_id_header section, plus the request-ID propagation section, which now states the priority of a client X-Request-Id
  • Line citations shifted by the added lines are renumbered.

Signed-off-by: Florentin Dubois [email protected]

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
FlorentinDUBOIS force-pushed the fix/request-id-per-request branch from e00de29 to 48dd5dd Compare September 27, 2026 23:41
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 6eef0ba into main Sep 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/request-id-per-request branch September 28, 2026 00:39
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]>
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.

1 participant