Skip to content

h2 sans-io: who owns timers (Q3) — the poll_timeout series, and why C1 must precede C2 #1359

Closed

Description

@FlorentinDUBOIS

Split out of #1337, which asked three questions about the H2 core's relationship to time. Q2 (how the core learns the time) and Q13 (the flood detector's clock) are answered and implemented. Q3 — who owns timers — is not, and it is the one that actually gates the deterministic H2 simulator.

The end state

The core stops owning TimeoutContainer handles and instead returns a next-deadline Option<Instant> that the embedder arms, with Mux's SessionState impl becoming the adapter. This is the shape lib/src/protocol/udp/manager.rs already uses: a poll_timeout() returning the next deadline plus an ArmTimer output the shell acts on.

Blast radius on the core side is small and exactly enumerable — six TimeoutContainer sites in lib/src/protocol/mux/h2.rs (the field, the constructor parameter, the constructor init, and three reset() calls). The Mux side carries fourteen, plus a forwarder in connection.rs and two in router.rs.

Why this is not commissioned yet

Two reasons, and the second is the one worth writing down.

It buys nothing without the socket split. ConnectionH2 owns socket: Front and touches it at 34 sites, fifteen of which are socket_wants_write() — and that one is a TLS-buffer question wearing a socket's clothes. A clock-and-timer refactor without the socket boundary (Q10, #1339) yields no simulator. The timer half is the smaller half. So this work should start only once there is an actual commitment to sim/tests/h2_simulation.rs with a named owner, and that commitment has to scope the socket split too.

The ordering is not negotiable, for a counter-intuitive reason. "Re-validate at the firing site" is necessary but not sufficient. Under a memoized reschedule() plus a timer wheel that can fire early, re-validation alone converts a spurious fire into a lost wakeup — the timer is consumed, the re-validation rejects it, and nothing re-arms.

That is not hypothetical. It is exactly the bug fixed in the UDP shell in #1354: UdpManager::reschedule only emitted ArmTimer when the next deadline differed from armed_deadline, and the fix was clearing armed_deadline on entry to handle_timeout — consume-then-reschedule.

Today's H2 path cannot have that bug, and this is the part that makes the ordering load-bearing rather than cautious. TimeoutContainer is duration-based with no memoization: TimeoutContainer::set (lib/src/timer.rs:120-129) unconditionally cancels and re-arms, and every StateResult::Continue exit from Mux::timeout re-arms. The hazard belongs to the proposed design, not to the current one. C2 is what introduces the memoization that makes C1's re-validation load-bearing — which is why C2 must not land without C1.

A corollary worth stating: there is no standalone bug fix hiding in Q3. That was the most plausible "actionable now" candidate across the whole fourteen-question decision set, and it does not pan out.

Order

  1. C1 — stored deadlines plus re-validation in Mux::timeout.
  2. C2 — poll_timeout(), adapter-owned container, consume-then-reschedule, and the two debug_asserts carried over from the UDP manager. Must not land without C1.
  3. C3 — delete the six h2.rs container sites.
  4. C4 — the simulator.

Nothing in this series lands before the Q2/Q13 clock work, which is now on main.

One loose end, unverified

In the backend-token branch, backend.timeout_container().triggered() fires and is re-armed only when !should_close, while the shared tail can return Continue through delay_close_for_frontend_flush("timeout") with only the frontend container re-armed. Whether that backend can still be in router.backends with live work at that point has not been traced — it needs a focused read of Connection::end_stream and delay_close_for_frontend_flush, or an integration test that stalls a backend after a partial response.

If it holds it is a small independent fix deserving its own issue; if it does not, it deserves one comment saying why it cannot happen. Explicitly flagged as unverified rather than asserted.

Activity

  1. FlorentinDUBOIS commented on Sep 20, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    C1, C2 and C3 landed on main in 95c5f379 (PR #1369) on 2026-09-20. C4 remains, and this issue stays open for it.

    step state
    C1 — stored deadlines plus re-validation in Mux::timeout landed
    C2 — poll_timeout(), adapter-owned container, consume-then-reschedule landed
    C3 — delete the six h2.rs container sites landed (inside C2, see below)
    C4 — the simulator, sim/tests/h2_simulation.rs not started

    Three things this issue asserted turned out to be wrong, and the PR corrected them with measurements rather than argument:

    "There is no standalone bug fix hiding in Q3" — there was. duration_to_tick rounds to the nearest tick, so an entry armed for D is delivered from D - tick/2 onwards. With tick_ms = 100 the earliness is (D + 50) mod 100, i.e. [0, 99] ms. A 60 s front_timeout could write a 408 and close a live session at 59.901 s. Mux::timeout took every delivery at face value.

    C3 could not be its own commit. An intermediate tree where the adapter owns a container and the core still owns one either arms the wheel twice or not at all, so neither half builds-and-tests green alone. C3 is folded into C2. The ordering that this issue said was load-bearing — C1 standalone, before C2 — is kept.

    The ordering constraint is now a test, not an argument. Mutating the tree to the C2-without-C1 shape reddens with timer coherence for Token(0): armed=false but the core wants Some(Instant{..}) — the lost wakeup, caught by Mux::debug_assert_timer_coherence carried over from UdpManager::check_invariants.

    The loose end flagged as unverified cannot happen. The question was whether a backend can still be in router.backends with live work when the shared tail returns Continue with only the frontend container re-armed. It cannot: Mux::timeout's backend branch calls backend.end_stream(..) unconditionally at the bottom of every iteration, outside the if/else chain. No separate issue is needed.

    What C4 still needs is the socket boundary this issue named as its real gate (Q10, #1339): ConnectionH2 owns socket: Front across 34 sites, fifteen of them socket_wants_write(). The timer half was the smaller half, exactly as written here.

  2. FlorentinDUBOIS commented on Sep 21, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    C4 is unblocked (2026-09-21), and the timer boundary itself is already done

    Measured against origin/main at 7be659544163f0d780f8eb0666a25d992f5f8e23.

    The gate is lifted

    This issue's previous comment ended: "What C4 still needs is the socket boundary this issue named as
    its real gate (Q10, #1339)."
    Q10 is now answered — the core goes byte-in / byte-out, rustls moves to
    an I/O shell, and Endpoint::socket(token) leaves the trait. See
    #1339 (comment).

    Worth recording here: the timer extraction went further than this issue's table says

    The table above marks C1/C2/C3 as landed in 95c5f379 (#1369). Reading the tree rather than the
    table: commit 22601f73, "refactor(mux): give the H1/H2 cores poll_timeout() and the adapter the
    timer"
    , is reachable through that merge, and grep finds zero crate::timer or
    TimeoutContainer references in either h2.rs or h1.rs — every remaining hit is inside a comment.
    ConnectionH2::poll_timeout, arm_timeout, clear_timeout and set_timeout_duration exist today,
    and LIFECYCLE.md invariant 23 records the ownership split.

    So the timer sub-machine is not merely "C1-C3 of a four-step plan". It is a completed sans-io
    extraction
    : the core publishes an intent, the adapter owns the real resource. That shape is now the
    template every other sub-machine copies.

    It is also the strongest available evidence for the strategy chosen for the rest of the work. Timers
    came out first and independently of flow control, stream-slot bookkeeping and HEADERS/CONTINUATION
    reassembly, on a 10,407-line file, without a parallel rewrite. That is a demonstration rather than an
    assumption.

    What C4 actually is, revised

    C4 was scoped here as "the simulator, sim/tests/h2_simulation.rs". Two revisions, both from reading
    the code rather than re-arguing the design.

    moonpool is not the first thing to build. A moonpool Workload buys a seeded RNG and a virtual
    clock, which pay off once there is a wide randomized action grammar to sweep. Before that exists, the
    useful artefact is a directed scenario library — the three zero.storage bugs of 2026-09-21
    (#1396, #1397, #1401) and their neighbours — driven by a plain synchronous harness. That can be built
    against main as it stands today, and it is not throwaway: the scenario and assertion layer is
    untouched as each sub-machine is extracted, and only the driver is swapped, once per sub-machine.

    The simulator alone is not sufficient, because both sides would be sozu-lib. The plan is a
    differential layer — one scenario replayed against the core in simulation and against a real worker
    end-to-end — but sim-observed == e2e-observed is not a real check here: in simulation we drive
    ConnectionH2, and end-to-end we drive a worker that contains ConnectionH2. A shared bug agrees
    with itself, and that comparison would have been green through all three of the September 21 bugs.

    The symmetry is broken by an independent oracle carried on the scenario, derived from the
    scenario's own construction inputs and never from any Sōzu output. The assertion is three-way,
    sim == e2e == expect. This is not novel here: sim/tests/tcp_preread_sim.rs already carries
    expected: Option<Output> computed independently of the code under test, and the three regression
    tests added on 2026-09-21 already encode a fixed header set with the peer's own loona_hpack::Encoder
    and assert against that fixed set, never against what Sōzu claims it decoded.

    One further trap specific to the end-to-end leg: worker.soft_stop() crosses a real command channel
    to an independently scheduled thread, so replaying the #1401 scenario has three possible outcomes and
    two of them pass without exercising the race at all. The harness therefore has to report a
    race-window hit rate — confirming from captured logs that the drain was actually deferred — and treat
    a scenario with zero hits as inconclusive, never as passing.

    Where this sits in the sequence

    Timer ownership is done. The remaining extraction runs: HPACK-owned state → connection-level flow
    control → H2 wire-level stream-slot bookkeeping (then per-stream flow control) → the flood detector →
    the GOAWAY/drain data split → HEADERS+CONTINUATION reassembly with the buffer-role split → the
    write_streams encoder-borrow restructuring → priority scheduling → ConnectionH2 drops Front.

    Two of those orderings are load-bearing rather than convenient. Reassembly cannot precede the
    stream-slot map, because the CONTINUATION-flood abort path (CVE-2024-27316) calls remove_dead_stream
    mid-reassembly. And priority cannot precede the borrow restructuring for a borrow-checker reason:
    H2BlockConverter holds &mut self.encoder across the entire per-stream loop in write_streams, so
    no &mut self method can run inside it — the existing free function distribute_overhead was made
    free precisely to route around that.

    Keeping this issue open for C4.

  3. FlorentinDUBOIS commented on Sep 24, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    C4 landed in #1485 (3304bb1a). All four steps of this issue are now on main, so closing.

    step landed
    C1 — stored deadlines plus re-validation in Mux::timeout #1369, 95c5f379
    C2 — poll_timeout(), adapter-owned container, consume-then-reschedule #1369
    C3 — delete the six h2.rs container sites #1369, inside C2
    C4 — the simulator, sim/tests/h2_simulation.rs #1485

    The timer boundary itself is verifiable rather than asserted: every remaining crate::timer / TimeoutContainer mention in h2.rs and h1.rs is a doc comment — 6 and 5 respectively, zero code.

    What C4 delivers

    Byte-in/byte-out of ConnectionH2 as a server under a seeded action grammar, with read fragmentation down to one octet, partial writes down to one octet, and a 1% buggify wire truncation. Four properties, each seen red under a named production mutation: determinism (same seed identical, four seeds ≥2 distinct fingerprints so a constant trace cannot pass vacuously), the duration_to_tick earliness window probed at every millisecond of [0, 99], the concurrent-stream ceiling under adversarial interleaving, and per-stream credit attribution.

    Writing the determinism guard found a real clock leak: ConnectionH2::new makes the one wall-clock Instant::now() the core still performs, and the connection deadline inherited it — enough to flip a millisecond boundary against the harness's own clock. Re-armed through Connection::set_timeout_duration at the injected base.

    Three production defects it found

    What C4 does not cover, and why

    The adapter half of the timer — Mux::sync_timeout's re-validation, which is the actual C1 change — is not simulated. Mux::ready_inner and timeout_inner read Instant::now() and Timer::poll_to is private, so driving it from sim/ would be a host-clock read wearing a virtual clock's clothes. The core-side half is what C4's tick-boundary property adds; the adapter half remains covered by mux/mod.rs's own tests.

    Making it simulatable needs a lib/ change — inject now into timeout_inner/sync_timeout and make Timer::poll_to reachable. That is flagged rather than attempted, and is not part of this issue.

    Now that #1489 is fixed, the connection-level idle deadline assertion the simulator deliberately omitted — because asserting the old behaviour would have certified the gap — becomes available to sim/.

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