Skip to content

test(mux-h2): drive a real TLS close_notify and assert the peer decrypts it - #1508

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
test/h2-close-notify-coverage
Sep 24, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
test/h2-close-notify-coverage

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

What

Closes #1498.

ConnectionH2::begin_tls_close is the seam that reaches FrontRustls::socket_close, which is
rustls::ServerConnection::send_close_notify. It had zero coverage. This adds one test that
drives a real close_notify over a real FrontRustls and asserts on the bytes the peer decrypts.

Test-only: the entire diff sits inside #[cfg(test)] mod tests (one hunk at h2.rs:13333; the
module opens at 6779). sozu-lib built as a dependency is byte-identical to 0dddedf2, so no
sozu-e2e or sozu-sim behaviour can change.

The gap, reproduced

Mutating begin_tls_close to {} on a detached 0dddedf2:

mutation cargo test -p sozu-lib at 0dddedf2 (baseline 1114)
tls_wants_write() -> false 1104 passed; 10 failed
flush_tls_records() no-op 1110 passed; 4 failed
begin_tls_close() -> {} 1114 passed; 0 failed

The two sibling rows were measured on this branch (baseline 1115 passed), where they read
1104 passed; 11 failed and 1110 passed; 5 failed; the counts above subtract the one failure
that is the test added here. Every row is self-consistent against its own baseline.

begin_tls_close also reddened no sozu-e2e test on the mutation. That cell was re-measured
on a mutated pristine 0dddedf2 in a verified-quiet window: 447 passed; 0 failed; 4 filtered out, first try. The earlier
446 passed; 1 failed reading at 42ed6bac, whose single failure was the load-induced
test_redirect_permanent_h2_skips_backend, no longer carries the claim.

The hole is structural, not an oversight. Every handler the suite drove a close over —
mio::net::TcpStream and the BackpressuredTlsSocket fixture — inherits
SocketHandler::socket_close's empty trait default, so the real seam and a no-op are
indistinguishable to all of them. FrontRustls is the only implementation in the tree that
overrides it.

#1484's coverage audit reached the same conclusion from the other direction, listing
initiate_close_notify among the socket_wants_write branches its six
ConnectionH2<FrontRustls> tests could not reach — reason: "needs a driven TLS close_notify".

The test

a_frontend_timeout_makes_the_peer_receive_a_tls_close_notify, built on the existing
rustls_h2_connection / handshaken_front_rustls fixture from #1484/#1491. It drives the
production route end to end:

Mux::timeout
  -> Mux::delay_close_for_frontend_flush("timeout")
    -> Connection::initiate_close_notify
      -> ConnectionH2::initiate_close_notify
        -> begin_tls_close
          -> FrontRustls::socket_close
            -> rustls::ServerConnection::send_close_notify

then flushes with the same Connection::flush_zero_buffer that Mux::shutting_down_inner calls
for exactly this purpose, on a connection whose zero buffer is empty so nothing but the alert
reaches the wire.

The assertion is on what the peer's real rustls::ClientConnection decrypts, not on a FIN:

  • IoState::peer_has_closed() — true only once a close_notify alert has been processed;
  • Reader::read answering Ok(0) — rustls's check_no_bytes_state yields Ok(0) only for a
    stream closed with a close_notify, UnexpectedEof("peer closed connection without sending TLS close_notify") for a bare FIN, and WouldBlock for neither;
  • received.is_empty() — no application plaintext accompanied the alert.

That second witness is the point. An assertion that a FIN had arrived would have passed against
the mutant, which still closes the socket. Likewise, close_notify_sent is still set by the
mutant, so no assertion on that flag could discriminate — only the bytes the peer receives can.

Seen red before it was trusted. With begin_tls_close mutated to {} on this branch:

test result: FAILED. 1114 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
thread '...a_frontend_timeout_makes_the_peer_receive_a_tls_close_notify' panicked at
lib/src/protocol/mux/h2.rs:13461:9:
the peer must decrypt a TLS close_notify alert: without it a client cannot tell this orderly
shutdown from a stream truncated mid-response, and reports a protocol error instead of a clean end

Restored and re-run: test result: ok. 1115 passed; 0 failed. Both cycles were run on the exact
bytes that ship, and the restore was proved byte-identical by md5
(916da773316f2ec9df8352b269619b52 before and after).

Correction to the issue's reachability claim

#1498 says ConnectionH2::initiate_close_notify "is reached from Mux::shutting_down via
self.frontend.initiate_close_notify()". It is not. Mux::delay_close_for_frontend_flush
(mux/mod.rs:966) is the sole caller of Connection::initiate_close_notify, and it is itself
called from five sites — four in Mux::ready_inner (frontend HUP, frontend readable-close,
backend close, frontend writable-close) and one in Mux::timeout_inner.
Mux::shutting_down_inner never asks for the alert; it drives frontend I/O and flushes records
the alert has already generated, which is why flush_zero_buffer is the right flush to use here.

The timeout site was chosen over the four ready_inner ones only because those need a
Rc<RefCell<dyn ProxySession>> and a dyn L7Proxy; the seam under test is identical on all five.

Not included: the H1-mirroring debug_assert!s — blocked

ConnectionH1::initiate_close_notify guards two invariants that H2 guards neither of, and
mirroring them onto H2 was intended here. It is blocked by line-pinned documentation
citations
, not by disagreement — the patch is written, tested and reverted:

             return false;
         }
+        // Past the guard we are always server-side; close_notify is a
+        // frontend-only TLS concern. Mirrors the same assertion on
+        // `ConnectionH1::initiate_close_notify`.
+        debug_assert!(
+            self.position.is_server(),
+            "initiate_close_notify past the guard must be server-side"
+        );
         if !self.close_notify_sent {
             trace!("{} H2 initiating CLOSE_NOTIFY", log_context!(self));
             self.begin_tls_close();
             self.close_notify_sent = true;
         }
+        // `close_notify` is monotone: once requested it stays sent for the
+        // connection's lifetime (a second send would corrupt the TLS stream).
+        debug_assert!(
+            self.close_notify_sent,
+            "close_notify_sent must be set once initiate_close_notify has run"
+        );
         if self.tls_wants_write() {

Those lines (shown trimmed of their explanatory comments; 21 lines as written) land at
h2.rs:1416, which shifts every cited line below them and reddens
python3 .github/scripts/check_doc_citations.py --root . on nine citations:

  • lib/src/protocol/mux/LIFECYCLE.md:1342 — invariant 8 cites h2.rs:4874-4881 (and
    h2.rs:6699-6706); after the shift 4874 is blank. The companion 6699-6706 drifts onto
    different non-blank lines and silently resolves, which is exactly the floor the script's own
    output warns about.
  • doc/h2_mux_internals.md — 8 pinned code blocks (lines 536, 560, 592, 657, 744, 1054, 1207,
    1215) whose strict content match fails on any shift.

Both files are being worked on concurrently, so repointing them here would collide — and
checking those branches shows the blocker survives their merge, so this is not simply a
"wait for them" follow-up:

  • docs/lifecycle-citation-repair (07574ad1) still spells invariant 8 with line numbers,
    merely renumbered: `h2.rs:4922-4930`, `h2.rs:6681-6689`.
  • docs/h2-mux-internals-snippet-sweep (92c9ea75) carries 17 pinned h2.rs:NNN blocks,
    up from 13 at 42ed6bac.

So landing the assertions needs its own step, ordered after both merge: repoint whatever the
pinned blocks and invariant 8 then say, or — better — convert invariant 8 to symbols. Shrinking the
assertions until 4874 happens to land on a non-blank line would make the citation resolve to the
wrong code, which is gaming the gate rather than fixing it. The assertions add no discriminating
power to the test above, so dropping them costs this changeset nothing.

Worth noting while the citation work is in flight: LIFECYCLE.md invariant 7, immediately
above, ends with "Cited by symbol rather than by line on purpose … Do not convert it back." —
while invariant 8 directly beneath it is line-pinned into h2.rs twice, and CLAUDE.md states
the repo rule as "A citation in a comment names a symbol, never a line." Converting invariant
8's two citations to symbols (ConnectionH2::create_stream, ConnectionH2::start_stream) would
unblock this and every future insertion into h2.rs. Reported, not fixed, per scope.

Validation

All on 4345dd68:

command result
cargo +nightly fmt --all -- --check exit 0
cargo build --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd exit 0
cargo clippy --all-targets … -- -D warnings exit 0
cargo test -p sozu-lib … 1115 passed; 0 failed (baseline 1114)
cargo test -p sozu … 113 passed; 0 failed
cargo test -p sozu-e2e … --skip tests::fuzz_tests:: 447 passed; 0 failed
RUSTFLAGS="--cfg tokio_unstable" cargo test -p sozu-sim --locked 12 + 6 + 6 + 6 passed; 0 failed
RUSTFLAGS="--cfg tokio_unstable" cargo clippy -p sozu-sim --all-targets -- -D warnings exit 0
check_doc_citations.py --root . exit 0
check_doc_citations.py --base 0dddedf2 exit 0 (306 cited lines compared, none changed text, 0 exempt)
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked exit 0
cargo bench -p sozu-lib --bench h2_stream_table -- --test exit 0 (60 Success)

Test-environment note (not a regression)

The e2e suite was run six times on this box while other work saturated it. At loadavg 40–50 it
reddened with a different test each time; at loadavg ≤ 20 it is green:

tree loadavg result
branch (with asserts) 14 → 51 test_issue_806, test_upgrade_keepalive_reconnect
branch (with asserts) 40 → 25 test_async
branch (with asserts) 33 → 37 test_h2_continuation_survives_a_graceful_drain_mid_reassembly
branch (with asserts) 12 → 20 447 passed; 0 failed
base 42ed6bac 33 / 37 447 passed; 0 failed (×2)
base + mutation 40 → 32 test_redirect_permanent_h2_skips_backend
branch, as shipped 14 → 7 447 passed; 0 failed

Every one of those tests passes 3/3 when run isolated. The rotating cast (rather than a fixed
test), the green runs at low load, and the fact that a base-derived tree reddened the same way
all point at host contention. test_issue_806 reports its own cause —
reconnecting to another backend took 201.01866ms, over the 100ms budget. Base was never run at
loadavg ≥ 40, so that gap is why the decisive argument is the machine property: the shipped diff
is entirely inside #[cfg(test)] mod tests, which is not compiled into a dependency build, so the
sozu-lib that sozu-e2e links is source-identical to 0dddedf2.

`ConnectionH2::begin_tls_close` reaches `FrontRustls::socket_close`, which
is `rustls::ServerConnection::send_close_notify`. It had no coverage at
all. Measured at 0dddedf: replacing its body with `{}` left `cargo test
-p sozu-lib` at `1114 passed; 0 failed` and reddened no `sozu-e2e` test
either, while the same mutation on either of its two siblings from the
same extraction reddens at once. Measured against this changeset's own
`1115 passed` baseline: `tls_wants_write() -> false` gives `1104 passed;
11 failed` and a no-op `flush_tls_records()` gives `1110 passed; 5
failed`, ten and four of those respectively being tests other than the
one added here.

The hole was structural rather than an oversight. Every handler the suite
drove a close over -- `mio::net::TcpStream` and the
`BackpressuredTlsSocket` fixture -- inherits `SocketHandler::socket_close`'s
empty trait default, so the real seam and a no-op are the same thing to
all of them, and `FrontRustls` is the only implementation in the tree that
overrides it. #1484's coverage audit reached the same conclusion from the
other direction, listing `initiate_close_notify` among the
`socket_wants_write` branches its six `ConnectionH2<FrontRustls>` tests
could not reach, for want of a driven close_notify.

`a_frontend_timeout_makes_the_peer_receive_a_tls_close_notify` drives the
production route end to end over the existing `rustls_h2_connection`
fixture: `Mux::timeout` -> `Mux::delay_close_for_frontend_flush` ->
`Connection::initiate_close_notify` ->
`ConnectionH2::initiate_close_notify` -> `begin_tls_close` ->
`send_close_notify`, then flushes through the same
`Connection::flush_zero_buffer` that `Mux::shutting_down_inner` calls for
this purpose. It asserts on what the peer's real
`rustls::ClientConnection` decrypts: `IoState::peer_has_closed()`, and
`Reader::read` answering `Ok(0)` rather than `UnexpectedEof`. That second
witness is the whole point -- rustls answers `Ok(0)` only for a stream
closed with a close_notify and `UnexpectedEof` for a bare FIN, so an
assertion that a FIN had arrived would have passed against the mutant.

Seen red before it was trusted: `1114 passed; 1 failed`, this test and
nothing else, failing on `the peer must decrypt a TLS close_notify alert`.

Test-only. The whole diff lies inside `#[cfg(test)] mod tests`, so
`sozu-lib` built as a dependency is byte-identical to 0dddedf and no
`sozu-e2e` behaviour can change.

Refs: #1498
Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the test/h2-close-notify-coverage branch from 7335ffa to 4345dd6 Compare September 24, 2026 17:00
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 68523cc into main Sep 24, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the test/h2-close-notify-coverage branch September 24, 2026 17:48
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.

mux-h2: the TLS close-notify path has no coverage at all — mutating it to a no-op leaves 1107 lib and 447 e2e tests green

1 participant