Skip to content

refactor(mux-h2): lift writable's close-arm flush triples into the socket shell - #1514

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-writable-flush-lift
Sep 25, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-writable-flush-lift

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Issue #1339, Q10. Follows #1507, #1512 and #1511.

ConnectionH2::writable held the last two query / flush / query triples inside the H2 core: an unconditional preamble flush whose post-flush answer the (H2State::Error, Position::Server) arm read, and a second, complete triple inside the H2State::GoAway arm. Both now follow the protocol #1512 established for ConnectionH2::finalize_write — the core answers a step, the shell performs it, and the core is told what the socket then said.

What moved

  • ConnectionH2::dispatch_writable_state(tls_wants_write: bool) -> H2WritableStateTarget is writable's (H2State, Position) match, arm for arm. Done(MuxResult) for every arm that ends the pass, Flush for the one flush the GOAWAY arm needs, WriteStreams for the four proxying states. It takes no Front, no Context and no Endpoint, because no other arm reads either.
  • ConnectionH2::dispatch_writable_state_after_flush(tls_wants_write: bool) is the GOAWAY arm's TlsFlushPhase::AfterFlush block, with its CloseAction::Disconnect => {} fall-through spelled as the force_disconnect it fell through to.
  • ConnectionH2::writable moves into the shell impl block, which now holds readable, writable and write_streams. It performs every TLS seam call its own body used to make inline: the preamble flush and the two queries around it, the stalled-drain re-arm query, and the H2State::GoAway arm's flush and post-flush query. Two seam reads reached on the same pass stay outside it and are named so the claim is exact — the one inside flush_zero_to_socket, which flush_pending_control_frames still performs and whose lift is a separate step, and the one inside force_disconnect, h2_close's third close site.

Protocol / security impact

The GOAWAY arm's nested if fused two questions — "does rustls hold buffered records" and "did the kernel accept the flush" — read off two calls to the same query. Its own comment calls conflating them the primary truncation vector under HAProxy chaining: a proxy that closes a TLS connection with records pending sends FIN, destroys them, and the peer reads a truncated response it cannot distinguish from an attack.

H2WritableStateTarget::Flush therefore carries no bool. The pre-flush answer is dispatch_writable_state's parameter; the post-flush answer is dispatch_writable_state_after_flush's; in the shell each read is its own let, and the third shadows the second inside the Flush arm alone. There is no binding in scope that a future edit could reuse for the wrong question.

H2FinalizeTarget is deliberately not reused: its SkipFlush answers a question no close arm asks — nothing writes to the socket between the dispatch and its flush, so no vectored write can have attempted it as a side effect — and sharing the enum would force a named-impossible arm into the shell's match. That is h2_close's own argument against widening CloseAction, applied the other way round.

Behaviour

Behaviour-preserving. Nothing on the wire, in a route, a metric, a configuration key or a CLI flag moves.

One ordering detail is real and unobservable rather than absent: the Error and GOAWAY arms used to read tls_wants_write once each, and the shell now reads it once before the dispatch — so the query happens for the arms that never consulted it, and its value is discarded there. SocketHandler::socket_wants_write is free of side effects (FrontRustls answers !self.peer_reset && self.session.wants_write(); every other handler takes the trait's false default) and no fixture counts it: BackpressuredTlsSocket deliberately models the record state rather than scripting an answer sequence, "so a positional script would pin the query COUNT instead of the behaviour". The flush count per pass is unchanged — at most one in the preamble, at most one for the GOAWAY arm — which is what its two flushes.get() >= 2 assertions read.

Seam call-site counts: tls_wants_write 13 → 12 (six of them now in the shell block), flush_tls_records 4 (unchanged), ensure_tls_flushed 6 (unchanged). flush_pending_control_frames keeps flush_zero_to_socket: a byte mover on the control-frame path is not a TLS seam, and lifting it is a different step.

Tests

The FrontRustls net of #1454 and #1498 was run unchanged — seven #[test] functions, five of which build a real ConnectionH2<FrontRustls> over a handshaken TLS 1.3 session on a loopback socket with pinned kernel queues. All pass without an edit.

Four TO SEE THIS RED recipes were repaired to name the functions the arms now live in, then applied as written and re-measured:

mutation result
delete ensure_tls_flushed from dispatch_writable_state_after_flush's ReArmAndContinue arm 1116 passed; 1 failed — a_flush_that_does_not_drain_keeps_the_connection_open
return MuxResult::Continue; after the flush_tls_records() in writable's H2WritableStateTarget::Flush arm 1115 passed; 2 failed
error_close_action(false) 1116 passed; 1 failed — a_rustls_frontend_in_error_state_re_arms_until_its_records_drain
goaway_close_action(AfterFlush, false, false) 1115 passed; 2 failed

Each failed on the assertion message its recipe names. Their three stale 1113/1114 counts were measured against a 1115-test suite and are corrected.

Commands run

cargo build                                                                     0
cargo +nightly fmt --all -- --check                                             0
cargo clippy --all-targets --locked --no-default-features \
  --features crypto-ring,opentelemetry,splice,simd -- -D warnings               0
cargo test -p sozu-lib --locked <same flags>                                    0   1117 passed; 0 failed
cargo test -p sozu     --locked <same flags>                                    0   113 passed; 0 failed
cargo test -p sozu-e2e --locked <same flags> -- --skip tests::fuzz_tests::      0   447 passed; 0 failed
RUSTFLAGS="--cfg tokio_unstable" cargo test -p sozu-sim --locked                0   30 passed; 0 failed
RUSTFLAGS="--cfg tokio_unstable" cargo clippy -p sozu-sim --all-targets \
  --locked -- -D warnings                                                       0
python3 .github/scripts/check_doc_citations.py --root .                         0
python3 .github/scripts/check_doc_citations.py --base c5c80909ba4b              0
python3 .github/scripts/check_doc_citations.py --audit --root .                 0
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked          0
cargo bench -p sozu-lib --bench h2_stream_table -- --test                       0

cargo test -p sozu-lib reads 1117 passed; 0 failed at c5c80909 and 1117 passed; 0 failed here.

Docs

doc/h2_mux_internals.md's three-seams table, its writable() entry point section and both shell-impl-block claims; h2_close.rs's module header and the docs on goaway_close_action / error_close_action; LIFECYCLE.md §9 invariant 27 and its Last revision date block.

Two pre-existing errors are corrected in passing, both on lines this changeset had to touch anyway. The seams table said 15 call sites for tls_wants_write — right at #1499, and #1512 folded two away without correcting the line. And a comment in h2.rs called the Error arm's post-flush query Uncovered, which stopped being true when #1454 added a_rustls_frontend_in_error_state_re_arms_until_its_records_drain.

Citations

The 50 h2.rs spans this changeset shifted were remapped by content — difflib.SequenceMatcher(autojunk=False) equal-opcodes from c5c80909, never a global offset, never keyed on a basename — and each was then audited on two keys: byte-identical line text, and an identically-named enclosing item. Zero mismatches over 74 compared endpoints.

doc/h2_mux_internals.md's readable and writable signature blocks are the spans content could not map: a 3 348-line move is a delete plus an insert, not an equal opcode. Both were re-anchored by hand and their quoted lines verified byte for byte against the source.

LIFECYCLE.md's H2WritePhase::Resume outbound-byte refresh dropped its number for the symbol instead, because the insertions moved it onto a line number another span already carried at the base revision — the reused-number class #1447 documents, which the drift rule compares against whatever the base put there. The ConnectionH2::poll_read_target anchor beside it already carries the same remedy.

…cket shell

`ConnectionH2::writable` held the last two query / flush / query triples inside
the H2 core: an unconditional preamble flush whose post-flush answer the
`(H2State::Error, Position::Server)` arm read, and a second, complete triple
inside the `H2State::GoAway` arm. Both now follow the protocol #1512 established
for `ConnectionH2::finalize_write` -- the core answers a step, the shell performs
it, and the core is told what the socket then said. Issue #1339, Q10.

`ConnectionH2::dispatch_writable_state(tls_wants_write: bool)` is `writable`'s
`(H2State, Position)` match, arm for arm, and it answers
`H2WritableStateTarget`: `Done(MuxResult)` for every arm that ends the pass,
`Flush` for the one flush the GOAWAY arm needs, `WriteStreams` for the four
proxying states. It takes no `Front`, no `Context` and no `Endpoint`, because no
other arm reads either. `ConnectionH2::dispatch_writable_state_after_flush` is
the GOAWAY arm's `TlsFlushPhase::AfterFlush` block with its
`CloseAction::Disconnect => {}` fall-through spelled as the `force_disconnect`
it fell through to.

The two fused questions are two parameters of two functions. The GOAWAY arm's
nested `if` asked "does rustls hold buffered records" and "did the kernel accept
the flush" off two calls to the same query, and its own comment calls conflating
them the primary truncation vector under HAProxy chaining: a proxy that closes a
TLS connection with records pending sends FIN, destroys them, and the peer reads
a truncated response it cannot distinguish from an attack. `Flush` therefore
carries no `bool`. The pre-flush answer is `dispatch_writable_state`'s
parameter, the post-flush answer is `dispatch_writable_state_after_flush`'s, and
in the shell each read is its own `let` -- the third shadows the second inside
the `Flush` arm alone. `H2FinalizeTarget` is not reused: its `SkipFlush` answers
a question no close arm asks, since nothing writes to the socket between the
dispatch and its flush, and sharing it would force a named-impossible arm into
the shell's match -- `h2_close`'s own argument against widening `CloseAction`.

`writable` moves into the shell impl block, which now holds `readable`,
`writable` and `write_streams`. It performs every TLS seam call its own body
used to make inline: the preamble flush and the two queries around it, the
stalled-drain re-arm query, and the GOAWAY arm's flush and post-flush query.
Two seam reads reached on the same pass stay outside it, and are named so the
claim is exact -- the one inside `flush_zero_to_socket`, which
`flush_pending_control_frames` still performs, and the one inside
`force_disconnect`, `h2_close`'s third close site.
`tls_wants_write`'s production call sites fall from 13 to 12, six of them in
that block; `flush_tls_records` stays at 4 and `ensure_tls_flushed` at 6.
`flush_pending_control_frames` keeps `flush_zero_to_socket`: a byte mover on the
control-frame path is not a TLS seam, and lifting it is a different step.

Behaviour-preserving. `cargo test -p sozu-lib` reads `1117 passed; 0 failed` at
c5c8090 and `1117 passed; 0 failed` here; `sozu` 113, `sozu-e2e` 447 and
`sozu-sim` 30 all pass, and nothing on the wire, in a route, a metric, a
configuration key or a CLI flag moves. One ordering detail is real and
unobservable rather than absent: the Error and GOAWAY arms used to read
`tls_wants_write` once each, and the shell now reads it once before the
dispatch, so the query happens for the arms that never consulted it and its
value is discarded there. `SocketHandler::socket_wants_write` is free of side
effects and no fixture counts it -- `BackpressuredTlsSocket` models the record
state rather than scripting an answer sequence, deliberately so that no
assertion pins the query count -- and the flush count per pass is unchanged,
which is what its two `flushes.get() >= 2` assertions read.

The net was run unchanged. The seven `FrontRustls` tests of #1454 and #1498,
five of which build a real `ConnectionH2<FrontRustls>`, pass without an edit.
Four `TO SEE THIS RED` recipes were repaired to name the functions the arms now
live in, then applied as written and re-measured: deleting `ensure_tls_flushed`
from `dispatch_writable_state_after_flush` gives `1116 passed; 1 failed`
(`a_flush_that_does_not_drain_keeps_the_connection_open`); returning `Continue`
after the `flush_tls_records()` in `writable`'s `H2WritableStateTarget::Flush`
arm gives `1115 passed; 2 failed`; `error_close_action(false)` gives
`1116 passed; 1 failed`
(`a_rustls_frontend_in_error_state_re_arms_until_its_records_drain`); pinning
`goaway_close_action(AfterFlush, false, ...)`'s third argument to `false` gives
`1115 passed; 2 failed`. Each failed on the assertion message its recipe names.
Their three stale `1113`/`1114` counts were measured against a 1115-test suite
and are corrected.

Docs move in the same changeset: `doc/h2_mux_internals.md`'s three-seams table,
its `writable() entry point` section and both shell-impl-block claims,
`h2_close.rs`'s module header and the docs on `goaway_close_action` and
`error_close_action`, and LIFECYCLE.md's invariant 27. The seams table said 15
call sites for `tls_wants_write`: right at #1499, and #1512 folded two away
without correcting the line. One comment in `h2.rs` called the Error arm's
post-flush query Uncovered; that was already wrong before this changeset renamed
what it points at, since #1454's rustls Error test drives it.

The 50 `h2.rs` spans this changeset shifted were remapped by CONTENT --
`difflib.SequenceMatcher(autojunk=False)` equal-opcodes from c5c8090, never a
global offset, never keyed on a basename -- and each was then audited on two
keys: byte-identical line text, and an identically-named enclosing item. Zero
mismatches over 74 compared endpoints. `doc/h2_mux_internals.md`'s `writable`
signature block is the one span content could not map, because a 3 348-line move
is a delete plus an insert and not an equal opcode; it was re-anchored by hand
and its five quoted lines verified byte for byte. LIFECYCLE.md's
`H2WritePhase::Resume` outbound-byte refresh dropped its number for the symbol
instead, because the insertions moved it onto a line number another span already
carried at the base revision, which the drift rule compares against whatever the
base put there (#1447).

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the refactor/h2-writable-flush-lift branch from 44c7c4e to 5e87bee Compare September 24, 2026 23:53
@FlorentinDUBOIS
FlorentinDUBOIS merged commit b7a34e0 into main Sep 25, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the refactor/h2-writable-flush-lift branch September 25, 2026 01:41
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