Repository navigation
fix(mux-h2): send a PING or SETTINGS ACK after a half-written stream frame - #1601
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 26, 2026 22:14
…frame Both acknowledgements are serialised into `zero` and set `expect_write = Some(Zero)` unconditionally. When the socket had taken only part of a HEADERS or DATA frame, that overwrote the stream's park: the next `writable()` flushed `zero` first, and the peer read the first part of the frame, the ACK, then the rest, a corrupt frame stream. `ConnectionH2::queue_zero_output` now leaves such a park alone and marks the ACK deferred. The resume that completes the parked frame hands `expect_write` to `zero` and ends the pass, so the ACK follows the frame and precedes any other stream frame, as HAProxy appends it to the tail of its single output ring. If the stream is removed first, the next control flush takes `zero` itself; the shutdown flush does not split a parked frame either. READABLE stays withdrawn until the ACK is flushed. Closes #1600 Signed-off-by: Florentin Dubois <[email protected]>
Three paths still broke the ordering the deferred ACK relies on. - `Mux::shutting_down` forces READABLE after `flush_zero_buffer`, which leaves a deferred ACK in place while its frame is parked. The forced `readable()` then read a frame header into `zero` on top of the ACK, parsed the ACK as the peer's frame and cleared it. `poll_read_target` now reads nothing into `zero` while it holds output (`zero_holds_output`), and `handle_header_state` asserts it. - `goaway()` and `send_initial_goaway()` cleared `zero` and set `expect_write = Some(Zero)` even with a frame parked: the GOAWAY was flushed inside the frame and a queued ACK was destroyed. Both now append after queued output and park through `park_zero_output`, the deferral the ACKs use. In `H2State::GoAway` a parked frame is still written, so the deferred GOAWAY can follow it. - The orphan test asserted the bytes of a truncated frame. It now asserts only that the connection reads again. Removing a stream parked mid-frame still drops the rest of that frame; that older loss is left for a separate change. Refs #1600 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/h2-control-frame-interleave
branch
from
September 26, 2026 22:43
fb7310f to
b13175d
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 27, 2026
…1599) ## Problem When the per-stream idle reap or a reset removes a stream while the connection is reading a DATA payload into that stream's buffer, `H2StreamTable::remove` sets `expect_read` to `None` and changes nothing else. The connection stays in `H2State::Frame` with nothing to read. Every later `readable()` then returns at the `expect_read() == None` early exit, so the rest of the payload and every frame behind it stay unread until the connection timeout. Every other stream on that connection stalls with it. A stream parked on buffer pressure and then reaped ends up in the same state, and READABLE interest is removed as well. `try_resume_reading` cannot restore it, because the stream no longer exists. ## Fix `remove_dead_stream` hands the unread remainder to `skip_orphaned_data_payload`, which does three things: - moves the connection to `H2State::Discard` with `expect_read = Some((Zero, n))` and no field block, and drops the remainder as it is read, in pieces no larger than the free space in `zero`. The remainder is never buffered whole and never answered with a GOAWAY. HAProxy skips a dropped frame's payload the same way (`src/mux_h2.c:4706-4716` at `0ceb8c65`). - credits the frame's whole `payload_len` to the connection receive window (RFC 9113 §6.9), as `handle_data_frame` does for DATA on a closed stream. HAProxy does the same (`src/mux_h2.c:4152-4157`). - restores READABLE interest only while `zero_holds_output()` is false. ## Backend `end_stream` RST The skip makes a backend-side bug reachable. On a `Position::Client` connection, `end_stream` wrote its RST_STREAM(CANCEL) into `zero` without marking `zero` as holding output and without stopping reads. The next frame header was then read in behind the RST, and the backend connection took our own RST for one of the backend's frames. It answered with GOAWAY(PROTOCOL_ERROR), which killed every other multiplexed stream. The RST now goes through `queue_zero_output`, the PING and SETTINGS ACK path from #1601: - READABLE is off until `zero` is flushed. - With another stream's frame half-written, the RST is deferred behind it (`zero_output_deferred`). The `Resume` arm of #1601 then hands `expect_write` to `Zero` once that frame is complete. If the parked stream is the one being ended, `remove_dead_stream` clears the park, and #1601's "parked stream removed" control-flush path takes `zero`. - If `zero` holds bytes read from the backend, the RST goes through `enqueue_rst` instead. ## Single read guard (unified with #1601) The read guard in `poll_read_target` and the assertion in `handle_header_state` are the ones from #1601 (`zero_holds_output()`). This PR no longer adds its own. The same guard covers a stalled WINDOW_UPDATE or RST_STREAM flush, which leaves its unsent tail in `zero` under `expect_write = Zero` with READABLE still on. Slower CI runners hit that case through the assertion, and a regression test now pins it. ## Tests Each test was seen red with its part of the fix removed: - `reaping_a_stream_mid_data_payload_keeps_the_connection_reading`: red on the former main. - `reaping_a_stream_parked_on_buffer_pressure_resumes_reading` - `a_remainder_near_max_frame_size_is_skipped_in_pieces` - `a_skipped_orphaned_data_payload_is_credited_to_the_connection_window` - `end_stream_on_a_backend_mid_data_payload_sends_its_rst_before_reading_on`: without the fix the connection ends in `state=GoAway` and the backend receives nothing. With it, the backend receives exactly RST_STREAM(CANCEL), then the PING ACK. - `skipping_an_orphaned_payload_keeps_readable_off_while_zero_holds_output` - `no_frame_header_is_read_while_zero_holds_unflushed_output`: panics with the CI message when the `zero_holds_output()` guard is removed. The #1601 tests still pass. The three e2e tests that failed in CI (`test_h2_flow_control_on_orphaned_data`, `test_h2_large_payload`, `test_h2_mid_pass_rst_does_not_force_yield`) pass 10 times out of 10 under `taskset -c 0`, with no assertion hit. The full e2e run gives 449 passed. ## Docs `LIFECYCLE.md` §5.4, `doc/h2_mux_internals.md` and `CHANGELOG.md` are updated. Doc citations are re-anchored to the same line text (`check_doc_citations.py --base b03ffa2`: 215 cited lines unchanged). Closes #1597 Signed-off-by: Florentin Dubois <[email protected]>
This was referenced Sep 27, 2026
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 27, 2026
…put queue `zero` was both the frame-header input buffer and the control-frame output buffer, and stream frames were written from each stream's own `kawa.out`, serialised against `zero` only through `expect_write`. When a partial write parked a stream mid-frame and the stream was then removed (a peer RST_STREAM, `end_stream`, expiry, `prune_inactive_streams_while_closing`), the rest of that frame went with it, and the next bytes on the connection landed inside what the peer still parsed as that frame (#1604). The connection now has one ordered output queue, `H2Output` (`lib/src/protocol/mux/h2_output.rs`). It receives every control frame and the unsent rest of the one stream frame a partial write cut, each whole when queued; `zero` is input only. Stream frames stay zero-copy: a transmit gathers the queue first, then the stream's `kawa.out` as `IoSlice`s, in the same writev(2) (`h2_transmit::gather_after`). When the write stops inside a frame, `h2_transmit::frame_tail` reads the gathered frame headers and measures the rest of that frame, extended to END_HEADERS for an unfinished header block (RFC 9113 section 6.10), and `ConnectionH2::handle_write` has the queue adopt it, counted to the stream at once. A stream therefore only owns frames the wire has not started, and removing it drops whole unsent frames. The copy is bounded by one frame or header block and paid only on a partial write, which is how `h2` owns its in-flight DATA frame (`FramedWrite`'s `Encoder::next`) and HAProxy every frame in its `mbuf` ring; sozu's stream storage is a pool buffer with no reference count, so the in-flight rest is copied rather than referenced. The queue holds 128 bytes inline, so a connection that never meets backpressure allocates nothing for it. With the shared buffer gone, so are its one-off defences: `zero_output_deferred`, `queue_zero_output`/`park_zero_output`, the `zero_holds_output` read guard and `handle_header_state`'s assertion, the `ResumeZero` flush stage with its `H2ControlFlushStage` and `ControlFlushResume` walk, `expect_write = Some(Zero)`, and the reassembly guards of the WINDOW_UPDATE, RST_STREAM and shutdown flushes. The control preamble queues its frames behind the queue and flushes it once; the deferred initial GOAWAY goes out in the pass that queues it; `flush_zero_buffer` becomes `flush_output_buffer`. Reads no longer stop while an ACK waits: they stop once one buffer of output is queued, as HAProxy's demux stops on a full `mbuf`. A backend connection turns READABLE on itself after its preface, which the old `zero` resume did as a side effect. Three new tests reproduce #1604 and fail on a078550 with the peer's own parse error ("frame type 0x0 announces 65536 bytes, 4 remain", "... 0 remain", "announces 32 bytes, 28 remain"); the #1599 and #1601 tests keep their intent. Measured on release builds, 20 H2 requests one per connection and 20 multiplexed: intentrace writev 120 -> 120 and 44 -> 44, fdtrace writev 123 -> 122..124, mcount mallocs within run-to-run noise. Docs: doc/h2_mux_internals.md, lib/src/protocol/mux/LIFECYCLE.md (invariant 28, invariant 24 amended), CHANGELOG.md. Closes #1604 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 27, 2026
…put queue (#1625) Closes #1604 ## Problem `zero` was both the frame-header **input** buffer and the control-frame **output** buffer, and stream frames were written from each stream's own `kawa.out`, serialised against `zero` only through `expect_write`. When a partial write parked a stream mid-frame and the stream was then removed (peer RST_STREAM, `end_stream`, expiry, `prune_inactive_streams_while_closing`, any of the `remove_dead_stream` callers), the rest of that frame was dropped, and the next bytes on the connection — a control frame, a GOAWAY, another stream's frame — landed inside what the peer still parsed as that frame. ## Reproduced first (red on `a0785505`, then green) Three new tests in `lib/src/protocol/mux/h2.rs` parse the wire the way the peer does: | test | failure on `a0785505` | |---|---| | `a_removed_half_written_stream_still_completes_its_frame` (removal, then a PING ACK) | `left: Err("frame type 0x0 announces 65536 bytes, 4 remain")` | | `a_removed_half_written_stream_does_not_corrupt_the_next_stream_frame` (removal, then another stream's DATA) | `left: Err("frame type 0x0 announces 65536 bytes, 0 remain")` | | `a_peer_reset_of_a_half_written_stream_still_completes_its_frame` (peer RST_STREAM read while parked) | `left: Err("frame type 0x0 announces 32 bytes, 28 remain")` | The #1599 and #1601 tests on the edge cases of `zero` (deferred ACK, GOAWAY and initial GOAWAY mid-frame, forced shutdown read, backend `end_stream` RST, orphaned DATA remainders) stayed green on that head and keep their intent here. On this tree, disabling the adoption of the cut frame's rest (`self.output.adopt_tail(kawa, tail)` → `let _ = (kawa, tail);`) reddens the three #1604 tests with the same messages and every #1601 test on the framing invariant's debug assertion. ## Design One **ordered output queue** per connection, `H2Output` (`lib/src/protocol/mux/h2_output.rs`): every control frame, and the unsent rest of the one stream frame a partial write cut, each **whole** when queued. `zero` is input only. - **Stream frames stay zero-copy.** A transmit gathers the queue first, then the stream's `kawa.out` blocks as `IoSlice`s into `kawa.storage`, in the same `writev(2)` (`h2_transmit::gather_after`, `ConnectionH2::gather_transmit`). - **Commit on write.** When the write stops inside a stream frame, `h2_transmit::frame_tail` reads the gathered frame headers and measures the rest of that frame — extended through CONTINUATION up to END_HEADERS for an unfinished header block, since RFC 9113 §6.10 forbids any other frame in between — and `ConnectionH2::handle_write` has the queue adopt it (copied, consumed from the stream, counted to the stream at once). A stream therefore only owns frames the wire has not started; removing it drops whole unsent frames, never half of one. Why a bounded copy rather than references into `kawa.out` kept alive after removal: - **HAProxy** owns every committed byte in its `mbuf` ring: control frames are appended with `br_tail` (`h2c_ack_settings`, `h2c_send_ping`, `src/mux_h2.c`), and `h2s_make_data`'s zero-copy path *swaps* the whole stream buffer into the ring, falling back to `goto copy` when the swap cannot apply. - **h2 (hyperium)** keeps one `FramedWrite` buffer for control frames and frame heads and holds an in-flight DATA payload **by value** in `Encoder::next` until written (`src/codec/framed_write.rs`); `has_capacity()` is false meanwhile. Its payload is a reference-counted `Bytes`, so liveness after the stream goes away is free. - **Sōzu**'s stream storage is a pool buffer that a removed stream gives back, with no reference count. Keeping references alive would mean deferring the release of every removed stream's buffer (options (a)/(b) of #1604, which touch every removal site and the slot recycling in `Context`). Copying only the in-flight rest keeps the zero-copy path for everything else and bounds the copy to one frame or header block, paid only on a partial write. The queue holds **128 bytes inline** (a frontend's whole server preface is 79) and spills to the heap only past that, so a connection that never meets backpressure allocates nothing for its output — as the pool buffer `zero` it replaces did not. ### Review fixes (second commit) - **`frame_tail` is linear in the gathered slices.** It used to rescan from the first slice for every header byte, O(frames × slices), after every transmit; the reviewer measured ~1.9 ms per call for 2700 one-byte DATA frames (an H1 chunked body with tiny chunks). It now walks a forward-only cursor, and the shells call it only when `size < offered`. `the_walk_is_linear_in_the_slices` counts slice steps (≤ 5400 for 5400 slices; the rescan mutation gives 7292700). - **`H2Output` gives back sent heap bytes while in use.** `consume` only advanced `head`, so a queue that never drained under backpressure kept every sent byte. It now compacts once the sent prefix is past the retained capacity and larger than what is left, and shrinks a capacity over 4× the rest. `a_queue_that_never_drains_stays_bounded` (without it: head 4096000, capacity 4 MiB). - **Emptying the queue releases the read cap.** `frontend_hung_up_while_draining` cleared the queue but not `reads_wait_for_output`; `ConnectionH2::clear_output` now does both. `clearing_the_output_releases_the_read_cap`. - Stale `zero` wording in `graceful_goaway` / `send_initial_goaway` fixed. - **Corrected claim:** dropping a removed stream's unsent frames keeps the *framing* whole, not the HPACK tables — an unsent HEADERS block was already encoded and its insertions stay in the encoder table. `LIFECYCLE.md` invariant 28, `h2_output.rs` and `doc/h2_mux_internals.md` now say so. That desync predates this PR and is **#1627** (reproduced red on `771c9e90`, with a second path in `check_header_capacity`); it is not fixed here. ### Removed mechanisms `zero_output_deferred`; `queue_zero_output` / `park_zero_output`; the `zero_holds_output` read guard and `handle_header_state`'s assertion; the `ResumeZero` flush stage with `H2ControlFlushStage` and `ControlFlushResume` (the control walk now queues and flushes **once**, `H2ControlFlushTarget::FlushOutput`); `expect_write = Some(Zero)` (only `Other` is ever parked, for scheduling); the reassembly guards of the WINDOW_UPDATE, RST_STREAM and shutdown flushes. `flush_zero_buffer` → `flush_output_buffer`. `expect_write` for `Other` stays: it keeps a parked stream first in line, but no longer carries framing. ### Behaviour changes, deliberately - **READABLE no longer drops while an ACK waits.** Its only justification was the shared buffer. Reads now stop only once one buffer of output is queued (`output_read_cap`), as HAProxy's demux stops on a full `mbuf` (`H2_CF_DEM_MROOM`); `a_full_output_queue_stops_reading_until_it_drains` pins it. - **A backend H2 connection turns READABLE on after its preface** in the `(ClientSettings, Client)` arm: the old `ResumeZero` stage did it as a side effect of flushing `zero`, and a client connection starts write-only. Found while removing that stage; `a_backend_connection_reads_once_its_preface_is_out` pins it. - The deferred initial GOAWAY is sent in the pass that queues it instead of the next one. - `h2_drain`'s deferral of the initial GOAWAY during header reassembly now protects no buffer; it is kept as drain policy (documented), since removing it changes the drain, not the output. ## Tests New: the three #1604 tests; `a_cut_header_block_is_completed_before_the_ack` (cut inside HEADERS and exactly on the HEADERS/CONTINUATION boundary); `a_full_output_queue_stops_reading_until_it_drains`; `a_backend_connection_reads_once_its_preface_is_out`; `h2_output` unit tests (order, prepaid accounting, inline→heap spill and back); `h2_transmit` `frame_tail_cases` and `gather_after_offers_the_prefix_first_and_only_when_there_is_one`. Each new or rewritten test's "TO SEE THIS RED" recipe was run and its failure line recorded (2026-09-27). No test was deleted without a replacement. Rewritten, with the intent kept: - **Premises that read the old internals** now read the queue: the #1601 fixture's `!is_readable()` premise → `!output.is_empty()` (reads no longer stop); the two stalled-drain tests' and the two liveness tests' `expect_write == Some(Zero)` → queued output; the #1599 orphan tests detected the PING ACK "in `zero`" → in the queue. - **Fixtures that queued non-frame bytes** as a stream's output (`FIRST_BLOCK`/`SECOND_BLOCK`, the 256 KiB rustls body) now hold real DATA frames of the same lengths: a stream's `kawa.out` only ever holds whole H2 frames, and the write pass now reads their headers. Byte counts and the byte-equality oracle are unchanged. - `skipping_an_orphaned_payload_keeps_readable_off_while_zero_holds_output` → `skipping_an_orphaned_payload_reads_on_while_output_is_queued`, and `no_frame_header_is_read_while_zero_holds_unflushed_output` → `a_frame_header_read_leaves_queued_output_intact`: both pinned the cure for the shared buffer; the replacements pin the property that replaces it, and the second keeps the original wire assertion (WINDOW_UPDATE, then PING ACK). - `a_deferred_ack_does_not_strand_reads_when_its_parked_stream_is_removed` asserts the queue drains (the wire is asserted by the new #1604 test it used to say was out of scope). - `a_legitimate_continuation_survives_a_graceful_goaway` waits on the queue instead of `expect_write`. - `reassembly_property::qc_h2_header_reassembly_survives_interleaved_control_frame_flushes` sets `TCP_NODELAY` on its loopback peer: the interleaved WINDOW_UPDATE is now really written (it used to be deferred during reassembly), which puts sozu's end in delayed-ACK mode, and the peer's Nagle then held its next small CONTINUATION for up to 40 ms, longer than the yield-bounded read loop waits. Baseline 5/5 green, this tree 0/3 without and 3/3 with. - `sim/tests/h2_simulation.rs` mirrors the new transmit bracket and control flush. ## Measurements (release, `--no-default-features --features crypto-ring,opentelemetry,splice,simd`) 20 requests, H2 one connection per request (`h2`) and multiplexed on one connection (`h2mux`), 30-byte body, `worker_count = 1`, LOAD1 < 4. Before = `bdc74ecd` (#1620), after = this branch. | | before | after | |---|---|---| | intentrace `h2` writev / total (ANSI stripped, anchored on the syscall name) | 120 / 822 | 120 / 821 | | intentrace `h2mux` writev / total | 44 / 290 | 44 / 290 | | fdtrace `h2` writev | 132 | 120 | | mcount `h2` mallocs / 20 conns, 4 runs | 3601, 3573, 3581, 3560 | 3570, 3578, 3571, 3586 | | mcount `h2mux` mallocs, 4 runs | 842, 831, 845, 845 | 884, 823, 831, 846 | | mcount realloc_grow `h2` / `h2mux` | 1181–1214 / 251 | 1202–1217 / 248–251 | No syscall is added, and allocations are within run-to-run noise; the same held against `a0785505` before #1620 landed. fdtrace `h2mux` writev varies with pacing (44–64 across runs on both trees). A first cut stored the queue in a plain `Vec`, which measured +1.35 malloc and +0.7 realloc per connection. That is why the queue has inline storage. ## Validation (rebased on `0155d40b`, head `16d6aba8`) Build, `cargo +nightly fmt --check`, clippy `-D warnings` with `crypto-ring,opentelemetry,splice,simd`, `cargo test` for `sozu-lib` (1274), `sozu` and `sozu-command-lib`, `cargo doc -D warnings`, the four `check_doc_citations.py` runs, the sim test and clippy under `--cfg tokio_unstable`: all pass on `16d6aba8`. E2E (`-- --skip tests::fuzz_tests::`): 456 passed, 0 failed, the HPACK e2e tests included. The three #1604 tests still fail without the fix (the `adopt_tail` mutation) on this head, with the same peer parse errors. H2 e2e, 10 runs, `taskset -c 0 cargo test -p sozu-e2e -- h2 --skip tests::fuzz_tests:: --test-threads=20`. One CPU with 20 threads: without `--test-threads` the pin runs the tests serially, about 5 h per run. The same loop was run on `bdc74ecd`: | test | this branch | `bdc74ecd` | |---|---|---| | `test_h2_continuation_survives_a_graceful_drain_mid_reassembly` (a single GOAWAY, then nothing) | 5 / 10 | 9 / 10 — pre-existing, #1623 | | `test_h2_settings_ack_timeout_goaways_the_frontend` (client cannot send its preface) | 7 / 10 | 7 / 10 — pre-existing, #1624 | | `test_h2_large_chunked_7mb_drains_fully` | 1 / 10 | 0 / 10 | | `test_h1_to_h2_basic_request` | 1 / 10 | 0 / 10 | `test_h2_large_chunked_7mb_drains_fully` failed once: the client read EOF at 3.13 of 7.76 MB after 1.65 s, with no RST. That is a close, not a stall and not a frame desync; either of those would have left the client reading until its 30 s deadline. No debug assertion or panic from `lib/` appears in any log. I could not reproduce it: `h2_correctness` 5 runs on each tree, pinned to one CPU with 20 threads, 0 failures on both. It is reported here unattributed, not claimed as pre-existing. `test_h1_to_h2_basic_request` failed once in the same way (a stability iteration under load). ## Docs `doc/h2_mux_internals.md` (new "ordered output queue" section, control preamble, `flush_output_to_socket`), `lib/src/protocol/mux/LIFECYCLE.md` (new invariant 28, invariant 24 and §5 amended), `CHANGELOG.md`; module docs of `h2_drain`, `h2_header_reassembly`, `h2_control_tx`, `h2_close`. Line citations re-anchored on the same text and function. ## Not touched Rebased on `0155d40b` (#1626, which fixed #1622, on top of #1620 (the in-tree HPACK module), #1617, #1618 and #1621). The only conflict was a line citation in `doc/testing.md`; every addition #1626 made to the shared docs is kept, and `converter.rs`, `hpack_state.rs` and `hpack/` are untouched by this PR. `pkawa.rs`, `converter.rs`, `hpack_state.rs` and the HPACK codec branch in `h2.rs` are untouched. **Related, tracked separately: #1627** (HPACK table desync when an encoded HEADERS block is dropped unsent, see above). #1622 (smallest table size before the last one) was fixed separately by #1626, which this branch is rebased on. 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 #1600
Problem
handle_ping_frameand the SETTINGS ACK serialise their acknowledgement intozeroand setexpect_write = Some(Zero)unconditionally. When the socket had taken only part of a HEADERS or DATA frame, the stream was parked asexpect_write = Some(Other { .. }), and the ACK overwrote that park. The nextwritable()flushedzerofirst, then a fresh scheduler pass sent the rest of the frame. The peer read: first part of the frame, the ACK, the rest.The same family showed up in three more places:
goaway()andsend_initial_goaway()clearzeroand setexpect_write = Some(Zero)even with a frame parked: the GOAWAY was flushed inside the frame and a queued ACK was destroyed.send_initial_goawayis whatMux::shutting_downreaches throughgraceful_goaway.Mux::shutting_downforces READABLE and callsreadable()afterflush_zero_buffer. With an ACK still inzero, the read landed on top of it:handle_header_stateparsed the ACK as the peer's frame header, then cleared it.Fix
queue_zero_output(both ACKs) andpark_zero_output(both GOAWAYs) leave a stream park alone and setzero_output_deferred. TheResumearm ofpoll_write_target, once the parked frame is complete, handsexpect_writetoZeroand ends the pass, so queued control output follows the frame and precedes any other stream frame.goaway()andsend_initial_goaway()append after output already queued inzeroinstead of clearing it. InH2State::GoAwaya parked frame is still written, so the deferred GOAWAY can follow it.poll_read_targetreads nothing intozerowhile it holds output (zero_holds_output:expect_write == Some(Zero)orzero_output_deferred), whatever forced READABLE. The flush restores READABLE interest, and the event is kept for that read.handle_header_stateasserts that no output is pending.zeroitself, so the connection does not stop reading.HAProxy never hits this:
h2c_send_pingandh2c_ack_settings(src/mux_h2.c) append atbr_tail(h2c->mbuf), the tail of the single output ring that also holds stream frames, andh2_senddrains it in order. Sozu keeps control frames (zero) and stream frames (each stream's buffer) apart, so the order has to come fromexpect_write. Merging the two buffers is out of scope.Not fixed here
Removing a stream parked mid-frame (
H2StreamTable::removenulls the park; callers include peer RST_STREAM,end_stream, the idle reaper andprune_inactive_streams_while_closing) drops the rest of that frame, so the peer reads a truncated frame. This predates the change. Fixing it needs either keeping the remaining bytes (remove_dead_streamwould need the stream context at every call site), delaying the removal until the frame is out, or a poisoned-wire state that stops every writer (control flush, GOAWAYs,end_stream's RST, shutdown flush) and closes the connection. The testa_deferred_ack_does_not_strand_reads_when_its_parked_stream_is_removedtherefore asserts only that the connection reads again, not the wire bytes.Interaction with #1599
#1599 adds a
debug_assert!inhandle_header_statethat only checksexpect_write == Some(Zero). This PR adds one that coverszero_output_deferredtoo. Whichever of the two merges second must keep a single assertion, extended tozero_output_deferred, and re-runcheck_doc_citations.py --base.Tests
A test socket takes N bytes, then answers
WouldBlock, and records what it took in order. It takes 20 bytes of a 41-byte DATA frame, then the output is parsed as the peer would parse it. Red onfb7310fe, green here:a_forced_shutdown_read_does_not_eat_a_deferred_ack: only the DATA frame reaches the peer, both ACKs are lost.a_goaway_during_a_half_written_frame_follows_it_and_the_ack:Err("frame type 0x0 announces 32 bytes, 28 remain").an_initial_goaway_during_a_half_written_frame_follows_it_and_the_ack:Err("frame type 0x64 announces 6579300 bytes, 8 remain").Also:
a_ping_…anda_settings_during_a_half_written_frame_is_acknowledged_after_it(red on main),two_pings_…andsettings_then_ping_during_one_park_are_acknowledged_after_the_frame, and the orphan liveness test above. Each doc comment carries its TO SEE THIS RED recipe, all verified.Local runs on this head, rebased on
f165a3dc, each with exit 0: build (--locked --no-default-features --features crypto-ring,opentelemetry,splice,simd),cargo +nightly fmt --all -- --check, clippy-D warnings, tests forsozu-lib(1203 passed),sozu(113),sozu-command-lib(184),sozu-e2e(449),sozu-sim, the fourcheck_doc_citations.pymodes (--base f165a3dc), andRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked.doc/h2_mux_internals.md,lib/src/protocol/mux/LIFECYCLE.mdandCHANGELOG.mdare updated;h2.rsline citations are moved to the same text.Signed-off-by: Florentin Dubois [email protected]