Skip to content

mux-h2: reaping a stream mid DATA payload nulls expect_read and wedges the connection until timeout #1597

Closed

Description

@FlorentinDUBOIS

Summary

When an H2 stream is removed while it is the target of a partially read DATA payload, expect_read is nulled without any state transition. The connection stays in H2State::Frame(header) with expect_read == None, and every later readable() pass returns at the expect_read() == None early exit without reading the socket. The connection reads nothing more (remaining payload, PING, new HEADERS) until its connection-level timeout fires. Other streams multiplexed on that connection stall with it.

Observed on origin/main at 4d104b3a0324acca3c18624a41f938d6f9070b16.

Cause

  • lib/src/protocol/mux/h2.rs:1951-2020: only a DATA frame on an existing stream sets expect_read = Some((H2StreamId::Other { gid, .. }, payload_len)), so its payload goes straight into the stream buffer.
  • lib/src/protocol/mux/h2.rs:4662-4800 (cancel_timed_out_streams, called at the start of poll_read_target and from Mux::timeout) and the reset paths call remove_dead_stream (h2.rs:3656).
  • lib/src/protocol/mux/h2_stream_table.rs:293-300 (H2StreamTable::remove) then sets expect_read = None when it names the removed gid. The state stays Frame(header) and nothing records the unread remainder of the payload.
  • lib/src/protocol/mux/h2.rs:2386-2389: every later pass takes the None branch. The only setters that later turn expect_read back into Some are the post-read dispatch and the handshake arms (h2.rs:4284, h2.rs:4293). None of them runs in Frame state, so the connection stays wedged.

A second form of the same bug hits a stream parked on buffer pressure (h2.rs:2413 removes READABLE interest). Once that stream is reaped, try_resume_reading (h2.rs:4610-4632) cannot re-enable READABLE, because it only handles expect_read == Some((Other, _)). For example, a slow backend holds a frontend upload that is parked mid-DATA until stream_idle_timeout reaps it. This second form is inferred from the code and has not been reproduced.

This is not an edge-triggered readiness loss. The reproduction below re-inserts both the READABLE event and the READABLE interest before every pass, and the connection still makes no progress.

Reproduction

Unit test in lib/src/protocol/mux/h2.rs mod tests, which uses the existing test_h2_connection loopback helper:

  1. The server connection is in H2State::Header. The peer sends HEADERS for stream 1 (END_HEADERS, no END_STREAM), then a DATA header announcing 10 bytes, followed by only 4 payload bytes.
  2. readable() runs until expect_read == Some((Other { .. }, 6)).
  3. context.now moves past stream_idle_timeout (30 s), and the peer sends the remaining 6 bytes plus a PING.
  4. readable() runs 64 times, with event and interest READABLE re-inserted before each pass.

Result: the stream is reaped (H2::IdleTimeout access log), and after that the connection never reads again:

after the reap the connection must still read the PING and queue its ACK; state=Frame(FrameHeader { payload_len: 10, frame_type: Data, flags: 0, stream_id: 1 }) expect_read=None stuck_passes=64

Fix direction (not implemented)

Fix this at the remove_dead_stream chokepoint, not at h2.rs:2387. That early return keeps readable() from spinning. When the removed gid is the current expect_read target with n > 0 bytes still unread, the connection has to consume those n bytes, for example through H2State::Discard with Some((Zero, n)), the way refuse_stream_and_discard (h2.rs:4945-4960) does. It must also credit the frame's full payload_len to connection-level flow control, as handle_data_frame's unknown-stream branch (h2.rs:5700-5716) does. Discard credits nothing today, because it exists for refused HEADERS.

The zero buffer's available space also needs a decision: refuse_stream_and_discard sends goaway(EnhanceYourCalm) when the payload does not fit (h2.rs:2256).

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