Skip to content

h2: backend RST_STREAM for a stream whose HEADERS never left kills the shared backend connection #1631

Closed

Description

@FlorentinDUBOIS

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_stream allocates the wire id (new_stream_id), registers it in the stream table and arms WRITABLE. Nothing is written yet.
  • ConnectionH2::end_stream, Position::Client arm: fully_completed = back_received_end_of_stream && front.is_terminated(); when false and the id is not in rst_sent, it pushes RST_STREAM(CANCEL) into self.output and calls queue_output, which arms WRITABLE. It never looks at whether the request left: stream.front.consumed is exactly "at least one request byte was written to this backend" on this position.
  • Preface ordering: the (H2State::ClientPreface, Position::Client(..)) arm of ConnectionH2::dispatch_writable_state appends H2_PRI and SETTINGS to self.output behind whatever is already queued, so a RST queued before the first writable goes out first.
  • ServerSettings: WRITABLE interest is removed in that state; end_stream re-arms it, and the (H2State::ServerSettings, Position::Client(..)) pattern of dispatch_writable_state answers force_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 exactly 00 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 reads 00 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, interest READABLE | HUP | ERROR only: end_stream alone arms WRITABLE; the peer reads the RST, sozu logs Unexpected combination: (Writable, ServerSettings, Client("c", Connected)) and ends in H2State::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 through Actions::ensure_not_idle → Recv::ensure_not_idle returns Reason::PROTOCOL_ERROR for id >= next_stream_id, mapped to Error::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 an H2_SS_IDLE stream → 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:

Refs: #1627, #1630 (which calls this out as out of scope).

Activity

  1. FlorentinDUBOIS commented on Sep 28, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    Scope correction: Router, and with it every backend connection in Router::backends, belongs to a single Mux session (field Mux::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.

  2. added 5 commits that reference this issue on Sep 28, 2026
    0ecbe60
    96b4bca
    b16ccab
    a3d3dd4
    de2df9f
  3. added a commit that references this issue on Sep 28, 2026
    cea53d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions