Repository navigation
Description
Summary
On a backend H2 connection (Position::Client), ConnectionH2::end_stream (lib/src/protocol/mux/h2.rs) queues RST_STREAM(CANCEL) for any stream that is not fully completed, without checking whether the stream's request HEADERS ever reached the backend. A stream id is allocated and registered by ConnectionH2::start_stream as soon as the router links the request, but HEADERS are only written by a later write pass in H2State::Header. When the frontend gives up in between (client RST_STREAM, client disconnect, frontend/stream timeout), the backend receives a frame for a stream it has never seen, and depending on the connection state the whole shared backend connection is lost:
Backend connection state when end_stream runs |
What the backend receives | Effect |
|---|---|---|
H2State::ClientPreface (TCP connect in progress) |
RST_STREAM(1) before PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n |
invalid connection preface (RFC 9113 §3.4) → connection error |
H2State::ServerSettings (preface sent, server SETTINGS not yet read) |
RST_STREAM(1), then sozu hits Unexpected combination: (Writable, ServerSettings, Client) and force_disconnects |
sozu kills the connection itself |
H2State::Header, stream not yet written (socket stall on an earlier parked stream, or the stream linked but not yet prepared) |
RST_STREAM(N) for an idle stream |
RFC 9113 §5.1 / §6.4: connection error PROTOCOL_ERROR |
The connection is shared: Router::connect (lib/src/protocol/mux/router.rs) multiplexes streams of different clients on one H2 backend connection, and explicitly reuses a BackendStatus::Connecting one when no connected one exists. One client cancelling a request during the backend handshake window therefore tears down the connection carrying other clients' requests. Browsers cancel requests routinely (navigation, aborted fetches), so this does not need a malicious client; a malicious one can time HEADERS then RST_STREAM to hit the window on purpose.
Mechanism
ConnectionH2::start_streamallocates the wire id (new_stream_id),registers it in the stream table and arms WRITABLE. Nothing is written yet.ConnectionH2::end_stream,Position::Clientarm:fully_completed = back_received_end_of_stream && front.is_terminated(); when false and the id is not inrst_sent, it pushesRST_STREAM(CANCEL)intoself.outputand callsqueue_output, which arms WRITABLE. It never looks at whether the request left:stream.front.consumedis exactly "at least one request byte was written to this backend" on this position.- Preface ordering: the
(H2State::ClientPreface, Position::Client(..))arm ofConnectionH2::dispatch_writable_stateappendsH2_PRIand SETTINGS toself.outputbehind whatever is already queued, so a RST queued before the first writable goes out first. ServerSettings: WRITABLE interest is removed in that state;end_streamre-arms it, and the(H2State::ServerSettings, Position::Client(..))pattern ofdispatch_writable_stateanswersforce_disconnect().ConnectionH2::cancel_timed_out_streams(enqueue_rst) has the same shape for a backend stream reaped before its HEADERS left.
Reproduction (unit level, on fedb4304; end_stream, start_stream and dispatch_writable_state are unchanged since 58550dd4)
Using the fixture of end_stream_on_a_backend_mid_data_payload_sends_its_rst_before_reading_on (lib/src/protocol/mux/h2.rs): a Position::Client connection, connection.start_stream(gid, &mut context) (answers true, stream 1), then immediately connection.end_stream(gid, &mut context), then drive writable() and read the loopback peer.
state = H2State::Header: the peer reads exactly00 00 04 03 00 00 00 00 01 00 00 00 08—RST_STREAM(1, CANCEL)and no HEADERS for stream 1.state = H2State::ClientPreface: the peer reads00 00 04 03 00 00 00 00 01 00 00 00 08 50 52 49 20 2a ...— the RST, then the preface and SETTINGS.state = H2State::ServerSettings, interestREADABLE | HUP | ERRORonly:end_streamalone arms WRITABLE; the peer reads the RST, sozu logsUnexpected combination: (Writable, ServerSettings, Client("c", Connected))and ends inH2State::Error.
Peer behaviour
- RFC 9113 §5.1 (idle): "Receiving any frame other than HEADERS or PRIORITY on a stream in this state MUST be treated as a connection error of type PROTOCOL_ERROR." §6.4: "If a RST_STREAM frame identifying an idle stream is received, the recipient MUST treat this as a connection error of type PROTOCOL_ERROR." §3.4: the client preface must be the first bytes.
- hyperium/h2 (
Streams::recv_reset,src/proto/streams/streams.rs): an unknown id goes throughActions::ensure_not_idle→Recv::ensure_not_idlereturnsReason::PROTOCOL_ERRORforid >= next_stream_id, mapped toError::library_go_away: GOAWAY(PROTOCOL_ERROR), connection closed. - HAProxy (
h2_frame_check_vs_state,src/mux_h2.c): a frame other than HEADERS/PRIORITY on anH2_SS_IDLEstream →h2c_error(h2c, H2_ERR_PROTOCOL_ERROR)and a glitch report.
Expected
A backend stream whose request never left sozu is simply forgotten: no frame for it at all. The id is burnt, which is harmless — the next HEADERS with a higher id implicitly closes it (RFC 9113 §5.1.1). This is what the frontend already does for Position::Server, where nothing is sent before the peer opened the stream. Both ConnectionH2::end_stream and ConnectionH2::cancel_timed_out_streams need the guard; stream.front.consumed (or an equivalent "HEADERS written" bit in the stream table) is the natural discriminator. Two related points to settle in the same change:
- A stream whose HEADERS were encoded but are still parked unsent in
kawa.out(the h2: HPACK encoder table diverges when an encoded HEADERS block is dropped unsent #1627 situation onPosition::Client) is in the same "never reached the backend" state and must not get a RST either; since fix(mux-h2): reset the HPACK encoder table when an encoded block is dropped unsent #1630 the HPACK side of dropping that block is handled by the encoder table reset. - No stream-level frame may be queued into
outputahead of the connection preface, whatever produced it.
Activity
fix(mux-h2): reset the HPACK encoder table when an encoded block is d…
fix(mux-h2): never reset a backend stream whose HEADERS have not left
FlorentinDUBOIS commentedon Sep 28, 2026
Scope correction:
Router, and with it every backend connection inRouter::backends, belongs to a singleMuxsession (fieldMux::router,lib/src/protocol/mux/mod.rs). An H2 backend connection is therefore shared only by the streams of one frontend connection, never between distinct client TCP connections. Any cross-user impact requires an intermediary (a CDN or load balancer, for example) that multiplexes several users over one frontend connection to sozu.For this issue: the backend connection error still takes down every other stream of the same session that is carried by that backend connection. With a browser that is the same user's parallel requests; behind an intermediary it can be other users' requests.
fix(mux-h2): never reset a backend stream whose HEADERS have not left
fix(mux): end a Content-Length response truncated by a backend close …
fix(mux-h2): never reset a backend stream whose HEADERS have not left
fix(mux-h2): give back the send credit of DATA prepared and dropped u…
fix(mux-h2): never reset a backend stream whose HEADERS have not left
fix(mux-h2): never reset a backend stream whose HEADERS have not left (…
docs: document the hot-path zero-copy and syscall campaign and refres…
docs: document the hot-path zero-copy and syscall campaign and refres…
docs: document the hot-path zero-copy and syscall campaign and refres…
docs(mux): name Router::plan_connect in the two ServerSettings commen…