Skip to content

test(h2): cover the vectored write loop's progress-under-WouldBlock branch - #1472

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
test/h2-backpressure-coverage
Sep 23, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
test/h2-backpressure-coverage

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closes the test gap #1454 names, ahead of the write_streams inversion that gap exists to constrain.

The branch in one paragraph

update_readiness (lib/src/protocol/mux/mod.rs:380-404) classifies a write pass as stalled iff size == 0. The status argument only clears the event bit. So socket_write_vectored returning size > 0 together with SocketResult::WouldBlock is not a stall, and flush_stream_out's while !kawa.out.is_empty() loop must go round again. The natural reading of the code — "a non-Continue status means stop" — is the wrong one, and a write machine built on it sends responses short. Nothing in the suite exercised that branch. Now five tests do.

Test-only. Zero production lines change: the #[cfg(test)] mod tests boundary opens at line 6332 on main and every hunk here lands at 7291+. sozu-lib goes 1078 → 1083.

Why extend BackpressuredTlsSocket rather than add a second handler

(size > 0, WouldBlock) is a TLS artefact — FrontRustls is the only handler that produces it, and it does so structurally: socket_write_vectored accumulates buffered_size from plaintext rustls accepted into session.writer(), while can_write goes false when write_tls blocks against the kernel (lib/src/socket.rs:934-1134). The two quantities are independent. A plain mio::net::TcpStream answers (0, WouldBlock) and can never produce the pair.

So the script belongs on the handler that already models rustls, one layer below the record count it already models. An empty script — the default — delegates exactly as before, which is why the three tests that predate it are untouched and unaffected. Scripted sizes are clamped to the gather's offer, so no production debug_assert! can stand in for a test assertion.

h2_transmit's qc_partial_writes_preserve_the_byte_stream looks like prior coverage and is not: it drives partial writes through drive, a loop in its own test module that calls neither flush_stream_out nor update_readiness_after_write, over a WritePlan carrying an accept count and no SocketResult at all. A mirror cannot constrain what it mirrors, and that plan cannot express the pair.

Each test names the production edit that reddens it

production edit result failing assertion
pass-terminator on status at h2.rs:2752 1082 / 1 a_partial_write_reporting_would_block_continues_the_pass, left: 5, right: 43
update_readiness's else arm → false 1082 / 1 a_write_that_moves_no_bytes_stalls_the_pass, got Drained
false for tls_wants_write at h2.rs:5842 1082 / 1 force_disconnect_re_arms_while_tls_records_are_pending, got CloseSession
true for tls_wants_write at h2.rs:5842 1081 / 2 force_disconnect_closes_when_no_tls_records_are_pending and the pre-existing a_flush_that_succeeds_closes_within_one_writable_call
delete self.readiness.interest = … at h2.rs:5854 1082 / 1 force_disconnect_re_arms…, left: Hup, right: Writable | Error | Hup
delete self.ensure_tls_flushed(); at h2.rs:5855 1082 / 1 force_disconnect_re_arms…, event bit
drop the bytes_written accumulation at h2.rs:2746 1081 / 2 a_fully_accepted_write… left: 0, right: 43 + a_partial_write…

Every failure above is a test's own assert!; no production debug_assert! ever pre-empted one.

The true row is why the two force_disconnect tests are not a symmetric pair. h2_close::force_disconnect_action is exhaustively tabled over its two booleans, but nothing called ConnectionH2::force_disconnect, so the query feeding the table and the wiring of ReArmAndContinue to interest, flush and return value had no coverage. Against false the new test is the catch. Against true it is not — the pre-existing test already catches that, through writable()'s GoAway fall-through — and the new test's value is the direct path to ConnectionH2::force_disconnect, where a red names the query under test instead of arriving through a write pass and a state machine.

What this branch does not buy — stated so the inversion is reviewed against it

flush_stream_out has one producer of FlushOutcome::Stalled (h2.rs:2753) and exactly two consumers: h2.rs:2266, the resume path's return MuxResult::Continue, and h2.rs:2597, the main loop's set_expect_write(Some(..)); break 'outer. Both are reached by zero tests, before and after this branch. A panic! planted at h2.rs:2753 gives 1082 passed; 1 failed, the sole reacher being a_write_that_moves_no_bytes_stalls_the_pass — which calls the associated function directly. No test enters flush_stream_out with debug_site = 2 at all.

These five tests pin the inside of the flush loop. The write_streams inversion lifts the round-again decision out of it, so the riskiest edits it makes are in code no test observes. Three shapes that would survive this suite:

  1. flush_stream_out keeps its signature but stops being the loop — all five tests stay green against a vestigial function.
  2. A pass terminator keyed on status rather than outcome at h2.rs:2597's neighbourhood. Combined with h2_close::finalize_action suppressing the finalize flush once socket_wants_write() is true (h2.rs:2897-2900), the pass can end with Ready::WRITABLE withdrawn and no external trigger — a hang, not a truncation.
  3. total_bytes_written mis-accumulation at h2.rs:2595, which feeds finalize_write's bytes_written_this_pass and selects invariant-16 RetainPendingBack over Quiesce.

The new response_stream_with_out_blocks helper builds a stream carrying response bytes; installing it in a Context and driving it through writable() is the step that makes sites 2266 and 2597 reachable. It is deliberately not in this branch — different fixture, different failure mode, and holding this one open for it delays the work it exists to constrain. Recorded on #1454 so the inversion is reviewed against a known blind spot rather than an assumed-covered one.

Refs #1454

…ranch

`update_readiness` (`mux/mod.rs`) classifies a write pass as stalled **iff
`size == 0`** — `status` only clears the event bit. So a
`socket_write_vectored` returning `size > 0` together with
`SocketResult::WouldBlock` is NOT a stall, and `flush_stream_out`'s
`while !kawa.out.is_empty()` loop must go round again. A write machine that
treated `status != Continue` as a pass terminator would drop that second
write and send the response short.

Nothing exercised that branch against production code. `FrontRustls` is the
only handler that returns the pair at all, and it does so structurally:
`socket_write_vectored` accumulates `buffered_size` from
`session.writer().write(..)` — plaintext rustls accepted — while `can_write`
goes false when `write_tls` blocks against the kernel. The two quantities are
independent. A plain `mio::net::TcpStream` answers `(0, WouldBlock)` and never
that shape, and `BackpressuredTlsSocket::socket_write_vectored` delegated
straight to one. `h2_transmit`'s `qc_partial_writes_preserve_the_byte_stream`
does drive partial writes, but through `drive`, a loop written in its own test
module that calls neither `flush_stream_out` nor `update_readiness_after_write`
and whose `WritePlan` carries an accept count and no `SocketResult` at all — a
mirror cannot constrain what it mirrors, and that plan cannot express the pair.

Extends `BackpressuredTlsSocket` with an optional `(size, status)` script for
`socket_write_vectored` rather than adding a second fault-injecting handler:
the pair is a TLS artefact, so it belongs on the handler that already models
rustls, one layer below the record count it already models. An empty script —
the default — delegates as before, so the three tests that predate it are
unaffected. Scripted sizes are clamped to the gather's offer so no production
`debug_assert!` can stand in for a test assertion.

Adds five tests:

- `a_partial_write_reporting_would_block_continues_the_pass` — the branch
  above, driven through the production `flush_stream_out`. Against the
  pass-terminator mutation it is the ONLY failure in the 1083-test `sozu-lib`
  suite, on its own assertion (`left: 5, right: 43`).
- `a_fully_accepted_write_drains_in_one_round` and
  `a_write_that_moves_no_bytes_stalls_the_pass` — the two arms that were
  already covered, kept as standing controls. Both pass against the correct
  loop and against that mutation, which is what shows the suite's existing
  shape proves nothing about this vector.
- `force_disconnect_re_arms_while_tls_records_are_pending` and
  `force_disconnect_closes_when_no_tls_records_are_pending` — the site #1454
  names. `h2_close::force_disconnect_action` is exhaustively tabled over its
  two booleans, but nothing called `ConnectionH2::force_disconnect`, so the
  query that feeds the table and the wiring of `ReArmAndContinue` to interest,
  flush and return value had no coverage. Hard-wiring `false` at the query
  site reddens exactly one test, the first of these two. Hard-wiring `true`
  reddens two: the second of these and the pre-existing
  `a_flush_that_succeeds_closes_within_one_writable_call`, which reaches the
  same call through `writable()`'s GoAway fall-through. So the pair's value
  against `true` is the direct path to `ConnectionH2::force_disconnect`, not
  the catch itself; against `false` it is the catch.

Test-only; no production line changes. `sozu-lib` goes from 1078 to 1083.

Refs #1454

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 1e414ed into main Sep 23, 2026
46 of 47 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Sep 23, 2026
`flush_stream_out` has one producer of `FlushOutcome::Stalled` and exactly
two consumers — the resume path's `return MuxResult::Continue`, and the main
loop's `set_expect_write(Some(H2StreamId::Other { .. })); break 'outer;` —
and no test reached either. Measured on 38fe585 (whose tree 1e414ed
squash-merged unchanged) with a `panic!` planted at BOTH consumers at once:
`1083 passed; 0 failed`. A `panic!` on the `return FlushOutcome::Stalled`
they both read gives `1082 passed; 1 failed`, the sole reacher being
`a_write_that_moves_no_bytes_stalls_the_pass`, which calls the associated
function directly and never enters `write_streams`. A `panic!` on the first
line of the resume block also leaves `1083 passed; 0 failed`: nothing called
`flush_stream_out` with `debug_site = 2`.

The gap was never the socket — `with_vectored_script` already scripts any
`(size, status)` pair, including ones no kernel returns. It was registering a
stream that carries response bytes, so the scheduler reaches it through
`writable()`. Production is untouched: every hunk lands inside `#[cfg(test)]
mod tests`, and `md5sum` over h2.rs lines 1..6331 — up to the `#[cfg(test)]`
boundary — is d14838f4f1f0d20b3f40336d3f5a2ca4 on this commit and on
1e414ed alike.

Six tests, each with a measured reddening edit. Counts below are against this
commit's 1089.

- `a_partial_write_reporting_would_block_continues_the_pass_through_writable`
  pins that a write which moved bytes under `WouldBlock` does not end the
  pass, driven through `writable()` rather than by calling the associated
  function. Adding `|| !matches!(status, SocketResult::Continue)` to
  `flush_stream_out`'s stall test gives `1086 passed; 3 failed`, this test
  failing on its own first assertion with `left: 5, right: 43`.

- `a_stalled_stream_parks_expect_write_for_the_next_pass` reaches the main
  loop's stall consumer. Deleting its `set_expect_write` gives `1088 passed;
  1 failed`, with `left: None, right: Some(Other { id: 1, gid: 0 })`.

- `a_stalled_resume_does_not_begin_a_scheduler_pass` reaches the resume
  path's stall consumer. Deleting its `return MuxResult::Continue` gives
  `1088 passed; 1 failed`, with `left: None, right: Some(Other { id: 1, gid:
  0 })`.

- `resume_path_bytes_do_not_make_the_pass_progress` pins that resume-path
  bytes are not pass progress, so `finalize_write` quiesces instead of
  retaining WRITABLE under LIFECYCLE §9 invariant 16. Hoisting `resume_bytes`
  out of the resume block and seeding `total_bytes_written` from it gives
  `1088 passed; 1 failed`, with `left: Writable | Error | Hup, right: Error |
  Hup`.

- `a_yielded_incremental_stream_is_prepared_once_per_pass` pins that one pass
  debits the connection send window once per stream, even when that stream's
  flush goes round twice. Visiting each stream twice within a pass
  (`order.iter().chain(order.iter())`) gives `1088 passed; 1 failed`, with
  `left: 65535, right: 32768`.

- `a_later_zero_byte_stream_does_not_erase_the_pass_progress` pins the
  pass-level accumulation `total_bytes_written =
  total_bytes_written.saturating_add(stream_bytes)`, which nothing covered.
  Replacing it with `total_bytes_written = stream_bytes;` leaves the whole
  suite green WITHOUT this test — measured `1088 passed; 0 failed`, the other
  five included — and gives `1088 passed; 1 failed` with it, on its own first
  assertion, `left: Error | Hup, right: Writable | Error | Hup`. The failure
  it hides is a hung response: stream 1 delivers a frame and keeps blocks
  queued, stream 3 is window-blocked and writes nothing, the total reads 0,
  `finalize_action` skips `RetainPendingBack` and falls through to `Quiesce`,
  `Ready::WRITABLE` is stripped, and edge-triggered epoll never re-fires.

The send-window test's shape is measured rather than assumed, and the
measurement corrects the obvious way to write it. A narrow per-stream window
cannot see a double prepare: with a 64-byte window over a 200-byte body and a
two-round flush, production and a literal double-visit edit both give
`delta=64 vectored_calls=2 blocks_left=1 bout=73`. `*parts.window =
parts.window.saturating_sub(consumed)` zeroes the stream's credit, the second
prepare computes `min(0, connection_window) == 0`, the converter's `window >
0` gate returns the chunk untouched, and `consumed = 0 - 0 = 0`.

That makes the second prepare invisible to those observables, NOT harmless.
Two things do move, measured on the same fixture: the converter's
flow-control-stall arm fires its unconditional
`incr!(names::h2::FLOW_CONTROL_STALL)` — proven with a `panic!` on that
`incr!`, which the fixture does not reach under production and does reach
under the double visit — and would also fire
`incr!(names::backend::FLOW_CONTROL_PAUSED)` on `Position::Client`; and
`context.debug.push(DebugEvent::S(..))`, which sits outside the prepare
gate and so fires per visit, measured 1 -> 2 while the SocketIO count stays
2. Those counters are dashboard signals for a real RFC 9113 §6.9 condition,
so a re-entrant write loop would inflate them with stalls that never reached
the wire. The bytes are idempotent; the telemetry is not.

The bytes are idempotent by four independent mechanisms, only one of which is
the window, and the comment now says so: the `consumed` double debit;
`H2Scheduler::note_fired`'s own `consumed > 0` gate; the `rst_sent` HashSet,
which is load-bearing because `H2BlockConverter::initialize` pushes a
RST_STREAM on every prepare of an errored kawa with no window gate at all;
and `mem::take` of the response-header edits.

The RFC 9218 §4 incremental yield is the one path that stops a prepare with
BOTH window and blocks remaining, which is why that test registers a
same-urgency incremental pair over a body wider than
`SETTINGS_MAX_FRAME_SIZE`. Its name says what it pins: a round-again-keyed
edit is not expressible in `write_streams`, because `flush_stream_out` drains
`kawa.out` completely or stalls and the rounds are invisible above it.

What the existing fixture cannot see:
`a_partial_write_reporting_would_block_continues_the_pass` drives the same
two-round flush but calls `flush_stream_out` directly — no `Context`, no
scheduler, no converter, no `H2FlowControl` — so it never runs a prepare at
all, and no amount of re-preparing is representable in it.

No absolute suite count is embedded in the new comments, and the parent
commit's two were rewritten the same way: a whole-crate pass count is
falsified by a test added anywhere in `sozu-lib`, and nothing checks it —
`check_doc_citations.py` reads only `doc/` and `**/LIFECYCLE.md`. The failing
test names and the `left:`/`right:` values survive; the totals did not. Both
of the parent's relative claims were re-verified and still hold: forcing
`force_disconnect_action`'s second argument to `true` fails
`force_disconnect_closes_when_no_tls_records_are_pending` and
`a_flush_that_succeeds_closes_within_one_writable_call`; forcing it to
`false` fails only `force_disconnect_re_arms_while_tls_records_are_pending`.

The suite goes from 1083 to 1089 tests.

Refs #1454
Refs #1472

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Sep 23, 2026
`flush_stream_out` has one producer of `FlushOutcome::Stalled` and exactly
two consumers — the resume path's `return MuxResult::Continue`, and the main
loop's `set_expect_write(Some(H2StreamId::Other { .. })); break 'outer;` —
and no test reached either. Measured on 38fe585 (whose tree 1e414ed
squash-merged unchanged) with a `panic!` planted at BOTH consumers at once:
`1083 passed; 0 failed`. A `panic!` on the `return FlushOutcome::Stalled`
they both read gives `1082 passed; 1 failed`, the sole reacher being
`a_write_that_moves_no_bytes_stalls_the_pass`, which calls the associated
function directly and never enters `write_streams`. A `panic!` on the first
line of the resume block also leaves `1083 passed; 0 failed`: nothing called
`flush_stream_out` with `debug_site = 2`.

The gap was never the socket — `with_vectored_script` already scripts any
`(size, status)` pair, including ones no kernel returns. It was registering a
stream that carries response bytes, so the scheduler reaches it through
`writable()`. Production is untouched: every hunk lands inside `#[cfg(test)]
mod tests`, and `md5sum` over h2.rs lines 1..6331 — up to the `#[cfg(test)]`
boundary — is d14838f4f1f0d20b3f40336d3f5a2ca4 on this commit and on
1e414ed alike.

Six tests, each with a measured reddening edit. Counts below are against this
commit's 1089.

- `a_partial_write_reporting_would_block_continues_the_pass_through_writable`
  pins that a write which moved bytes under `WouldBlock` does not end the
  pass, driven through `writable()` rather than by calling the associated
  function. Adding `|| !matches!(status, SocketResult::Continue)` to
  `flush_stream_out`'s stall test gives `1086 passed; 3 failed`, this test
  failing on its own first assertion with `left: 5, right: 43`.

- `a_stalled_stream_parks_expect_write_for_the_next_pass` reaches the main
  loop's stall consumer. Deleting its `set_expect_write` gives `1088 passed;
  1 failed`, with `left: None, right: Some(Other { id: 1, gid: 0 })`.

- `a_stalled_resume_does_not_begin_a_scheduler_pass` reaches the resume
  path's stall consumer. Deleting its `return MuxResult::Continue` gives
  `1088 passed; 1 failed`, with `left: None, right: Some(Other { id: 1, gid:
  0 })`.

- `resume_path_bytes_do_not_make_the_pass_progress` pins that resume-path
  bytes are not pass progress, so `finalize_write` quiesces instead of
  retaining WRITABLE under LIFECYCLE §9 invariant 16. Hoisting `resume_bytes`
  out of the resume block and seeding `total_bytes_written` from it gives
  `1088 passed; 1 failed`, with `left: Writable | Error | Hup, right: Error |
  Hup`.

- `a_yielded_incremental_stream_is_prepared_once_per_pass` pins that one pass
  debits the connection send window once per stream, even when that stream's
  flush goes round twice. Visiting each stream twice within a pass
  (`order.iter().chain(order.iter())`) gives `1088 passed; 1 failed`, with
  `left: 65535, right: 32768`.

- `a_later_zero_byte_stream_does_not_erase_the_pass_progress` pins the
  pass-level accumulation `total_bytes_written =
  total_bytes_written.saturating_add(stream_bytes)`, which nothing covered.
  Replacing it with `total_bytes_written = stream_bytes;` leaves the whole
  suite green WITHOUT this test — measured `1088 passed; 0 failed`, the other
  five included — and gives `1088 passed; 1 failed` with it, on its own first
  assertion, `left: Error | Hup, right: Writable | Error | Hup`. The failure
  it hides is a hung response: stream 1 delivers a frame and keeps blocks
  queued, stream 3 is window-blocked and writes nothing, the total reads 0,
  `finalize_action` skips `RetainPendingBack` and falls through to `Quiesce`,
  `Ready::WRITABLE` is stripped, and edge-triggered epoll never re-fires.

The send-window test's shape is measured rather than assumed, and the
measurement corrects the obvious way to write it. A narrow per-stream window
cannot see a double prepare: with a 64-byte window over a 200-byte body and a
two-round flush, production and a literal double-visit edit both give
`delta=64 vectored_calls=2 blocks_left=1 bout=73`. `*parts.window =
parts.window.saturating_sub(consumed)` zeroes the stream's credit, the second
prepare computes `min(0, connection_window) == 0`, the converter's `window >
0` gate returns the chunk untouched, and `consumed = 0 - 0 = 0`.

That makes the second prepare invisible to those observables, NOT harmless.
Two things do move, measured on the same fixture: the converter's
flow-control-stall arm fires its unconditional
`incr!(names::h2::FLOW_CONTROL_STALL)` — proven with a `panic!` on that
`incr!`, which the fixture does not reach under production and does reach
under the double visit — and would also fire
`incr!(names::backend::FLOW_CONTROL_PAUSED)` on `Position::Client`; and
`context.debug.push(DebugEvent::S(..))`, which sits outside the prepare
gate and so fires per visit, measured 1 -> 2 while the SocketIO count stays
2. Those counters are dashboard signals for a real RFC 9113 §6.9 condition,
so a re-entrant write loop would inflate them with stalls that never reached
the wire. The bytes are idempotent; the telemetry is not.

The bytes are idempotent by four independent mechanisms, only one of which is
the window, and the comment now says so: the `consumed` double debit;
`H2Scheduler::note_fired`'s own `consumed > 0` gate; the `rst_sent` HashSet,
which is load-bearing because `H2BlockConverter::initialize` pushes a
RST_STREAM on every prepare of an errored kawa with no window gate at all;
and `mem::take` of the response-header edits.

The RFC 9218 §4 incremental yield is the one path that stops a prepare with
BOTH window and blocks remaining, which is why that test registers a
same-urgency incremental pair over a body wider than
`SETTINGS_MAX_FRAME_SIZE`. Its name says what it pins: a round-again-keyed
edit is not expressible in `write_streams`, because `flush_stream_out` drains
`kawa.out` completely or stalls and the rounds are invisible above it.

What the existing fixture cannot see:
`a_partial_write_reporting_would_block_continues_the_pass` drives the same
two-round flush but calls `flush_stream_out` directly — no `Context`, no
scheduler, no converter, no `H2FlowControl` — so it never runs a prepare at
all, and no amount of re-preparing is representable in it.

No absolute suite count is embedded in the new comments, and the parent
commit's two were rewritten the same way: a whole-crate pass count is
falsified by a test added anywhere in `sozu-lib`, and nothing checks it —
`check_doc_citations.py` reads only `doc/` and `**/LIFECYCLE.md`. The failing
test names and the `left:`/`right:` values survive; the totals did not. Both
of the parent's relative claims were re-verified and still hold: forcing
`force_disconnect_action`'s second argument to `true` fails
`force_disconnect_closes_when_no_tls_records_are_pending` and
`a_flush_that_succeeds_closes_within_one_writable_call`; forcing it to
`false` fails only `force_disconnect_re_arms_while_tls_records_are_pending`.

The suite goes from 1083 to 1089 tests.

Refs #1454
Refs #1472

Signed-off-by: Florentin Dubois <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant