Repository navigation
feat(mux): give the H2 core one clock snapshot per pass - #1358
Merged
Merged
Conversation
Every time-based decision in the H2 core now reads `self.now`; only `Mux` samples the clock. `Mux` takes one `Instant::now()` per outer `ready()` pass and at the top of `timeout` and `shutting_down`, stores it on `Context.now`, and `ConnectionH2` mirrors it into its own `now` field at each public entry. `graceful_goaway` takes `now` as a parameter; the flood detector takes it too, so `H2FloodDetector::window_start` could become private. Twenty-one production real-clock reads in `lib/src/protocol/mux/h2.rs` became snapshot reads. `Instant` stays the type: no `Clock` trait, no newtype, no new dependency. `check_flood_or_return!` grew a `$self.now` argument, which left its ten call sites untouched. `SessionMetrics` and `TimeoutContainer`/`TIMER` deliberately keep reading the real clock. Metrics want true elapsed time, and the container is the embedder's timer that the core never reads. Timer OWNERSHIP is a separate question and is not touched here. BEHAVIOUR: every deadline armed or evaluated inside a pass is now accurate to within that pass, in EITHER direction. Arm sites run at arbitrary depth into a pass while evaluation sites run at its top — `cancel_timed_out_streams` is the first thing `readable` does — so a stamp can be older than the event it records and a deadline can fire up to one pass early as well as late. Before this change both ends read the real clock and the comparison was exact. The flood and back-pressure windows are fail-closed WITHIN a pass, because `now` is constant across it and the window can therefore decay at most once per pass instead of repeatedly under a burst; their boundary still carries the same one-pass error as every other deadline. No wire byte, metric, configuration key or CLI flag changes. `Mux::shutting_down` samples the clock itself, and that line is load-bearing rather than belt-and-braces. `shut_down_sessions()` drives it from outside `ready()`, and it calls `graceful_shutdown_deadline_elapsed()` unconditionally while calling `graceful_goaway` only on the not-yet-draining branch. Without its own sample, a silently draining session would evaluate its forced-close budget against the snapshot of its last `ready()` pass forever and the budget would never expire — unbounded lateness, the opposite of the guarantee above. An earlier draft also mirrored that sample into `ConnectionH2::now`; that line was dead, because `drive_frontend_shutdown_io` always reaches `readable()` for an H2 frontend (`force_h2_read` is unconditionally true, so the early return above it cannot fire) and `readable` overwrites the field from `context.now` before the deadline check reads it. Six unit tests and two end-to-end tests, each seen red under a named mutation before it was allowed to pass. The end-to-end pair covers what the wire can actually falsify: that the flood window DECAYS between bursts, which is the false-positive half of the CVE flood control and which nothing tested before — the existing flood tests only ever prove a threshold trips — and that the SETTINGS-ACK watchdog GOAWAYs with `SETTINGS_TIMEOUT` (0x4) at its deadline and not before. `e2e/COVERAGE.md` records the bound honestly: WHICH clock a deadline reads is not falsifiable end to end, so reverting a `self.now` read to `Instant::now()` leaves both end-to-end tests green. That property belongs to the unit tests that inject instants, and they are named there. Invariant 20 in `LIFECYCLE.md` carries the reviewer check as `grep -nE 'Instant::now|SystemTime::now|\.elapsed\(\)'`. The parentheses are deliberately absent: the bare-function-reference form is what hid the twenty-first site, `.or_insert_with(Instant::now)`, which a grep for `Instant::now()` does not match. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 19, 2026 14:14
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.
Answers Q2 and Q13 of #1337. Q3 (timer ownership) is deliberately untouched — see the comment on that issue.
Every time-based decision in the H2 core now reads
self.now; onlyMuxsamples the clock.Muxtakes oneInstant::now()per outerready()pass and at the top oftimeout/shutting_down, stores it onContext.now, andConnectionH2mirrors it at each public entry. Twenty-one production real-clock reads inh2.rsbecame snapshot reads.Instantstays the type — noClocktrait, no newtype, no new dependency.check_flood_or_return!gained a$self.nowargument, leaving its ten call sites untouched.H2FloodDetector::window_startis now private.Deliberately still on the real clock:
SessionMetrics(metrics want true elapsed) andTimeoutContainer/TIMER(the embedder's timer; the core never reads it).Behaviour
Every deadline armed or evaluated inside a pass is accurate to within that pass, in either direction.
Arm sites run at arbitrary depth into a pass; evaluation sites run at its top —
cancel_timed_out_streamsis the first thingreadabledoes. So a stamp can be older than the event it records, and a deadline can fire up to one pass early as well as late. Before this change both ends read the real clock and the comparison was exact. An earlier draft of this changeset claimed "bounded lateness, never earliness"; that was wrong and the claim is corrected everywhere it appeared.The flood and back-pressure windows are fail-closed within a pass:
nowis constant across it, so the window decays at most once per pass instead of repeatedly under a burst. Their boundary still carries the same one-pass error as every other deadline.No wire byte, metric, configuration key or CLI flag changes.
Mux::shutting_downsamples the clock itselfThat line is load-bearing, not belt-and-braces.
shut_down_sessions()drivesshutting_downfrom outsideready(), and it callsgraceful_shutdown_deadline_elapsed()unconditionally while callinggraceful_goawayonly on the not-yet-draining branch. Without its own sample, a silently draining session evaluates its forced-close budget against the snapshot of its lastready()pass forever and the budget never expires — unbounded lateness, the opposite of the guarantee above.An earlier draft also mirrored that sample into
ConnectionH2::now. That line was dead:drive_frontend_shutdown_ioalways reachesreadable()for an H2 frontend, becauseforce_h2_readis unconditionally true so the early return above it cannot fire, andreadableoverwrites the field fromcontext.nowbefore the deadline check reads it. Removing it also removed the last out-of-module writer, soConnectionH2::nowispub(super).Tests
Six unit tests and two end-to-end tests, each seen red under a named mutation before it was allowed to pass. Lib suite 872 → 878.
The end-to-end pair covers what the wire can actually falsify:
GOAWAY ENHANCE_YOUR_CALM.SETTINGS_TIMEOUT(0x4) at its deadline and not before.h2_tests.rs::test_h2_settings_ack_timeoutcovers the backend side but asserts onlyverify_sozu_alive, so it stays green with the deadline deleted entirely — andH2_ERROR_SETTINGS_TIMEOUTath2_tests.rs:2936is declared and used nowhere, the residue of an assertion never written.e2e/COVERAGE.mdrecords the bound honestly: which clock a deadline reads is not falsifiable end to end. Reverting aself.nowread toInstant::now()leaves both e2e tests green, because the behaviour is identical — it just reads a second clock. That property belongs to the unit tests that inject instants, and they are named there.The reviewer check
Invariant 20 in
LIFECYCLE.mdisgrep -nE 'Instant::now|SystemTime::now|\.elapsed\(\)'. The parentheses are deliberately absent: the bare-function-reference form is what hid the twenty-first site,.or_insert_with(Instant::now), which a grep forInstant::now()does not match.