Skip to content

h2 sans-io: the socket boundary and RTT capture (Q10, Q11) #1339

Closed

Description

@FlorentinDUBOIS

Part of the H2 sans-io extraction decision set. lib/src/protocol/mux/ holds 95 I/O and
ambient-state touchpoints: 26 mechanical, 55 structural, 14 requiring a maintainer decision

(h2.rs 53 / mod.rs 24 / connection.rs 9 / stream.rs 6 / router.rs 3). ConnectionH2<Front: SocketHandler>
carries 54 production socket references in h2.rs alone, and Context<L> holds
pub pool: Weak<RefCell<Pool>>. The structural majority is the signal: this is not a mechanical
extraction.

No design is proposed here and no work is budgeted until the question below is answered. The goal of
the extraction is a deterministic simulator for H2 in the FoundationDB style described in
doc/testing.md; every question below is a place where the current code reaches for I/O, the clock,
the allocator or a random source from inside what would become the pure core.

Question 10 — does the core still know what a socket is

h2.rs:1685 ConnectionH2<Front: SocketHandler>. Stay generic, with the simulator supplying an
in-memory implementation — cheapest, and keeps socket_wants_write / socket_close TLS-and-fd
semantics inside the core's contract — or drop the parameter for byte-in / byte-out entry points?

Dropping it first requires deciding how rustls's socket_wants_write(), consulted at 15 sites, is
represented. That is the crux: socket_wants_write is not a socket question, it is "the TLS layer has
buffered handshake or alert bytes it must flush before progress". A byte-in/byte-out core needs that
as an explicit output, not an ambient query.

Question 11 — round-trip times

h2.rs:3866 snapshot_rtts. Are client_rtt / server_rtt

  • (a) passed in as already-captured Option<Duration>, or
  • (b) kept as in-core getsockopt(TCP_INFO)?

(a) lets Endpoint::socket(token) (mod.rs:289) be deleted from the trait outright; (b) keeps it,
and keeps a syscall inside the core.

Why these are one decision

11 is only a real question once 10 is settled. If the core stays generic over SocketHandler, (b)
remains available and Endpoint::socket survives. If the core goes byte-in/byte-out, (a) is forced
and Endpoint::socket must go. Answering 11 first would pre-commit 10 by accident.

Activity

  1. FlorentinDUBOIS commented on Sep 21, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    2026-09-21: Q10/Q11 stand as posed, but two of this issue's numbers are wrong, three citations have drifted, and a real correctness bug was hiding in the boundary it flags

    Corrections

    "54 production socket references in h2.rs alone" is wrong — it is 34, and it was 34 before this
    issue was opened.
    Counting self.socket occurrences above the mod tests { line gives 34 against
    origin/main (42ac35bf) and 34 against 74c3ba1f, the commit immediately preceding this issue's
    2026-09-18 opening. So the figure was never correct on any commit — it is not a stale count.
    Issue #1359 independently re-derived it as "34 sites" when naming this issue as its gate; that
    number is the right one.

    "socket_wants_write(), consulted at 15 sites" is correct. grep -n returns 16 hits, of which
    one (h2.rs:2056) is a doc comment and 15 are call sites, unchanged since before this issue opened.

    All three line citations have drifted. They were exactly right when written; five commits since,
    most recently #1396 (+520 lines), have shifted them:

    Citation as written Correct at 74c3ba1f Correct at 42ac35bf
    h2.rs:1685 — struct ConnectionH2<Front: SocketHandler> yes h2.rs:1712
    h2.rs:3866 — fn snapshot_rtts yes h2.rs:4174
    mod.rs:289 — fn socket on Endpoint yes mod.rs:346

    The 95-touchpoint breakdown (26 mechanical / 55 structural / 14 decision; h2.rs 53, mod.rs 24,
    connection.rs 9, stream.rs 6, router.rs 3) is arithmetically self-consistent — both groupings
    sum to 95 — but is not independently re-derivable, because the issue does not state the rule that
    sorts a touchpoint into one bucket or another. No evidence it is wrong; only that it cannot be
    checked the way the other two were.

    What Q10 and Q11 reduce to

    Q10 is unchanged in substance. The fork is still "stay generic over SocketHandler" vs.
    "byte-in/byte-out", and socket_wants_write() — the TLS-flush query, not a plain read or write — is
    still the crux at 15 call sites. Only the scale moves: the generic-Front surface in h2.rs
    production code is 34 self.socket accesses plus one Endpoint::socket(token) call from
    snapshot_rtts. Smaller than claimed, not smaller in kind.

    Q11 is unchanged and still gated on Q10: pass Option<Duration> in (which lets
    Endpoint::socket, now mod.rs:346, be deleted) or keep getsockopt(TCP_INFO) in-core (which
    keeps it).

    The standalone defect, found and fixed

    ConnectionH2::zero is simultaneously the read-side accumulation buffer for a HEADERS+CONTINUATION
    field block in progress — header_block_fragment is a (start, len) window into it, valid across
    however many readable() passes the block takes, by protocol design — and the write scratch space
    flush_pending_control_frames reuses for any queued WINDOW_UPDATE or RST_STREAM, on any
    stream. Mux::ready dispatches frontend readable() then writable() in the same sweep, so
    ordinary connection-level flow-control housekeeping landing between a stream's HEADERS and its
    CONTINUATION cleared zero.storage out from under the accumulating fragment.

    No attacker is needed. The failure is silent: the clobbered bytes still HPACK-decode, so a
    legitimate two-frame block carrying x-sozu-marker: legitimate-value came back as
    ("from", <garbage>) rather than as a decode error.

    Fixed in #1397 by header_block_reassembly_in_progress() (h2.rs:3887), gating the WINDOW_UPDATE
    drain (h2.rs:3950) and RST_STREAM drain (h2.rs:4021) of flush_pending_control_frames
    (h2.rs:3899) to defer rather than flush. Nothing is lost — both frame kinds already arm WRITABLE
    at queue time. This mirrors the protection the codebase already applies to the same buffer in the
    other direction: a stalled zero-buffer write already disables READABLE so a fresh read cannot
    clobber it; the read side had no equivalent.

    Same bug class, different trigger — not fixed

    graceful_goaway (h2.rs:5011) also clears self.zero.storage unconditionally at h2.rs:5029 to
    serialize the advisory GOAWAY, despite its own comment stating the intent to keep reading in-flight
    streams during the drain window. A worker's graceful shutdown or hot reload landing mid-reassembly
    hits the identical corruption — unconditionally, every time, not only when a control frame happens
    to be queued. It wants its own changeset and its own test.

  2. FlorentinDUBOIS commented on Sep 21, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    Q10 and Q11 are answered (2026-09-21). Also: four of this issue's five per-file counts are wrong.

    Measured against origin/main at 7be659544163f0d780f8eb0666a25d992f5f8e23.

    Q10 — the core goes byte-in / byte-out

    ConnectionH2 loses its Front: SocketHandler parameter. It never touches a socket, Readiness, or
    mio. rustls lives entirely in an I/O shell.

    The argument that decided it is not the fifteen socket_wants_write() sites. It is the backend
    leg:

    // lib/src/protocol/mux/router.rs
    backends: HashMap<Token, Connection<SessionTcpStream>>

    Hard-coded to SessionTcpStream whatever the frontend is — MuxClear or MuxTls. And
    SessionTcpStream overrides neither socket_wants_write nor socket_close, so it takes the trait
    defaults (false / no-op, lib/src/socket.rs). The same generic core is therefore already
    monomorphised a second time today, with every one of those fifteen checks resolving statically to
    false.
    Sōzu demonstrates on every request that the TLS coupling is incidental rather than
    structural. That is stronger evidence than any argument about the call sites themselves.

    Two supporting findings:

    The fifteen sites do not survive as core logic. About eleven are trace!/debug! arguments or
    edge-triggered WRITABLE re-arming (ensure_tls_flushed, the flush_pending_control_frames stages).
    The remaining four gate a close, and that need is satisfied by inverting control: the core emits its
    bytes plus a close intent, and the shell — which alone knows the rustls backlog — decides when to
    tear down. None of the fifteen decodes a frame, advances a stream, or touches flow control.
    Counting h1.rs (10) and shared.rs's drain_tls_close_notify (2), the mechanism is 27 sites
    repo-wide, every one asking the same question, and entirely inert for plain TCP.

    The alternative this issue posed — "stay generic, simulator supplies an in-memory implementation" —
    is what we already do.
    SocketHandler is the TLS abstraction trait and socket_wants_write()
    is the ambient poll. Keeping it renames the problem. Worth noting because quinn-proto is often
    cited as the model here: it does put TLS inside the core behind crypto::Session, but for a reason
    that does not transfer — QUIC carries the TLS handshake inside its own CRYPTO frames, so its core has
    no choice. H2 runs on an ordinary TLS record stream that we already terminate.

    One more blocker worth recording, since it is what actually stops an in-memory implementation today:
    SocketHandler::socket_ref/socket_mut return &mio::net::TcpStream, a concrete OS type. No
    in-memory pipe can satisfy that signature. The current H2 unit-test harness uses a real loopback TCP
    socket accordingly, so it is not deterministic in the FoundationDB sense either.

    Q11 — forced by Q10, exactly as this issue predicted

    Option (a). client_rtt / server_rtt arrive as already-captured Option<Duration>, and
    Endpoint::socket(token) leaves the trait. This issue's own framing was right: "If the core goes
    byte-in/byte-out, (a) is forced and Endpoint::socket must go."

    socket_rtt is a raw getsockopt(TCP_INFO) (lib/src/socket.rs), reached through
    Endpoint::socket(token) from four call sites — snapshot_rtts in h2.rs plus three inline copies
    of the same three-line pattern in h1.rs, which has no shared helper. RTT is intrinsically a
    live-socket property; a byte-in/byte-out core cannot produce it, so the shell injects it. Note this
    changes the Endpoint trait's shape for every implementor, not only H2.

    Correction to this issue's counts

    Four of the five per-file figures are wrong, and the issue contradicts itself: its breakdown sums
    53+24+9+6+3 = 95 while it quotes "54 production socket references in h2.rs" separately.

    file stated here measured at 7be65954
    h2.rs 53 (54 elsewhere in the text) 54
    mod.rs 24 22
    connection.rs 9 13
    router.rs 3 12
    stream.rs 6 0
    total 95 101

    Method: non-comment lines matching socket, restricted to each file's production range (before its
    #[cfg(test)]/mod tests). It is textual, so it also catches field declarations and the local
    socket_write: bool; the point is that it is the same method applied to every file.

    Two deltas matter.

    stream.rs is 0, not 6. Stream touches no socket at all — the file says so itself: "Stream
    does not own a socket reference — the frontend socket lives on the parent Mux/connection and the
    backend socket lives on…"
    . The six were comments. This is good news for sequencing: the stream slot
    lifecycle can be extracted without untangling any I/O.

    router.rs is 12, not 3, and they are the least mechanical ones in the tree: set_nodelay
    (setsockopt), register_socket (epoll registration, with its own CWE-400 rollback comment), and the
    raw TcpStream handoff from the connect path. These stay in the shell, but there are four times as
    many of them as this issue budgeted.

    Related measurements, same revision: nine live Instant::now()/SystemTime::now() reads remain in
    mux/, all at construction or in diagnostics — the per-pass snapshot discipline from #1358 holds.
    Exactly one production ULID site, in ConnectionH2::create_stream, which hides a SystemTime::now()
    inside rusty_ulid and is therefore invisible to any grep over mux/. Both pool checkout sites are
    already Option-fallible with a graceful REFUSED_STREAM-shaped fallback, which is the shape a
    sans-io core wants.

    The hardest site, recorded so it is not discovered late

    ConnectionH2::writable, GoAway arm (h2.rs around :4145): a nested check that fuses "rustls has
    buffered records" with "did the kernel accept the flush". Its own comment calls it the primary
    truncation vector
    under HAProxy chaining. A sans-io core cannot attempt a flush itself, so this
    two-level backpressure signal has to be reconstructed entirely from what the shell reports back.
    Getting it wrong is data loss, not style.

    What this unblocks

    #1359's C4 was explicitly gated on this question. It is now unblocked.

    The extraction is a strangler — one sub-machine at a time, each independently mergeable with its
    regression test seen red first — with a differential layer replaying one scenario against both the
    core in simulation and a real worker. Sequencing follows in its own comment.

  3. added 20 commits that reference this issue on Sep 24, 2026
  4. FlorentinDUBOIS commented on Sep 25, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    Q10 and Q11 are shipped. Closing, with the numbers re-measured one last time.

    Verified against origin/main at fa11f549c02722a378b983a0b3cb716187f54e14. Citing by symbol
    rather than by line, because every line citation in this issue's original text had drifted by the
    time the first comment checked them — that is the whole lesson of #1447.

    Q10 — the core went byte-in / byte-out, as decided

    ConnectionH2 no longer carries a Front: SocketHandler parameter. It is a plain pub struct ConnectionH2. The socket, Readiness and rustls live in H2Shell<Front: SocketHandler>, which
    owns the core rather than the other way round.

    The measurement this issue opened on was "54 production socket references in h2.rs alone". A
    later comment corrected it to 34 and showed it had never been 54 on any commit. It is now 11,
    and the more useful fact is where they are: every one of them sits inside an
    impl<Front: SocketHandler> H2Shell<Front> block. The twelfth occurrence is prose — a doc comment
    on ConnectionH2::peer_address explaining why the peer= slot is no longer sourced from
    self.socket. Zero socket references remain in the core.

    socket_wants_write() — the crux, and correctly identified as such — is no longer an ambient query
    the core makes. It is the shell's business, which is what "byte-in/byte-out" had to mean for the
    15 call sites to be answerable at all.

    Q11 — (a), and Endpoint::socket is gone

    snapshot_rtts is now fn snapshot_rtts<E: Endpoint>(&self, endpoint, linked_token) -> (Option<Duration>, Option<Duration>), returning self.client_rtt and endpoint.peer_rtt(token).
    No getsockopt(TCP_INFO) inside the core.

    Endpoint::socket was deleted from the trait, exactly as the issue predicted answering (a) would
    allow. Its replacement is Endpoint::peer_rtt(token) -> Option<Duration>, and the distinction is
    the point rather than a rename: the predecessor handed out a mio::net::TcpStream, a concrete OS
    type, so any connection could reach any other connection's socket for any purpose and no in-memory
    transport could ever satisfy the trait. The successor returns the value. The only surviving
    mention of fn socket on Endpoint in mod.rs is that doc comment recording why it went.

    The standalone defect this issue flagged and left open is also closed

    The 2026-09-21 comment recorded a second instance of the zero.storage clobber — graceful_goaway
    clearing the buffer unconditionally to serialize the advisory GOAWAY, mid-reassembly, on every
    graceful shutdown rather than only when a control frame happened to be queued — and said it wanted
    its own changeset and its own test. It got both, as #1398, now closed.

    graceful_goaway computes header_block_reassembly_in_progress() and hands it to
    H2DrainState::begin_graceful_drain, which answers DeferInitial; the deferred GOAWAY is
    re-emitted by flush_pending_control_frames through send_initial_goaway once reassembly
    completes, and readiness.arm_writable() guarantees a writable() pass so it is not left waiting
    on an unrelated event. The test is a_legitimate_continuation_survives_a_graceful_goaway, and it
    carries a "To SEE THIS RED" recipe whose honest content is that no mutation is needed — the red was
    the shipping behaviour on main.

    What this issue does not close

    Nothing here. The sans-io arc's remaining content lives in #1340 (Q12 part 2) and #1341 (Q4).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions