Repository navigation
refactor(mux-h2): lift writable's close-arm flush triples into the socket shell - #1514
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 24, 2026 23:39
FlorentinDUBOIS
force-pushed
the
refactor/h2-writable-flush-lift
branch
from
September 24, 2026 23:50
ccf5813 to
44c7c4e
Compare
…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
force-pushed
the
refactor/h2-writable-flush-lift
branch
from
September 24, 2026 23:53
44c7c4e to
5e87bee
Compare
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.
Issue #1339, Q10. Follows #1507, #1512 and #1511.
ConnectionH2::writableheld 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 theH2State::GoAwayarm. Both now follow the protocol #1512 established forConnectionH2::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) -> H2WritableStateTargetiswritable's(H2State, Position)match, arm for arm.Done(MuxResult)for every arm that ends the pass,Flushfor the one flush the GOAWAY arm needs,WriteStreamsfor the four proxying states. It takes noFront, noContextand noEndpoint, because no other arm reads either.ConnectionH2::dispatch_writable_state_after_flush(tls_wants_write: bool)is the GOAWAY arm'sTlsFlushPhase::AfterFlushblock, with itsCloseAction::Disconnect => {}fall-through spelled as theforce_disconnectit fell through to.ConnectionH2::writablemoves into the shell impl block, which now holdsreadable,writableandwrite_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 theH2State::GoAwayarm'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 insideflush_zero_to_socket, whichflush_pending_control_framesstill performs and whose lift is a separate step, and the one insideforce_disconnect,h2_close's third close site.Protocol / security impact
The GOAWAY arm's nested
iffused 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::Flushtherefore carries nobool. The pre-flush answer isdispatch_writable_state's parameter; the post-flush answer isdispatch_writable_state_after_flush's; in the shell each read is its ownlet, and the third shadows the second inside theFlusharm alone. There is no binding in scope that a future edit could reuse for the wrong question.H2FinalizeTargetis deliberately not reused: itsSkipFlushanswers 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 ish2_close's own argument against wideningCloseAction, 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_writeonce 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_writeis free of side effects (FrontRustlsanswers!self.peer_reset && self.session.wants_write(); every other handler takes the trait'sfalsedefault) and no fixture counts it:BackpressuredTlsSocketdeliberately 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 twoflushes.get() >= 2assertions read.Seam call-site counts:
tls_wants_write13 → 12 (six of them now in the shell block),flush_tls_records4 (unchanged),ensure_tls_flushed6 (unchanged).flush_pending_control_frameskeepsflush_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
FrontRustlsnet of #1454 and #1498 was run unchanged — seven#[test]functions, five of which build a realConnectionH2<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 REDrecipes were repaired to name the functions the arms now live in, then applied as written and re-measured:ensure_tls_flushedfromdispatch_writable_state_after_flush'sReArmAndContinuearm1116 passed; 1 failed—a_flush_that_does_not_drain_keeps_the_connection_openreturn MuxResult::Continue;after theflush_tls_records()inwritable'sH2WritableStateTarget::Flusharm1115 passed; 2 failederror_close_action(false)1116 passed; 1 failed—a_rustls_frontend_in_error_state_re_arms_until_its_records_draingoaway_close_action(AfterFlush, false, false)1115 passed; 2 failedEach failed on the assertion message its recipe names. Their three stale
1113/1114counts were measured against a 1115-test suite and are corrected.Commands run
cargo test -p sozu-libreads1117 passed; 0 failedatc5c80909and1117 passed; 0 failedhere.Docs
doc/h2_mux_internals.md's three-seams table, itswritable() entry pointsection and both shell-impl-block claims;h2_close.rs's module header and the docs ongoaway_close_action/error_close_action;LIFECYCLE.md§9 invariant 27 and itsLast revision dateblock.Two pre-existing errors are corrected in passing, both on lines this changeset had to touch anyway. The seams table said
15call sites fortls_wants_write— right at #1499, and #1512 folded two away without correcting the line. And a comment inh2.rscalled the Error arm's post-flush query Uncovered, which stopped being true when #1454 addeda_rustls_frontend_in_error_state_re_arms_until_its_records_drain.Citations
The 50
h2.rsspans this changeset shifted were remapped by content —difflib.SequenceMatcher(autojunk=False)equal-opcodes fromc5c80909, 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'sreadableandwritablesignature 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'sH2WritePhase::Resumeoutbound-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. TheConnectionH2::poll_read_targetanchor beside it already carries the same remedy.