Skip to content

fix(mux-h2): arm the frontend deadline on stream bytes, not on an ACK-only pass - #1494

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-ack-only-pass-liveness
Sep 24, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-ack-only-pass-liveness

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Refs #1489.

ConnectionH2::poll_read_target documents, in prose, that PING, WINDOW_UPDATE and SETTINGS must not reset the frontend timeout — "otherwise a peer sending periodic PINGs prevents timeout detection on stuck sessions". The read path honoured it. write_streams opened with an unconditional arm_timeout(), so any pass with something to send reset it, including a pass whose only output is an acknowledgement.

Reproduced before touching anything, front_timeout 600 s, one stream at 10 ms. t0 + 600.01 s is the correct answer — armed at 10 ms:

event before after
HEADERS @ 10 ms t0 + 600.01 s ✓ t0 + 600.01 s
PING @ 10 s t0 + 610 s t0 + 600.01 s
empty pass @ 20 s t0 + 610 s t0 + 600.01 s
WINDOW_UPDATE @ 30 s t0 + 610 s t0 + 600.01 s
SETTINGS @ 40 s t0 + 640 s t0 + 600.01 s
PING @ 500 s t0 + 1100 s t0 + 600.01 s

The three that moved are exactly the three that queue an acknowledgement. WINDOW_UPDATE queues nothing, which is why it correctly never moved.

The harness had to wait on bytes actually reaching the connection socket via peek; without that the loopback read quiesces on a "not yet" WouldBlock and the last two rows silently measure a frame that never arrived — which is how the first two reproduction attempts under-reported the defect.

The rule, and where it lives

Arm the frontend deadline on application-stream activity in either direction. Inbound is unchanged (DATA payload, HEADERS). Outbound becomes: a write-pass transmit that moved a real stream's bytes.

The seam is the H2StreamId::Other arm of ConnectionH2::handle_write, gated on size > 0, replacing the unconditional arm at the top of write_streams.

  • handle_write is already where the pass classifies what it emitted — it decides resume_bytes vs stream_bytes at that point — so the rule reads literally at the site rather than being reconstructed from context.
  • Both halves of the gate are load-bearing and neither is implied by reaching the write pass: the stream id separates a stream's bytes from self.zero's, and size > 0 separates bytes the socket took from an offer it refused.
  • finalize_write would have been wrong: resume_bytes deliberately never reaches it, and three Done terminators bypass it entirely.
  • arm_timeout() reads self.now, the snapshot writable() adopts at pass entry, so arming per transmit computes the same instant pass entry would have. The gate is the whole change, not the moment.

Production arm_timeout() call sites stay at 3, which is what LIFECYCLE §9 invariant 9 asserts.

A larger finding than the issue described

Close and drain paths were checked and need no special case — they keep their deadline the way every other pass does, by flushing a stream's bytes, and Mux::timeout_inner already re-arms explicitly on each branch that keeps a session alive.

What does end is this: Mux::drive_frontend_shutdown_io sets force_h2_write unconditionally for an H2 frontend, so a draining connection still in H2State::Header reached write_streams on every shutting-down poll. Today's unconditional arm therefore made a draining session immune to front_timeout for the entire drain — byte or no byte. That is now bounded by graceful_shutdown_deadline and by Mux::timeout's own draining branch.

Covered by a_draining_write_pass_that_moves_stream_bytes_still_arms_the_deadline, which asserts both legs.

The per-stream idle deadline is untouched.

Direction disclosure

handle_write serves backend H2 too (Position::Client), so an upstream sending keepalive PINGs no longer renews backend_timeout either — a stuck upstream that only pings now times out. Response HEADERS/DATA reads still renew it. The same rule applied consistently, e2e green, and LIFECYCLE invariant 22's prose updated to match.

Two independent reverts, which is what rules out a vacuous pass

A test asserting only "the deadline did not move" passes against a build that never arms — the opposite defect, and worse, since it turns front_timeout into an unconditional session cap. So each of the four tests pairs a negative leg with a positive leg on the same connection, and two different reverts redden them in two different places:

Revert A — self.arm_timeout(); back at the top of write_streams → 1100 passed; 4 failed, each on its negative leg:

a_ping_only_peer_…       left: 610s   (right 600.01s)
a_settings_only_peer_…   left: 640s   (right 600.01s)
a_write_pass_that_moves_stream_bytes_arms_…   left: 610s (right 600s)
a_draining_write_pass_…  left: 610s   (right 600s)

Revert B — if size > 0 → if false, a build that never arms on the write path → 1100 passed; 4 failed, all four on their positive legs: left: 600.01s (right 1130s), left: 600.01s (right 650s), left: 600s (right 620s) ×2.

Test-shaping check: the production change is three lines. The only literal shared with test code is 0, in assert_eq!(written, 0, …) — a behavioural "the pass moved no byte" premise. Assertions read poll_timeout() offsets, expect_write and metrics.bout.

Documentation — the inconsistency was the bug

  • LIFECYCLE §9 invariant 9 — its sentence "A control frame reaches none of the three" was false; the third site was the top of write_streams. Rewritten with why reaching write_streams is not evidence of application data.
  • LIFECYCLE §7.1 — the reset list now names the write site, and the "control frames deliberately do not reset it" claim now says it was prose-only until this fix.
  • LIFECYCLE invariant 22 and its verbatim duplicate in mod.rs — both named write_streams as an arm site.
  • h2.rs — poll_read_target's reset list, and the writable() dispatch comment, which claimed "Reset the timeout here … so that control frame writes don't reset it": precisely the thing it failed to do.
  • doc/h2_mux_internals.md — writable() dispatch and handle_write sections.

Verification

cargo +nightly fmt --all -- --check                     exit 0
cargo build  --locked --no-default-features --features crypto-ring,…   exit 0
cargo clippy --all-targets --locked … -- -D warnings    exit 0, 0 diagnostics
cargo test -p sozu-lib --locked …    ok. 1104 passed; 0 failed     (1100 + the 4 added)
cargo test -p sozu     --locked …    ok. 113 passed; 0 failed
cargo test -p sozu-e2e --locked … -- --skip tests::fuzz_tests::
                                     ok. 446 passed; 0 failed
check_doc_citations.py --root .      208 citations, 527 test names, 16/16 pinned verbatim
check_doc_citations.py --base 9472cd96   259 compared, none changed
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked   exit 0

No test deleted or ignored, no assertion loosened, no #[allow], no lint escape.

Disclosed: 64 exempt citation spans, audited rather than accepted

--base reports 64 re-anchored spans exempt from the comparison and never checked at all — not 0. The line insertions in h2.rs/mod.rs drifted 49 pre-existing citations that were intact at base. --show buckets them exactly:

  • 15 are endpoints of the 8 repointed pinned code blocks in doc/h2_mux_internals.md, independently covered by rule 4 (all 16 pinned blocks quote their cited lines verbatim).
  • 49 are prose citation endpoints, renumbered mechanically from a difflib base→head line map. Verified independently: for all 50 renumbered endpoints the head line text is byte-identical to the base line text, whitespace-stripped — 0 mismatches.

Two citations were removed rather than renumbered, so nothing remains to compare: LIFECYCLE §7.1's h2.rs:5257 / h2.rs:1835-1841 became symbol citations, and h2_mux_internals.md's h2.rs:4142 became cancel_timed_out_streams plus the file — the script's own recommendation, since it had drifted onto a blank line. No new file.rs:NNN citation was added anywhere.

Follow-up, not done

The H2 simulator (#1485) deliberately does not assert the connection-level idle deadline, because "asserting today's behaviour would certify the gap". The gap is now closed, so that assertion becomes available to sim/.

…te pass

`ConnectionH2::poll_read_target` documents that PING, WINDOW_UPDATE and
SETTINGS must not reset the frontend timeout, "otherwise a peer sending
periodic PINGs prevents timeout detection on stuck sessions". The read
path honoured it, arming only on DATA payload and on HEADERS. The write
path undid it.

`ConnectionH2::write_streams` armed the connection deadline as its first
statement, and `writable()` dispatches into that function for every
proxying state, so reaching it never meant application data was being
written. A connection whose only queued output was the PING ACK or
SETTINGS ACK that `flush_pending_control_frames` had just drained sits in
`H2State::Header` like any other, falls through the preamble's `None`,
and armed. Measured with `front_timeout` 600 s and one stream opened at
10 ms, a PING at 10 s, a SETTINGS at 40 s and a PING at 500 s each pushed
the deadline a further full timeout out; WINDOW_UPDATE queues no
acknowledgement, gave the write path no reason to run, and correctly did
not move it. `front_timeout` therefore never fired on a PING-only peer.

The arm moves to the `H2StreamId::Other` arm of
`ConnectionH2::handle_write`, gated on `size > 0`. Both halves are
load-bearing and neither is implied by reaching the write pass: the
stream id is what makes the bytes a stream's rather than `self.zero`'s,
and the size is what makes them bytes the socket actually took.
`arm_timeout` reads the pass clock snapshot, so arming per transmit lands
on the same instant pass entry would have computed - the gate is the
whole change, not the moment. The call-site count stays at three, which
is what LIFECYCLE §9 invariant 9 states.

The per-stream idle deadline is a separate mechanism and is untouched.
The close and drain paths keep their deadline the way every other pass
does, by flushing a stream's bytes: `Mux::timeout` already re-arms
explicitly on each branch that keeps a session alive, and
`H2State::GoAway` / `H2State::Error` never reach the stream-write path.
What ends is `Mux::drive_frontend_shutdown_io` forcing a write pass on
every shutting-down poll and renewing a draining session for free,
whether or not a byte moved.

Four tests pin it, each pairing its negative with a positive leg on the
same connection, because a build that never armed would satisfy every
"the deadline did not move" assertion while turning `front_timeout` into
an unconditional session cap.

Refs #1489

Signed-off-by: Florentin Dubois <[email protected]>
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