Repository navigation
fix(h1): honour a close option in a request Connection list - #1687
Merged
Merged
Conversation
`HttpContext::on_request_headers` compared the whole `Connection` value with `close`, so a client sending `Connection: keep-alive, close` or `Connection: close, TE` kept its connection open after the response. `Connection` is a comma-separated list of case-insensitive options (RFC 9110 §7.6.1), and a `close` among them asks for the connection to close after the response (RFC 9112 §9.6). Match `close` with `has_connection_option`, the list-token matcher the response side has used since #1683, across every `Connection` line. Single-token values, requests without `close`, and the forwarded `Connection` lines are unchanged; `closed` is not the option. The request path holds no other whole-value `Connection` compare: upgrades key off the 101 status, and H2 rejects `Connection` before the editor. Covered by the unit test `a_close_option_in_a_request_connection_list_closes_the_client` and the e2e test `test_h1_client_connection_close_option_in_a_list_closes_client`, both seen red before the fix. LIFECYCLE.md and CHANGELOG.md updated. Follow-up to #1683. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 29, 2026 15:04
This was referenced Sep 29, 2026
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 29, 2026
## Problem While Sōzu shuts down (`HttpContext::closing`, set by `Mux::shutting_down_inner`), `HttpContext::on_request_headers` and `HttpContext::on_response_headers` (`lib/src/protocol/kawa_h1/editor.rs`) overwrote every `Connection` value with `close`. That dropped the other options: a field the peer nominated as hop-by-hop (`Connection: x-custom`) was forwarded as end-to-end (RFC 9110 §7.6.1), a request's `Upgrade` field reached the backend without the `upgrade` option that must accompany it (§7.8), and, on the response side, the backend's own `close` was no longer recorded in `keep_alive_backend`. An HTTP/1.1 final response without a `Connection` line got no `close` at all. ## Fix - Both header loops now always record the `close` decision from the original value (`keep_alive_frontend` / `keep_alive_backend`); nothing is rewritten inside the loop. - The HTTP/1.0 merge from #1683 moves into `merge_connection_options_into_close`: every `Connection` line is elided and one line is pushed with the other options followed by a single `close`; `keep-alive` (and an already listed `close`) is dropped. A lone `close` stays a static value (no allocation). - Request side: under `closing`, the merge runs (it replaces the old push-if-absent), so `Connection: close` is still added when the client sent none. - Response side: the merge runs for every final response under `closing` (HTTP/1.1 as well as HTTP/1.0), and as before for a non-persistent HTTP/1.0 response. A 1xx is left alone, as the HTTP/1.0 branch already did. - Docs: `lib/src/protocol/kawa_h1/LIFECYCLE.md` (both callbacks), `CHANGELOG.md` (Unreleased / Fixed). ## Decision: a request asking for `Upgrade` during shutdown Options: (a) keep `Connection: Upgrade, close` and let the backend decide; (b) refuse the upgrade. This PR takes **(b)**: the `upgrade` option is dropped along with every `Upgrade` field (a nominated field must not go end-to-end, §7.6.1). The backend then answers in HTTP/1.1, which a server may always do (§7.8: accepting an upgrade is optional), and the client gets a normal response with `Connection: close` and can retry the upgrade on a new connection. Why not (a): `ConnectionH1::writable` (`lib/src/protocol/mux/h1.rs`) checks `stream.context.closing` **before** the `101` arm, so while draining Sōzu never completes an upgrade. Under (a), a 101 would reach the client and the connection would close right after it. Refusing is the only choice consistent with the existing drain semantics (finish in-flight work, start no long-lived work). Before this PR the request already went out as `Connection: close` + `Upgrade: websocket` (see RED below), which was broken in its own way. If you would rather keep the upgrade, `writable` has to change too; please say so on this PR. ## `lib/src/protocol/kawa_h1/answers.rs` whole-value `Connection: close` match: left as is That match (in the template parser) sets the template's own `keep_alive` flag. It reads operator-authored answer templates, not peer traffic, and every shipped default template carries a lone `Connection: close` line (checked: all 15 `Connection:` lines of the built-in default templates in `answers.rs`, outside its test module, are exactly `Connection: close`), so no default answer is affected. It is the same kind of defect as #1687 (a list such as `x, close` would read as keep-alive), but it is outside this shutdown fix. It deserves its own small follow-up that exposes `has_connection_option` to `answers.rs`. ## Tests New unit tests in `editor.rs`, run with `closing = true`: - `a_shutting_down_request_keeps_its_connection_options`: no Connection → `close`; `x-custom` → `x-custom, close`; `keep-alive, x-custom` → `x-custom, close`; `close` stays a single `close` and clears `keep_alive_frontend`; two lines merged; HTTP/1.0. - `a_shutting_down_request_does_not_upgrade`: `Upgrade` → `close` with no `Upgrade:` field; `keep-alive, Upgrade, x-custom` → `x-custom, close`. - `a_shutting_down_response_keeps_its_connection_options`: the same matrix on HTTP/1.1 responses plus a persistent HTTP/1.0 one; the backend `close` clears `keep_alive_backend`. - `a_shutting_down_interim_response_is_left_alone`: a 101 keeps `Connection: Upgrade`; a 100 gets no line. **RED** (tests added on `origin/main` 3ed6e8a before the fix, `cargo test -p sozu-lib --lib a_shutting_down`): ``` test protocol::kawa_h1::editor::tests::a_shutting_down_request_does_not_upgrade ... FAILED test protocol::kawa_h1::editor::tests::a_shutting_down_request_keeps_its_connection_options ... FAILED test protocol::kawa_h1::editor::tests::a_shutting_down_interim_response_is_left_alone ... FAILED test protocol::kawa_h1::editor::tests::a_shutting_down_response_keeps_its_connection_options ... FAILED websocket: the Upgrade field is not forwarded, got "GET /chat HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\nUpgrade: websocket\r\n..." nominated field: ... left: ["Connection: close"] right: ["Connection: x-custom, close"] 101: ... left: ["Connection: close"] right: ["Connection: Upgrade"] no Connection: ... got "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nSozu-Id: ...\r\n\r\nabcd" left: [] right: ["Connection: close"] test result: FAILED. 0 passed; 4 failed; 0 ignored; 0 measured; 1349 filtered out ``` **GREEN** (`cargo test -p sozu-lib --lib kawa_h1`): ``` test protocol::kawa_h1::editor::tests::a_shutting_down_request_does_not_upgrade ... ok test protocol::kawa_h1::editor::tests::a_shutting_down_interim_response_is_left_alone ... ok test protocol::kawa_h1::editor::tests::a_shutting_down_response_keeps_its_connection_options ... ok test protocol::kawa_h1::editor::tests::a_shutting_down_request_keeps_its_connection_options ... ok test result: ok. 71 passed; 0 failed ``` ### Why there is no e2e test The request-side shutdown branch is unreachable today, and no known sequence reaches the response-side one: the fix is defensive. The e2e harness cannot reach either. `Mux::shutting_down_inner` sets `closing` only on an `Unlinked` stream, whose headers were already edited, and an idle keep-alive session is closed rather than marked (see `try_msg_close`). Measured: with an `eprintln!` probe in both merge branches, the whole `sozu-e2e` suite (fuzz skipped) got **0 probe hits** (493 passed / 7 failed; the 7 are load flakes, see below). The probe was then removed. This shows only that the harness does not reach the branches; whether production traffic can reach them (for example via a retried or H2 stream) is not established here. The unit tests are the coverage, and LIFECYCLE.md says so. ## Validation (each by its own exit code) - `cargo build --all-features` → 0 - `cargo clippy --all-features --all-targets -- -D warnings` → 0 - `cargo +nightly fmt --all -- --check` → 0 - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked` → 0 - `cargo test -p sozu-lib` → 0 (1363 passed); `-p sozu` → 0 (238); `-p sozu-command-lib` → 0 (195) - e2e: `http10` 5/5, `close_delimited` 12/12, `keep_alive` 4/4, `connection` 20/20, `upgrade` 14/14, `websocket` 4/4, `soft_stop` 2/2, all exit 0 - `check_doc_citations.py` `--self-test`, `--root .`, `--base origin/main`, `--audit --root .` → all 0 Flake triage for the 7 failures in the full probe run (host load average 43–63 from concurrent builds): `test_h1_smuggling_te_cl_trailing_tab`, `e2e_h2_flow_per_stream_idle_timeout`, `test_upgrade` and `test_upgrade_multiple_in_flight` passed on their first isolated rerun. `test_h2_graceful_shutdown_completes_large_transfer` and `test_issue_806` failed once in isolation, then passed 3 more isolated runs. `test_h1_to_h2_basic_request` (H1 client → H2 backend, `closing` never set) then went 8/8 on this branch and 8/8 on an `origin/main` worktree in interleaved runs. None of these paths enter the changed branches: the probe fired 0 times. ## Revision 2 (after review) - Rebased on `origin/main` d9ce2a0 (#1686). - MSRV clippy (1.93.1, `clippy::nonminimal_bool`) flagged the option filter in `merge_connection_options_into_close`: it is now a named `dropped` disjunction, with no `allow`. `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` → 0 (0 warning/error lines); stable clippy → 0. - Dropped the tautological shutdown `debug_assert!` in `on_response_headers`. - LIFECYCLE.md now states that the request-side shutdown branch is unreachable today (`Mux::shutting_down_inner` sets `closing` only on an `Unlinked`, non-quiesced stream whose heads were both edited) and that both branches are defensive. - Rerun: build, nightly fmt, doc `-D warnings`, `cargo test -p sozu-lib` (1372 passed), `kawa_h1` 71 passed, e2e `http10` 5/5, `connection` 20/20, `upgrade` 15/15, `websocket` 4/4, four citation checks → all 0. ## Residual risks - Textual conflict with #1685 (`feat/forwarded-headers`) in the `on_request_headers` header loop: adjacent lines only, trivial to resolve. The first PR to merge leaves a one-hunk rebase for the other. - The shutdown branches are covered by unit tests only (see above). Closes #1690 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
Follow-up to #1683. The request side still compared the whole
Connectionvalue withclose, so a client sendingConnection: keep-alive, closeorConnection: close, TEkept its connection open after the response.Connectionis a comma-separated list of case-insensitive options (RFC 9110 §7.6.1), and acloseamong them means the connection closes after the response (RFC 9112 §9.6). #1683 already matches it this way on the response side, so the two sides disagreed.Change
lib/src/protocol/kawa_h1/editor.rs:1105(HttpContext::on_request_headers):compare_no_case(val, b"close")becomeshas_connection_option(val, b"close"). This is the list-token matcher ateditor.rs:104, the one the response side uses ateditor.rs:1491. It runs on everyConnectionline.closestill closes, a request without the option still keeps its connection, and the forwardedConnectionlines are sent as the client wrote them. A token likecloseddoes not match.Connectionas a connection-specific header before the editor sees it.Other
Connectioncompares checkedConnectionH1::writable,lib/src/protocol/mux/h1.rs), not from the request header. Thepkawa.rscompares check header names. The request side never readskeep-alive.lib/src/protocol/kawa_h1/answers.rs:238matchesConnection: closeas a whole value too. It reads operator-written answer templates at configuration time, not client input, and is outside the request path. It can be fixed separately if wanted.Not fixed here:
closingoverwrites theConnectionvalueDuring shutdown (
closing = true), bothon_request_headers(editor.rs:1102) andon_response_headers(editor.rs:1487) replace eachConnectionvalue withclose. On responses this happens before the HTTP/1.0 merge runs. The replacement drops any hop-by-hop fields the line nominated (RFC 9110 §7.6.1), for HTTP/1.1 as well. This predates #1683. Fixing it correctly means recording the close first and merging the options afterwards, on both sides and for every version, with the HTTP/1.0 merge loop as the model. That changes shutdown behaviour, including for requests withConnection: Upgrade, so it is not in this PR.Tests (red, then green)
a_close_option_in_a_request_connection_list_closes_the_clientcoversclose,keep-alive, close,close, TE,closelisted on a secondConnectionline (keep-alive/TE, Close),closed(must not match),keep-alivealone, and noConnectionheader. It also checks thatkeep_alive_backendis not touched and that theConnectionlines are forwarded unchanged. Before the fix the three list cases fail. The single-token cases passed before and after, and pin the unchanged behaviour.test_h1_client_connection_close_option_in_a_list_closes_client(e2e/src/tests/mux_tests.rs). The client sendskeep-alive, close, thenclose, TE, to a keep-alive backend that answers with aContent-Length. Sōzu must close the client connection after the response and release thehttp.active_requestscharge.RED, before the fix:
GREEN, after the fix:
Validation
cargo build --all-featuresexits 0cargo clippy --all-features --all-targets -- -D warningsexits 0cargo +nightly fmt --all -- --checkexits 0cargo test -p sozu-lib:1349 passed; 0 failed, and every other target passescargo test -p sozu-e2e -- keep_alive close connection:52 passed; 0 failedpython3 .github/scripts/check_doc_citations.py:--self-test,--root .,--base origin/mainand--audit --root .all exit 0. The audit's findings are all inproxy_protocol/LIFECYCLE.md, which this PR does not touch.Docs
lib/src/protocol/kawa_h1/LIFECYCLE.md§2: theon_request_headersentry now states the request-sidecloserule and names both tests.CHANGELOG.md: new entry under Unreleased / Fixed.