Skip to content

fix(h1): end the backend stream when a linked request is rejected - #1719

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-trailer-overflow-backend
Sep 30, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h1-trailer-overflow-backend

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1716

Problem

The H1 frontend can reject a request after its stream is linked to a backend. A chunked request whose trailer section goes over h2_max_header_fields (#1707) is one case, when the trailer section arrives after the head and body were already forwarded.

In that case, the Position::Server arms of the parse-error branch in ConnectionH1::readable (lib/src/protocol/mux/h1.rs) answer the client:

  • 400 when the response has not started;
  • a cut response when it has.

Neither arm ends the backend stream. set_default_answer and forcefully_terminate_answer (lib/src/protocol/mux/answers.rs) only mark the stream Unlinked. As a result:

  • The backend ConnectionH1 keeps the request, cut before its trailer section, and stays Connected.
  • It stays attached to the stream slot that the next keep-alive request reuses.
  • backend_streams still lists it. In debug builds, which include e2e, this panics Mux::ready_inner: backend_streams index key count mismatch: expected={}, actual={Token(5): [0]}. In release builds, the backend connection stays open until the client session closes.

#1707's split test did not catch this. Its request and the builtin 400 both carry Connection: close, so the session closed in the same pass. The bug shows once a listener 400 answer omits Connection: close, which set_default_answer explicitly supports.

Fix

Both Position::Server arms now end the backend stream first, with endpoint.end_stream(token, ...), as the Position::Client arm and the timeout paths in mod.rs already do. unlink_stream removes the stream from the reverse index.

  • H1 backend: the connection force-disconnects. It is never returned to the keep-alive pool, even when its response was already complete:
    • the 400 arm clears the response first (the 400 replaces it anyway);
    • the cut-response arm clears keep_alive_backend (that arm closes the client connection regardless).
  • H2 backend: only that stream ends. ConnectionH2::end_stream sends RST_STREAM(CANCEL) once the stream's request reached the wire, and the shared connection stays open for its other streams.

What the client sees does not change. The client connection is at most as persistent as the 400 answer's Connection header: writable keeps it only when both keep_alive_frontend and keep_alive_backend hold, so a client Connection: close, or a backend response that closes, still closes it.

This covers every frontend parse error that lands after linking, not only the trailer bound. I checked the other set_default_answer sites:

  • the pipelined parse in writable and the malformed-request check in readable run before linking;
  • the routing errors run in Link;
  • the timeout paths already call unlink_stream.

Docs: lib/src/protocol/kawa_h1/LIFECYCLE.md §2.3 and CHANGELOG.md (Unreleased, Fixed).

Test

test_h1_trailer_field_limit_exceeded_split_closes_backend (e2e/src/tests/h1_security_tests.rs) uses a listener whose 400 answer keeps the client connection alive. It sends a keep-alive chunked request in two segments:

  1. the head and body, up to 0\r\n, which the backend receives;
  2. 129 trailer fields.

It then checks that:

  • the client gets 400;
  • the backend connection closes while the client connection stays open;
  • a follow-up request on the same client connection gets a 200 through a fresh backend connection.

Red, with the fix reverted:

thread '<unnamed>' panicked at lib/src/protocol/mux/mod.rs:3322:13:
assertion `left == right` failed: backend_streams index key count mismatch: expected={}, actual={Token(5): [0]}
TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 295 bytes ending "...\r\n\r\n5\r\nHello\r\n0\r\n", client got Some("HTTP/1.1 400 Bad Request"), backend_closed=false, follow-up got ""
test result: FAILED. 0 passed; 1 failed

Green:

TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 295 bytes ending "...\r\n\r\n5\r\nHello\r\n0\r\n", client got Some("HTTP/1.1 400 Bad Request"), backend_closed=true, follow-up got "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nSozu-Id: ...\r\n\r\npong"
test result: ok. 1 passed; 0 failed

Unit test a_request_error_after_a_linked_response_started_ends_the_backend_stream (lib/src/protocol/mux/h1.rs) covers the cut-response arm. It links a pooled mock H1 backend, reads its complete response, marks it started on the client's wire, then sends a malformed chunk. It asserts:

  • the session closes;
  • the stream leaves backend_streams;
  • the backend no longer carries the stream and is Disconnecting, not pooled.

It was seen red under two mutations of that arm:

  • whole block removed: the rejected stream must leave the backend reverse index;
  • keep_alive_backend = false removed: a backend holding part of a rejected request must be disconnected, not pooled.

It is green with the arm restored.

The 400 arm's response clear (before end_stream) still has no dedicated test. It needs the backend's full response and the trailer overflow to land in the same pass, before any response byte is written.

Validation

Each check was judged by its own exit code:

Check Exit code Result
cargo +nightly fmt --all --check 0
cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings 0
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked 0
cargo test -p sozu-lib --locked 0 1424 unit tests passed after rebasing on ac2789f3; integration tests passed
cargo test -p sozu-e2e --locked h1_security_tests, 3 runs 0, 0, 0 39 passed each time
cargo test -p sozu-e2e --locked (full suite) 0 540 passed
cd fuzz && cargo +nightly fuzz build 0
check_doc_citations.py --self-test 0
check_doc_citations.py --root . 0
check_doc_citations.py --root . --base origin/main 0
check_doc_citations.py --audit --root . 0 findings only in untouched proxy_protocol/LIFECYCLE.md

cargo test -p sozu-lib runs without the splice feature. splice_readable_eof_drains_kernel_pipe_bytes_before_closing (lib/src/protocol/pipe.rs) is gated on #[cfg(all(target_os = "linux", feature = "splice"))], so it was not compiled and never ran in any of the runs above. An earlier version of this description said it passed; that was wrong.

@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/h1-trailer-overflow-backend branch 2 times, most recently from 46a19c5 to 784d453 Compare September 30, 2026 13:23
When the H1 frontend rejected a request after its stream was linked, for
example a chunked request whose trailer section goes over
h2_max_header_fields once the head and body were forwarded, the
Position::Server arms of the parse-error branch in ConnectionH1::readable
answered 400, or cut a response already started. They did not end the
backend stream.

set_default_answer and forcefully_terminate_answer only mark the stream
Unlinked. The backend ConnectionH1 therefore kept a request cut before its
trailer section, stayed attached to the stream slot the next keep-alive
request reuses, and stayed listed in backend_streams. That panicked the
debug invariant in Mux::ready_inner, and in release builds it left the
backend connection open until the client session closed.

Both arms now end the backend stream first, as the Position::Client arm
and the timeout paths already do. On an H1 backend, the connection closes
and is never returned to the keep-alive pool, even when its response was
already complete: the 400 arm clears the response before ending the
stream, and the cut-response arm drops keep_alive_backend. On an H2
backend, only the stream ends (RST_STREAM(CANCEL) once its request reached
the wire), and the shared connection stays open. The client gets the same
answer as before, and the client connection is at most as persistent as
the 400 answer's Connection header.

Closes #1716

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/h1-trailer-overflow-backend branch from 784d453 to 75bbccb Compare September 30, 2026 15:22
@FlorentinDUBOIS
FlorentinDUBOIS merged commit cd65824 into main Sep 30, 2026
51 of 52 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h1-trailer-overflow-backend branch September 30, 2026 17:11
FlorentinDUBOIS added a commit that referenced this pull request Oct 1, 2026
…nfinished request (#1748)

Follow-up to #1719 and #1726.

## Problem

`test_h1_trailer_field_limit_exceeded_split_closes_backend` fails on `main` since #1719 was squash-merged on top of #1726. After a 400 for an over-bound trailer section, the test expected a follow-up request on the same client connection to be served.

#1726 closes the H1 client connection when a response completes before its request was received whole (`request-incomplete`). An over-bound trailer section leaves the request unfinished. Its end cannot be found, so the connection cannot carry another request (RFC 9112 §6.3). The close is intended. The test expectation was wrong.

## Change

Test-side only, plus doc corrections. No runtime change.

- The test now checks that the client connection reads EOF after the 400 with no further bytes, and that a follow-up request reaches no backend. The other checks are kept: head and body forwarded, 400 answered, and the backend connection holding the cut request closed. The 400 template still omits `Connection: close`, so the close can only come from the incomplete request.
- CHANGELOG (#1716 entry), `lib/src/protocol/kawa_h1/LIFECYCLE.md` §2.3 and the 400-arm comment in `lib/src/protocol/mux/h1.rs` said the client connection stays as persistent as the answer's `Connection` header. They now say it closes.

Coverage gap: since #1726, removing the 400-arm `end_stream` block from `ConnectionH1::readable` no longer turns this test red. The client close tears the backend down as well, and no `backend_streams` panic fires. The test doc says so. The 400 arm of #1719 has no dedicated unit test. The cut-response arm keeps one.

## Test

Unmodified test on `origin/main` (`c2cad338..3d3f077`), red:

```
TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 365 bytes ending "...\r\n\r\n5\r\nHello\r\n0\r\n", client got Some("HTTP/1.1 400 Bad Request"), backend_closed=true, follow-up got ""
TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: FAIL — the next request on the client connection was not served
test result: FAILED. 0 passed; 1 failed
```

Updated test, green (3 iterations):

```
TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 365 bytes ending "...\r\n\r\n5\r\nHello\r\n0\r\n", client got Some("HTTP/1.1 400 Bad Request"), backend_closed=true, client_closed=true, after the 400 "", reaccepted=false
test result: ok. 1 passed; 0 failed
```

## Validation

| Check | Exit code | Result |
|---|---|---|
| `cargo +nightly fmt --all --check` | 0 | |
| `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` | 0 | |
| `RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps` | 0 | |
| `cargo test -p sozu-e2e --locked --features opentelemetry,splice,simd -- h1_security_tests`, 3 runs | 0, 0, 0 | 41 passed each time |
| `cargo test -p sozu-e2e ... -- test_h1_early_response_mid` (#1726) | 0 | 2 passed |
| `cargo test -p sozu-lib --all-features --locked --lib -- protocol::mux::h1` | 0 | 21 passed, including the #1719 and #1726 unit tests |
| `cd fuzz && cargo +nightly fuzz build` | 0 | |
| `check_doc_citations.py` `--self-test`, `--root .`, `--root . --base origin/main`, `--audit --root .` | 0, 0, 0, 0 | |



Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 1, 2026
#1719 made both Position::Server arms of the parse-error branch in
ConnectionH1::readable end the linked backend stream. Only the
cut-response arm had a unit test. The 400 arm relied on the e2e test
test_h1_trailer_field_limit_exceeded_split_closes_backend, which no
longer catches a regression there: since #1726 the client connection
closes after a 400 for an unfinished request, and that close tears the
backend down too.

a_request_error_answered_400_after_linking_ends_the_backend_stream links
a pooled mock H1 backend, reads its complete response without starting
it on the client's wire, then sends a malformed chunk. It asserts that
the stream leaves backend_streams, and that the backend no longer
carries it and is Disconnecting, not pooled.

Seen red with the 400-arm block removed: "the rejected stream must leave
the backend reverse index". Green with it.

CHANGELOG: the #1716 entry names the new test.

Follow-up to #1719.

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 1, 2026
Follow-up to #1719.

## Problem

#1719 made both `Position::Server` arms of the parse-error branch in `ConnectionH1::readable` (`lib/src/protocol/mux/h1.rs`) end the linked backend stream. Only the cut-response arm has a unit test. The 400 arm relied on the e2e test `test_h1_trailer_field_limit_exceeded_split_closes_backend`, which no longer catches a regression there: since #1726 the client connection closes after a 400 for an unfinished request, and that close tears the backend down too (see #1748).

## Change

Test only. `a_request_error_answered_400_after_linking_ends_the_backend_stream` links a pooled mock H1 backend and reads its complete response without starting it on the client's wire, then sends a malformed chunk. It asserts that the request is answered on the client connection, that the stream leaves `backend_streams`, and that the backend no longer carries it and is `Disconnecting`, not pooled. The CHANGELOG #1716 entry names the new test.

## Test

Red, on a `git archive` copy with the 400-arm block removed:

```
test protocol::mux::h1::tests::a_request_error_answered_400_after_linking_ends_the_backend_stream ... FAILED
panicked at lib/src/protocol/mux/h1.rs:2677:9:
the rejected stream must leave the backend reverse index
test result: FAILED. 2 passed; 1 failed
```

Green, on this branch:

```
test protocol::mux::h1::tests::a_request_error_answered_400_after_linking_ends_the_backend_stream ... ok
test result: ok. 3 passed; 0 failed
```

## Validation

| Check | Exit code | Result |
|---|---|---|
| `cargo +nightly fmt --all --check` | 0 | |
| `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` | 0 | |
| `RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps` | 0 | |
| `cargo test -p sozu-lib --all-features --locked` | 0 | 1465 unit tests passed; integration tests passed |
| `cd fuzz && cargo +nightly fuzz build` | 0 | |
| `check_doc_citations.py` `--self-test`, `--root .`, `--root . --base origin/main`, `--audit --root .` | 0, 0, 0, 0 | |


Stacked on #1739.



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.

H1 request rejected after linking leaves the backend stream attached: backend holds a cut request, index desyncs

1 participant