Skip to content

refactor(mux-h2): drop ConnectionH2's Front parameter into an H2Shell (#1339, Q10) - #1516

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-shell-socket-boundary
Sep 25, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-shell-socket-boundary

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Question 10 of #1339, and the milestone the socket-boundary series (#1496, #1499, #1507, #1511, #1512, #1514) was built for. Stacked on Q11 (#1515), which had to land first: snapshot_rtts held the last SocketHandler::socket_ref reach and no amount of shell-lifting could remove it.

What changed

Connection::H2 now holds H2Shell<Front: SocketHandler> { core: ConnectionH2, socket: Front }. ConnectionH2 names no socket, no SocketHandler and no type parameter.

Connection<Front: SocketHandler> and Router::backends' declared type are unchanged: the parameter did not leave the mux, it moved off the state machine.

before after
production self.socket code references in h2.rs above mod tests 7 7
…of those, in ConnectionH2's impls 7 0
…of those, in H2Shell's impls — 7

(11 grep lines after, 4 of them /// prose. The code/prose distinction has bitten this series repeatedly, so both numbers are given.)

poll_timeout, arm_timeout and set_timeout_duration widen from pub(super) to pub so an out-of-crate driver can arm the connection deadline.

Both halves live in h2.rs, so the shell reaches the core's private fields exactly as the methods it inherited did. Nothing was widened to pub to let the socket keep working — the pub surface that grew exists for the out-of-crate driver below.

Three core steps changed shape rather than moving

  • flush_pending_control_frames called flush_zero_to_socket at three points. It is now a resumable walk answering H2ControlFlushTarget::FlushZero(H2ControlFlushStage), with handle_control_flush running the continuation belonging to the stage that asked. The three differ — only the zero-buffer resume re-enables READABLE interest, only the two serialising stages re-arm expect_write on a stall — so the stage travels out with the request and back with the answer rather than a flag living on the connection. The walk cannot restart from the top: the hung-up cleanup and the SETTINGS-ACK timeout above it are not idempotent mid-pass.
  • close moved whole, taking its shared::drain_tls_close_notify hand-off with it. ConnectionH1::close shares that helper byte-for-byte and is untouched.
  • force_disconnect is inverted, per the maintainer's call.

The force_disconnect inversion, and one deliberate deviation

It read tls_wants_write itself from 17 sites (not 19 — a repo-wide grep picks up 2 more on ConnectionH1::force_disconnect in h1.rs, a different type that keeps its socket), 7 of them inside the pub, socket-free handle_read. So neither moving it nor threading a socket-derived bool through goaway's 31 call sites and the 9 check_flood_or_return! sites was available.

H2FinalizeTarget, H2ControlFlushTarget and H2WritableStateTarget are all returned by the step that raises them. H2ForceDisconnectTarget cannot be — handle_read returns MuxResult and its signature does not change — so it is parked on the connection, taken by poll_force_disconnect, and settled by force_disconnect_after_query. That is the one place this step departs from the S3–S5 grammar, and it is forced.

The core settles everything socket-free at the raising site (H2State::Error; on Position::Client the backend status and HUP event) and defers only the decision, the ReArmAndContinue readiness write and the three debug! lines that render wants_write=. peer_gone_after_final_goaway() is carried in the target rather than re-read: it reads stream_table and zero.storage, and remove_dead_stream runs between raise and settlement on the reset_stream paths.

H2Shell::settled takes the parked target, so a double settlement is impossible, and every MuxResult a core step hands the shell passes through it — a missed settlement is greppable, not asserted. H2Shell::settle does the same for start_stream (answers bool) and cancel_timed_out_streams (answers nothing), which settle immediately because Mux reads Readiness::filter_interest between entry points.

The discipline is greppable, not asserted. Every core call in the shell impl whose type is MuxResult is either an argument of settled() or flows into one:

7402: let result = self.core.force_disconnect();        -> self.settled(result)
7411: let result = self.core.graceful_goaway(now);      -> self.settled(result)
7754: return self.core.finalize_write_after_flush(..);  -> write_streams -> writable_inner -> writable -> settled

plus start_stream and cancel_timed_out_streams, whose own answers are not MuxResult and which call settle() directly. A core call in that block returning MuxResult that appears nowhere in that list is the bug.

One behaviour change, named

cancel_timed_out_streams (let _ = self.enqueue_rst(..)) and start_stream (discarded graceful_goaway) drop a MuxResult that can carry a force-disconnect. A CloseSession raised there used to be swallowed and the connection closed a pass later through the H2State::Error arm; it now propagates on this pass. Both are reachable only through a gen_goaway serialisation failure — goaway and send_initial_goaway reach force_disconnect on that arm alone.

A carried tls_wants_write field refreshed by the shell after every socket call was considered and rejected: less code, but it puts an ambient socket fact back in the core — the thing this series exists to remove — and rests on a refresh discipline rather than a shape.

The measurable outcome: the simulator

sim/tests/h2_simulation.rs held a Connection<SimSocket>, so SimSocket had to be a SocketHandler, whose socket_ref/socket_mut return &mio::net::TcpStream — a concrete OS type no in-memory transport can synthesise. That forced a connected loopback stream the harness never read or wrote, a PlaceholderPeer to keep it connected, and the single mio line under [target.'cfg(tokio_unstable)'.dev-dependencies].

The harness now builds ConnectionH2 directly and feeds it &[u8] in / Vec<u8> out over two byte queues, driving the same pub poll/handle pairs H2Shell drives.

SimSocket, PlaceholderPeer and the mio dev-dependency are all gone. H2Harness::pump was rewritten. Every scenario and assertion is unchanged — h2_simulation still reports 12 passed. Cargo.lock loses exactly one line. No production dependency changes.

That the in-tree shell and an out-of-crate driver now stand in the same places is what makes the split measurable rather than asserted.

The unsafe window, re-proved at its new location

Exactly one unsafe block in production h2.rs, wrapping the gather call only. In write_streams as it now sits on H2Shell: the Vec<IoSlice<'static>> is declared at the top of the function body, the unsafe { gather } call is 48 lines below it, and confirm three statements after that — all three inside the same private fn write_streams. Between gather and confirm, the only thing entered is socket_write_vectored, which reborrows the descriptors for the call and cannot retain them.

The window did not widen. What changed is the receiver (&mut ConnectionH2<Front> → &mut H2Shell<Front>) and the buffer arriving through ConnectionH2::write_buffer instead of the free function — neither moves either end of the bracket. write_streams stays private, on H2Shell exactly as it was on ConnectionH2, so the bracket still cannot span the now-pub poll/handle seam. The simulator's own write_streams opens and closes its own bracket inside its own single function, for the same reason.

Tests

a_force_disconnect_raised_inside_handle_read_is_settled_before_the_pass_returns guards the new mechanism from the read path, where the direct-call sibling cannot reach. Seen red first against its own recipe (return result instead of self.settled(result) in H2Shell::readable): left: Hup, right: Writable | Error | Hup, while force_disconnect_re_arms_while_tls_records_are_pending stays green — which is why both exist.

sozu-lib goes 1118 → 1119 (--all-features), the one addition being that test.

Docs

doc/h2_mux_internals.md (the three TLS seams, a new shell section, the inversion, the Debug note), doc/testing.md, doc/lifetime_of_a_session.md, lib/src/protocol/mux/LIFECYCLE.md and CHANGELOG.md — including the mio dev-dependency removal as a build-contract change.

…#1339, Q10)

`Connection::H2` now holds `H2Shell<Front: SocketHandler> { core: ConnectionH2,
socket: Front }`, and `ConnectionH2` names no socket, no `SocketHandler` and no
type parameter. `Connection<Front>` and `Router::backends`' declared type are
unchanged: the parameter did not leave the mux, it moved off the state machine.

Production `self.socket` code references in `h2.rs` above `mod tests` stay at 7
and all 7 are now on `H2Shell`; `ConnectionH2`'s own impls hold 0.
`poll_timeout`, `arm_timeout` and `set_timeout_duration` widen to `pub` so an
out-of-crate driver can arm the connection deadline. Both halves live in
`h2.rs`, so the shell reaches the core's private fields exactly as the methods
it inherited did — nothing was widened to `pub` to let the socket keep working.

Three core steps changed shape rather than moving:

- `flush_pending_control_frames` called `flush_zero_to_socket` at three points.
  It is now a resumable walk answering `H2ControlFlushTarget::FlushZero(stage)`,
  with `handle_control_flush` running the continuation belonging to the stage
  that asked. The three differ, so the stage travels out with the request and
  back with the answer instead of a flag living on the connection.
- `close` moved whole, taking its `shared::drain_tls_close_notify` hand-off
  with it. `ConnectionH1::close` shares that helper and is untouched.
- `force_disconnect` is inverted. It read `tls_wants_write` itself from 17
  sites, 7 inside the `pub`, socket-free `handle_read`, so neither moving it nor
  threading a socket-derived bool through `goaway`'s 31 call sites was
  available. It settles everything socket-free at the raising site, parks an
  `H2ForceDisconnectTarget` carrying `peer_gone_after_final_goaway()` as it read
  it, and `force_disconnect_after_query` settles it with the answer
  `H2Shell::settled` read. That helper takes the parked target, so a double
  settlement is impossible, and every `MuxResult` a core step hands the shell
  goes through it.

One behaviour change: `cancel_timed_out_streams` and `start_stream` discard a
`MuxResult` that can carry a force-disconnect. A `CloseSession` raised there was
swallowed and the connection closed a pass later through the `H2State::Error`
arm; it now propagates on this pass. Both are reachable only through a
`gen_goaway` serialisation failure.

The measurable outcome is the simulator. `sim/tests/h2_simulation.rs` held a
`Connection<SimSocket>`, so `SimSocket` had to be a `SocketHandler`, whose
`socket_ref`/`socket_mut` return `&mio::net::TcpStream` — a concrete OS type no
in-memory transport can synthesise. That forced a loopback placeholder the
harness never read or wrote, a `PlaceholderPeer` to keep it connected, and the
`mio` dev-dependency. The harness now builds `ConnectionH2` directly and feeds
it `&[u8]` in / `Vec<u8>` out over two byte queues, driving the same `pub`
poll/handle pairs `H2Shell` drives. `SimSocket`, `PlaceholderPeer` and the `mio`
dev-dependency are gone, `pump` was rewritten, and every scenario and assertion
is unchanged — `sozu-sim` still reports 12 passed for `h2_simulation`.
`Cargo.lock` loses exactly one line. No production dependency changes.

`a_force_disconnect_raised_inside_handle_read_is_settled_before_the_pass_returns`
guards the new mechanism and was seen red first against its own recipe.

`doc/h2_mux_internals.md`, `doc/testing.md`, `doc/lifetime_of_a_session.md`,
`LIFECYCLE.md` and `CHANGELOG.md` are updated in this changeset.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 5355cec into main Sep 25, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the refactor/h2-shell-socket-boundary branch September 25, 2026 07:47
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