Repository navigation
refactor(mux-h2): drop ConnectionH2's Front parameter into an H2Shell (#1339, Q10) - #1516
Merged
Merged
Conversation
…#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
requested review from
Wonshtrum and
llenotre
as code owners
September 25, 2026 06:53
This was referenced Sep 25, 2026
Closed
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.
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_rttsheld the lastSocketHandler::socket_refreach and no amount of shell-lifting could remove it.What changed
Connection::H2now holdsH2Shell<Front: SocketHandler> { core: ConnectionH2, socket: Front }.ConnectionH2names no socket, noSocketHandlerand no type parameter.Connection<Front: SocketHandler>andRouter::backends' declared type are unchanged: the parameter did not leave the mux, it moved off the state machine.self.socketcode references inh2.rsabovemod testsConnectionH2's implsH2Shell's impls(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_timeoutandset_timeout_durationwiden frompub(super)topubso 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 topubto let the socket keep working — thepubsurface that grew exists for the out-of-crate driver below.Three core steps changed shape rather than moving
flush_pending_control_framescalledflush_zero_to_socketat three points. It is now a resumable walk answeringH2ControlFlushTarget::FlushZero(H2ControlFlushStage), withhandle_control_flushrunning 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-armexpect_writeon 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.closemoved whole, taking itsshared::drain_tls_close_notifyhand-off with it.ConnectionH1::closeshares that helper byte-for-byte and is untouched.force_disconnectis inverted, per the maintainer's call.The
force_disconnectinversion, and one deliberate deviationIt read
tls_wants_writeitself from 17 sites (not 19 — a repo-wide grep picks up 2 more onConnectionH1::force_disconnectinh1.rs, a different type that keeps its socket), 7 of them inside thepub, socket-freehandle_read. So neither moving it nor threading a socket-derivedboolthroughgoaway's 31 call sites and the 9check_flood_or_return!sites was available.H2FinalizeTarget,H2ControlFlushTargetandH2WritableStateTargetare all returned by the step that raises them.H2ForceDisconnectTargetcannot be —handle_readreturnsMuxResultand its signature does not change — so it is parked on the connection, taken bypoll_force_disconnect, and settled byforce_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; onPosition::Clientthe backend status andHUPevent) and defers only the decision, theReArmAndContinuereadiness write and the threedebug!lines that renderwants_write=.peer_gone_after_final_goaway()is carried in the target rather than re-read: it readsstream_tableandzero.storage, andremove_dead_streamruns between raise and settlement on thereset_streampaths.H2Shell::settledtakes the parked target, so a double settlement is impossible, and everyMuxResulta core step hands the shell passes through it — a missed settlement is greppable, not asserted.H2Shell::settledoes the same forstart_stream(answersbool) andcancel_timed_out_streams(answers nothing), which settle immediately becauseMuxreadsReadiness::filter_interestbetween entry points.The discipline is greppable, not asserted. Every core call in the shell impl whose type is
MuxResultis either an argument ofsettled()or flows into one:plus
start_streamandcancel_timed_out_streams, whose own answers are notMuxResultand which callsettle()directly. A core call in that block returningMuxResultthat appears nowhere in that list is the bug.One behaviour change, named
cancel_timed_out_streams(let _ = self.enqueue_rst(..)) andstart_stream(discardedgraceful_goaway) drop aMuxResultthat can carry a force-disconnect. ACloseSessionraised there used to be swallowed and the connection closed a pass later through theH2State::Errorarm; it now propagates on this pass. Both are reachable only through agen_goawayserialisation failure —goawayandsend_initial_goawayreachforce_disconnecton that arm alone.A carried
tls_wants_writefield 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.rsheld aConnection<SimSocket>, soSimSockethad to be aSocketHandler, whosesocket_ref/socket_mutreturn&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, aPlaceholderPeerto keep it connected, and the singlemioline under[target.'cfg(tokio_unstable)'.dev-dependencies].The harness now builds
ConnectionH2directly and feeds it&[u8]in /Vec<u8>out over two byte queues, driving the samepubpoll/handle pairsH2Shelldrives.SimSocket,PlaceholderPeerand themiodev-dependency are all gone.H2Harness::pumpwas rewritten. Every scenario and assertion is unchanged —h2_simulationstill reports12 passed.Cargo.lockloses 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
unsafewindow, re-proved at its new locationExactly one
unsafeblock in productionh2.rs, wrapping thegathercall only. Inwrite_streamsas it now sits onH2Shell: theVec<IoSlice<'static>>is declared at the top of the function body, theunsafe { gather }call is 48 lines below it, andconfirmthree statements after that — all three inside the same privatefn write_streams. Between gather and confirm, the only thing entered issocket_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 throughConnectionH2::write_bufferinstead of the free function — neither moves either end of the bracket.write_streamsstays private, onH2Shellexactly as it was onConnectionH2, so the bracket still cannot span the now-pubpoll/handle seam. The simulator's ownwrite_streamsopens 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_returnsguards the new mechanism from the read path, where the direct-call sibling cannot reach. Seen red first against its own recipe (returnresultinstead ofself.settled(result)inH2Shell::readable):left: Hup,right: Writable | Error | Hup, whileforce_disconnect_re_arms_while_tls_records_are_pendingstays green — which is why both exist.sozu-libgoes 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, theDebugnote),doc/testing.md,doc/lifetime_of_a_session.md,lib/src/protocol/mux/LIFECYCLE.mdandCHANGELOG.md— including themiodev-dependency removal as a build-contract change.