Skip to content

fix(h1): honour a close option in a request Connection list - #1687

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-request-connection-close
Sep 29, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-request-connection-close

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1683. The request side still 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 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") becomes has_connection_option(val, b"close"). This is the list-token matcher at editor.rs:104, the one the response side uses at editor.rs:1491. It runs on every Connection line.
  • Nothing else changes: a single close still closes, a request without the option still keeps its connection, and the forwarded Connection lines are sent as the client wrote them. A token like closed does not match.
  • H2 clients are not affected: pkawa rejects Connection as a connection-specific header before the editor sees it.

Other Connection compares checked

  • Request path: there are no other whole-value compares. Upgrades are detected from the 101 status (ConnectionH1::writable, lib/src/protocol/mux/h1.rs), not from the request header. The pkawa.rs compares check header names. The request side never reads keep-alive.
  • Not changed: lib/src/protocol/kawa_h1/answers.rs:238 matches Connection: close as 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: closing overwrites the Connection value

During shutdown (closing = true), both on_request_headers (editor.rs:1102) and on_response_headers (editor.rs:1487) replace each Connection value with close. 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 with Connection: Upgrade, so it is not in this PR.

Tests (red, then green)

  • Unit: a_close_option_in_a_request_connection_list_closes_the_client covers close, keep-alive, close, close, TE, close listed on a second Connection line (keep-alive / TE, Close ), closed (must not match), keep-alive alone, and no Connection header. It also checks that keep_alive_backend is not touched and that the Connection lines are forwarded unchanged. Before the fix the three list cases fail. The single-token cases passed before and after, and pin the unchanged behaviour.
  • e2e: test_h1_client_connection_close_option_in_a_list_closes_client (e2e/src/tests/mux_tests.rs). The client sends keep-alive, close, then close, TE, to a keep-alive backend that answers with a Content-Length. Sōzu must close the client connection after the response and release the http.active_requests charge.

RED, before the fix:

test protocol::kawa_h1::editor::tests::a_close_option_in_a_request_connection_list_closes_the_client ... FAILED
assertion `left == right` failed: keep-alive then close: keep_alive_frontend
  left: true
 right: false
H1-CLIENT-CLOSE-LIST: the client connection was not closed: still open after 5s, read so far: "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nSozu-Id: 01M3PSFTX4EFKY1G2M8KTVYTWK\r\n\r\npong"
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 503 filtered out; finished in 5.07s

GREEN, after the fix:

test protocol::kawa_h1::editor::tests::a_close_option_in_a_request_connection_list_closes_the_client ... ok
test tests::mux_tests::test_h1_client_connection_close_option_in_a_list_closes_client ... ok

Validation

  • cargo build --all-features exits 0
  • cargo clippy --all-features --all-targets -- -D warnings exits 0
  • cargo +nightly fmt --all -- --check exits 0
  • cargo test -p sozu-lib: 1349 passed; 0 failed, and every other target passes
  • cargo test -p sozu-e2e -- keep_alive close connection: 52 passed; 0 failed
  • python3 .github/scripts/check_doc_citations.py: --self-test, --root ., --base origin/main and --audit --root . all exit 0. The audit's findings are all in proxy_protocol/LIFECYCLE.md, which this PR does not touch.

Docs

  • lib/src/protocol/kawa_h1/LIFECYCLE.md §2: the on_request_headers entry now states the request-side close rule and names both tests.
  • CHANGELOG.md: new entry under Unreleased / Fixed.

`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
FlorentinDUBOIS merged commit 3ed6e8a into main Sep 29, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h1-request-connection-close branch September 29, 2026 16:07
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]>
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