Repository navigation
Description
Summary
When the H1 frontend rejects a request after its stream is linked to a backend, the backend stream is never ended. The trailer bound of #1707 is the clearest trigger. The backend connection keeps a request cut before its trailer section. It is never closed while the client connection lives, and the backend reverse index still lists the stream.
Debug builds, which include every e2e run, panic the worker on the backend_streams invariant. Release builds leave the backend connection open, still attached to the stream slot that the next request on the same client connection reuses.
Observed on origin/main at a810211.
Cause
lib/src/protocol/mux/h1.rs:487-494:HttpContext::filter_request_trailersmarks the front kawa in error when the trailer section goes overh2_max_header_fields. When the request arrives in several reads, the head and body were already forwarded, so the stream isLinked(token)and the backend holds everything up to0\r\n.lib/src/protocol/mux/h1.rs:534-538: thePosition::Serverarm answers 400 withset_default_answer. ThePosition::Server if stream.back.consumedarm ath1.rs:509-532cuts the response withforcefully_terminate_answer. Neither arm ends the backend stream. ThePosition::Clientarm just above (h1.rs:496-507) does callendpoint.end_stream(token, ...).lib/src/protocol/mux/answers.rs:326and:342: both helpers setstream.state = StreamState::UnlinkedwithoutContext::unlink_stream. The backendConnectionH1keepsstream = Some(id)andBackendStatus::Connected, andcontext.backend_streamskeepstoken -> [id].- The timeout paths in
lib/src/protocol/mux/mod.rs(for example:3437and:3544) callunlink_streambefore the same helpers. The request-error arms do not.
Impact:
- Debug and test builds: the end-of-pass check in
Mux::ready_inner(lib/src/protocol/mux/mod.rs:3322) panics the worker:
backend_streams index key count mismatch: expected={}, actual={Token(5): [0]}. - Release builds:
- The backend connection holds a half-written request and is closed only when the client session closes. The backend waits for a trailer section that never comes.
- Its
readablestill parses whatever the backend sends intocontext.streams[id].back. That is the slot the keep-alive reset (h1.rswritable) hands to the next request on the same client connection.
This is not specific to trailers. Any frontend parse error after the stream is linked takes the same arms, for example a malformed chunk-size line in a later segment.
#1707's test_h1_trailer_field_limit_exceeded_rejected_split does not see it for two reasons:
- Its request carries
Connection: close, and the builtin 400 carriesConnection: closetoo. - The session therefore returns
CloseSessionin the sameready_innerpass. It never reaches the invariant check, andMux::closetears the backend down as a side effect.
A listener 400 answer without Connection: close exposes the bug. set_default_answer explicitly supports such an answer as the operator's way to keep the client connection alive.
Reproduction
This e2e test is added in the fix PR, in e2e/src/tests/h1_security_tests.rs:
- Configure an HTTP listener whose
400answer isHTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n. - Send, keep-alive, the first segment
POST /api HTTP/1.1,Host,Transfer-Encoding: chunked,5\r\nHello\r\n0\r\n. Wait for the backend to receive it. - Send a second segment with 129 trailer fields (
h2_max_header_fields+ 1) and the final CRLF. - The client gets
HTTP/1.1 400 Bad Request. The backend connection is never closed, and the worker panics:
TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 295 bytes, client got Some("HTTP/1.1 400 Bad Request"), backend_closed=false
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]}
Expected
Once a request is rejected while its stream is linked, sozu should end the backend stream, as the Position::Client arm and the timeout paths already do:
- The backend connection is unlinked and closed. It is never returned to the keep-alive pool, even when its response was already complete, because it holds a partial request.
- The client is answered exactly as today: 400 when no response byte has left, a cut response and a close otherwise. The client connection's keep-alive stays governed by the 400 answer's
Connectionheader.
Activity
fix(h1): end the backend stream when a linked request is rejected
fix(h1): end the backend stream when a linked request is rejected
fix(h1): end the backend stream when a linked request is rejected
test(e2e): expect the client connection to close after a 400 for an u…
test(h1): cover the 400 arm that ends a linked backend stream
test(h1): cover the 400 arm that ends a linked backend stream (#1754)
fix(h1): end the backend stream when a linked request is rejected (so…