Skip to content

refactor(mux-h2): hand ensure_tls_flushed the TLS answer instead of asking - #1507

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-tls-query-injection
Sep 24, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-tls-query-injection

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

ConnectionH2::ensure_tls_flushed read self.tls_wants_write() itself, so each of its eight call sites paid a socket query the composite could not see was already answered. It now takes tls_wants_write: bool.

Five callers already held the answer. For four, the re-arm is the second half of a decision h2_close had just taken on that very value — finalize_action's AfterFlush phase, error_close_action, goaway_close_action's AfterFlush phase, and force_disconnect, which already bound it above its match. The fifth, initiate_close_notify, reaches no h2_close decision at all: its branch condition is the answer. Nothing mutates the socket in between at any of the five, so the second query could only repeat the first.

The three stalled-drain tails of flush_pending_control_frames keep a query, now spelled at the call site rather than borrowed from the helper: they run after the flush_zero_to_socket whose stall brought them there, and that write is exactly what changes the answer, so a value bound before the call would be the wrong one. The helper's doc records which callers are in which group and why.

What this removes is five runtime re-queries, not source occurrences. Production self.tls_wants_write() occurrences go 13 → 15: the helper's own read is deleted, and the three stalled-drain tails each spell their own. The saving is at runtime, on the five paths where the answer was already in hand.

Scope

This is one helper of the three the socket-boundary sequencing originally grouped here, and the narrowing is deliberate rather than partial work:

  • ConnectionH2::has_pending_write keeps its own query and moves to the step where the shell owns the socket. It is pub, Connection dispatches it through forward!, and that macro's H1 arm is socket_wants_write() — parameterising it means widening the seam's visibility and reaching the socket from connection.rs and from the simulator, growing the out-of-file query surface the named TLS seams were introduced to shrink.
  • ConnectionH2::flush_zero_to_socket keeps its own too, and moves with the byte movers. Its only read sits inside a per-iteration #[cfg(debug_assertions)] trace taken after each socket_write; a caller-supplied value would log the pre-flush answer — the stale wants_write= that force_disconnect's own comment forbids, on the truncation diagnostic itself.

Six files touched: h2.rs, h2_close.rs (the ​```ignore example), doc/h2_mux_internals.md, doc/testing.md, LIFECYCLE.md, CHANGELOG.md. No connection.rs, no h1.rs, no sim/, no forward! change, no visibility change to tls_wants_write.

No behaviour change, and the counts say so

Against a baseline measured on a detached d192407b worktree: sozu-lib 1113, sozu 113, sozu-e2e 447, sozu-sim 11/6/6/6 — every one unmoved. fmt --check, clippy --all-targets -D warnings, clippy -p sozu-sim under --cfg tokio_unstable, cargo doc with RUSTDOCFLAGS="-D warnings", and cargo bench -p sozu-lib --bench h2_stream_table -- --test all exit 0.

The coverage guard is unmoved too

Replacing ConnectionH2::tls_wants_write's body with false yields 1103 passed; 10 failed on the tree before this change and the same 1103 passed; 10 failed after it — over an identical set of ten test names, diffed, not merely an identical count.

Two of the ten are a_stalled_window_update_drain_re_signals_the_writable_event and a_stalled_rst_stream_drain_re_signals_the_writable_event — the stages whose query moved out to the caller. That is what shows the seam is still wired to its callers rather than quietly decoupled from them, which is the one way a refactor like this could be silently wrong while every test stays green.

#1484's six ConnectionH2<FrontRustls> tests are byte-unchanged and green, proven structurally rather than by a green run: every hunk in h2.rs ends at or below line 8007, and that block starts at 12497.

Citations

45 doc citations into h2.rs were re-anchored by the line shift, mapped through an exact old→new table built from difflib equal opcodes against d192407b. 0 unmapped. The opcode list doubled as a scope check: exactly 17 changed regions, every one accounted for.

Every cited span was then compared against its pre-image slab by slab, interiors included — not only the endpoints check_doc_citations.py --base --show lists. A range whose two endpoints both land in equal blocks can still straddle an edit in its interior, so an endpoint-only audit is not sufficient. 50 citations, 20 of them multi-line ranges, all byte-identical. That covers the 63 re-anchored spans the check exempts from its own comparison and never looks at.

CHANGELOG placement

Unreleased had no ### ➖ Removed section; this opens one immediately before ### 🔐 Security. The ✨ Added, 🔄 Changed and 🐛 Fixed headings stay at lines 5, 211 and 1406 — unmoved — so no other in-flight changeset's insertion anchor shifts.

Known stale, deliberately not touched

Four TO SEE THIS RED recipes still say self.socket.socket_wants_write(), which #1499 already replaced in production: h2.rs:8260 and :8328 (the WINDOW_UPDATE and RST_STREAM drain stages), :13197 (error_close_action), :13284 (goaway_close_action's third argument). Stale before this change, so left under the no-tidying rule — two of them describe stages this commit edits, so they are now doubly stale. They belong with S4, which touches the same drain stages.

Refs #1339

…sking

`ConnectionH2::ensure_tls_flushed` read `self.tls_wants_write()` itself, so
each of its eight call sites paid a socket query the composite could not see
was already answered. Five of them had the answer in hand. For four, the
re-arm is the second half of a decision `h2_close` had just taken on that
very value: `finalize_action`'s `AfterFlush` phase, `error_close_action`,
`goaway_close_action`'s `AfterFlush` phase, and `force_disconnect`, which
already bound the answer above its `match`. The fifth, `initiate_close_notify`,
reaches no `h2_close` decision at all — its branch condition is the answer.
Nothing mutates the socket in between at any of the five, so the second query
could only repeat the first; all five now pass the binding they hold and the
helper takes a `tls_wants_write: bool`.

The three stalled-drain tails of `flush_pending_control_frames` keep a query,
now spelled at the call site rather than borrowed from the helper. They run
after the `flush_zero_to_socket` whose stall brought them there, and that
write is exactly what changes the answer, so a value bound before the call
would be the wrong one. The helper's doc states which callers are in which
group and why, so the asymmetry is read where it lives.

Two sibling helpers deliberately keep their own query.
`ConnectionH2::has_pending_write` is `pub`, `Connection` dispatches it through
`forward!`, and that macro's H1 arm *is* `socket_wants_write()`, so handing it
a parameter means widening the seam's visibility and reaching the socket from
`connection.rs` and from the simulator — growing the out-of-file query surface
the named seams were introduced to shrink. `ConnectionH2::flush_zero_to_socket`
reads it only inside a per-iteration `#[cfg(debug_assertions)]` trace taken
after each `socket_write`, where a caller-supplied value would log the
pre-flush answer: the stale `wants_write=` that `force_disconnect`'s own
comment forbids.

No behaviour change, and the counts say so. `cargo test -p sozu-lib` is
unmoved at 1113 passed, `sozu` at 113, `sozu-e2e` at 447 and `sozu-sim` at
12/6/6/6. The coverage guard is unmoved too: replacing
`ConnectionH2::tls_wants_write`'s body with `false` yields `1103 passed; 10
failed` before this change and the same ten test names after it, among them
the two stalled-drain tests whose query moved out to the caller — which is
what proves the seam is still wired to its callers rather than quietly
decoupled from them.

52 of the 57 `h2.rs` citation numbers these three documents carry were
re-anchored by the line shift — exactly the 52 spans
`check_doc_citations.py --base` exempts from its own comparison and never
checks. Every cited span was then compared against its pre-image slab by
slab: interiors included and not only the endpoints `--base` lists, and
against both sides of the merge rather than one — 89 spans, 37 of them
multi-line ranges, 1372 source lines, all byte-identical. Each of the 52
exempt spans was then read by hand against the statement its prose names.

Refs #1339

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