Skip to content

feat(mux): give the H2 core one clock snapshot per pass - #1358

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
feat/h2-clock-hygiene
Sep 19, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
feat/h2-clock-hygiene

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

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; only Mux samples the clock. Mux takes one Instant::now() per outer ready() pass and at the top of timeout/shutting_down, stores it on Context.now, and ConnectionH2 mirrors it at each public entry. Twenty-one production real-clock reads in h2.rs became snapshot reads.

Instant stays the type — no Clock trait, no newtype, no new dependency. check_flood_or_return! gained a $self.now argument, leaving its ten call sites untouched. H2FloodDetector::window_start is now private.

Deliberately still on the real clock: SessionMetrics (metrics want true elapsed) and TimeoutContainer/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_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. 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: now is 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_down samples the clock itself

That line is load-bearing, not belt-and-braces. shut_down_sessions() drives shutting_down 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 evaluates its forced-close budget against the snapshot of its last ready() 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_io always reaches readable() for an H2 frontend, because 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. Removing it also removed the last out-of-module writer, so ConnectionH2::now is pub(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:

  • the flood window DECAYS between bursts — the false-positive half of the CVE flood control. Nothing tested this before; the existing flood tests only ever prove a threshold trips. Frozen window ⇒ exactly 100 PING ACKs then GOAWAY ENHANCE_YOUR_CALM.
  • the SETTINGS-ACK watchdog GOAWAYs with SETTINGS_TIMEOUT (0x4) at its deadline and not before. h2_tests.rs::test_h2_settings_ack_timeout covers the backend side but asserts only verify_sozu_alive, so it stays green with the deadline deleted entirely — and H2_ERROR_SETTINGS_TIMEOUT at h2_tests.rs:2936 is declared and used nowhere, the residue of an assertion never written.

e2e/COVERAGE.md records the bound honestly: which clock a deadline reads is not falsifiable end to end. Reverting a self.now read to Instant::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.md is 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.

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
FlorentinDUBOIS merged commit 8be5167 into main Sep 19, 2026
23 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the feat/h2-clock-hygiene branch September 19, 2026 17:26
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