Repository navigation
fix(h1): end the backend stream when a linked request is rejected - #1719
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 30, 2026 11:38
FlorentinDUBOIS
force-pushed
the
fix/h1-trailer-overflow-backend
branch
2 times, most recently
from
September 30, 2026 13:23
46a19c5 to
784d453
Compare
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
force-pushed
the
fix/h1-trailer-overflow-backend
branch
from
September 30, 2026 15:22
784d453 to
75bbccb
Compare
This was referenced Oct 1, 2026
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]>
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.
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::Serverarms of the parse-error branch inConnectionH1::readable(lib/src/protocol/mux/h1.rs) answer the client:Neither arm ends the backend stream.
set_default_answerandforcefully_terminate_answer(lib/src/protocol/mux/answers.rs) only mark the streamUnlinked. As a result:ConnectionH1keeps the request, cut before its trailer section, and staysConnected.backend_streamsstill lists it. In debug builds, which include e2e, this panicsMux::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 omitsConnection: close, whichset_default_answerexplicitly supports.Fix
Both
Position::Serverarms now end the backend stream first, withendpoint.end_stream(token, ...), as thePosition::Clientarm and the timeout paths inmod.rsalready do.unlink_streamremoves the stream from the reverse index.keep_alive_backend(that arm closes the client connection regardless).ConnectionH2::end_streamsendsRST_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
Connectionheader:writablekeeps it only when bothkeep_alive_frontendandkeep_alive_backendhold, so a clientConnection: 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_answersites:writableand the malformed-request check inreadablerun before linking;Link;unlink_stream.Docs:
lib/src/protocol/kawa_h1/LIFECYCLE.md§2.3 andCHANGELOG.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:0\r\n, which the backend receives;It then checks that:
Red, with the fix reverted:
Green:
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:backend_streams;Disconnecting, not pooled.It was seen red under two mutations of that arm:
the rejected stream must leave the backend reverse index;keep_alive_backend = falseremoved: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:
cargo +nightly fmt --all --checkcargo +1.93.1 clippy --all-features --all-targets --locked -- -D warningsRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --lockedcargo test -p sozu-lib --lockedac2789f3; integration tests passedcargo test -p sozu-e2e --locked h1_security_tests, 3 runscargo test -p sozu-e2e --locked(full suite)cd fuzz && cargo +nightly fuzz buildcheck_doc_citations.py --self-testcheck_doc_citations.py --root .check_doc_citations.py --root . --base origin/maincheck_doc_citations.py --audit --root .proxy_protocol/LIFECYCLE.mdcargo test -p sozu-libruns without thesplicefeature.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.