Skip to content

perf(mux): stop spinning empty writes on every draining H2 session - #1645

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/drain-pass-cost
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/drain-pass-cost

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

What

During a soft stop, Server::shut_down_sessions calls shutting_down() on every draining session at each 100 ms tick. For an H2 frontend, Mux::drive_frontend_shutdown_io forces one writable() pass, then kept looping until the WRITABLE event bit cleared. The forced pass sets that bit itself (signal_pending_write), and writable() clears it only on WouldBlock. For an idle draining session with a writable socket, the bit never cleared, so every tick ran all MAX_LOOP_ITERATIONS (10 000) empty writable() calls, for every draining H2 session.

The loop now stops as soon as nothing is queued (has_pending_write()) or the socket blocks. The forced first pass is unchanged. The loop's exit condition dates from 28c6807.

Evidence

Locating it. No sampling profiler could run on this workstation: perf is absent, and samply needs perf_event_paranoid <= 1 where it is 2, with no root. I bisected with thread CPU time (CLOCK_THREAD_CPUTIME_ID) instead, using temporary instrumentation that is not part of this PR. It showed that shut_down_sessions() accounts for the whole drain-pass cost. A per-call counter in the write loop then printed, for all 50 calls on the e2e drain test:

DBGDRIVE iterations=10000 cpu=29.49ms (median) has_pending_write=false event_writable=true

Before/after. Thread CPU time of one shut_down_sessions() call on test_h2_continuation_survives_a_graceful_drain_mid_reassembly, run alone in an isolated network namespace, 3 runs per cell:

build before, median per pass after, median per pass total over 3 runs
release 3.94 ms 7 µs (5 µs, re-measured on 51547b63) 397 ms → 1.3 ms
debug 79.5 ms 116 µs 5.55 s → 15 ms

So the cost is real in release builds. It is paid per draining session and per tick, and every other event on the worker waits behind it: 100 draining sessions cost about 400 ms of CPU per 100 ms tick.

Test seen red. mux::tests::an_idle_draining_h2_session_makes_one_writable_pass_per_shutdown_tick builds an idle draining H2 Mux with a live stream and budget left, runs one shutting_down() pass, and counts writable() calls through a cfg(test) thread-local counter. With the old exit condition restored:

assertion `left == right` failed: one shutdown pass on an idle draining H2 session must make exactly the one forced writable() call, not spin until MAX_LOOP_ITERATIONS (10000); got 10000

Effect on #1623 (budget untouched)

#1623's command, taskset -c 0 sozu_e2e h2 --skip tests::fuzz_tests:: --test-threads=20, 10 runs per tree, each in an isolated network namespace. h2_graceful_shutdown_deadline_seconds keeps its 5 s default, and the test is unchanged.

test main @ 3a79685e this PR @ 1c4b4fbe
test_h2_continuation_survives_a_graceful_drain_mid_reassembly (#1623) failed 10/10 failed 0/10
test_h2_graceful_shutdown_completes_large_transfer failed 10/10 failed 3/10
e2e_h2_flow_per_stream_idle_timeout failed 7/10 failed 2/10
test_h1_to_h2_basic_request failed 6/10 failed 2/10
test_rewrite_path_h1_h2 failed 5/10 failed 1/10
test_h1_to_h2_backend_smoke failed 4/10 failed 2/10
test_h2_goaway_graceful_drain failed 3/10 failed 0/10
test_h2_graceful_shutdown_deadline_configurable_short failed 2/10 failed 0/10
test_h2_flood_threshold_live_patch failed 0/10 failed 1/10
test_h2_settings_ack_timeout_goaways_the_frontend (#1624) failed 10/10 failed 8/10

After rebasing onto 85f0ae33, two more runs of the same command on 51547b63 passed the whole suite both times (249/249). After rebasing onto 96b4bca9 (#1640), one more run on f6a6e6a7: 254/254. The single test_h2_flood_threshold_live_patch failure noted below was checked in review: that test passes 10/10 on both the base and this branch, so it is not a regression.

The two series ran one after the other, under the same ambient machine load. test_h2_settings_ack_timeout_goaways_the_frontend is a separate test bug (its handshake runs under a 100 ms poll timeout), fixed by #1636, which was not yet on this branch for the 10-run series. test_h2_flood_threshold_live_patch failed once in these 10 runs; review then ran it 10/10 green on both trees.

Review question: does an RFC 9218 yield now drain once per tick? (not reproduced)

The concern: on an incremental same-urgency yield, finalize_write returns RetainPendingBack (invariant 16) without setting expect_write, so has_pending_write() is false and the new loop stops after one pass, where the old one kept going.

Reading the code, the state should not be stranded. ready()'s inner loop continues while frontend.readiness().filter_interest() is non-empty, and RetainPendingBack keeps both interest and the signal_pending_write event bit. So ready() resumes the retained writes itself before it returns, and a drain pass starts with no retained work. This is an inference from the code; the recipe below tests it.

Recipe (temporary e2e, not committed): two streams u=3, i on one connection, 1 MiB bodies (64 DATA frames each), isolated namespace, default 5 s drain budget. Three runs per tree.

variant old loop this PR
stream windows at 16 KiB, soft stop lands while both are flow-blocked, then WINDOW_UPDATE on the connection and both streams truncated: 290 to 481 KiB of 1 MiB delivered, cut at 4.65 to 4.98 s full 1 MiB on both, in 90 to 126 ms
windows open, client stops reading so the socket blocks, soft stop, client resumes reading full, 38 to 50 ms full, 37 to 48 ms

So in the scenario that concern describes, the old loop is the one that fails to drain, and this change fixes it.

Docs

The added lines shift lib/src/protocol/mux/mod.rs, which drifts 16 numeric citations (15 in LIFECYCLE.md, 1 in doc/testing.md). Each is replaced by the symbol its prose already names (Mux::ready_inner, Mux::timeout_inner, Mux::shutting_down_inner), so they no longer move with the line numbers. check_doc_citations.py --base origin/main passes. CHANGELOG entry under Fixed.

Validation

  • cargo build --locked, cargo clippy --all-targets --locked -- -D warnings, cargo +nightly fmt --all -- --check: ok
  • cargo test --workspace --lib --locked --exclude sozu-e2e: ok (113 + 184 + 1299)
  • check_doc_citations.py: --self-test exit 0; broken fixture exit 1 with the failure report; --base origin/main exit 0; plain run exit 0
  • sozu_e2e h2_ --skip tests::fuzz_tests:: on f6a6e6a7 (isolated namespace): 246 passed
  • The e2e H2 suite under contention: covered by the 10 runs above. Across those runs, every failure on this branch is in a test that also fails on main.

Closes #1623

Signed-off-by: Florentin Dubois [email protected]

During a soft stop, `Server::shut_down_sessions` calls `shutting_down()`
on every draining session at each 100 ms tick, and
`Mux::drive_frontend_shutdown_io` forces one `writable()` pass on an H2
frontend. Its loop then continued until the WRITABLE event bit cleared.
The forced pass sets that bit itself (`signal_pending_write`), and
`writable()` clears it only on `WouldBlock`, so for an idle draining
session with a writable socket the bit never cleared. Every tick ran all
`MAX_LOOP_ITERATIONS` (10 000) `writable()` calls. Measured with a
per-call counter on the e2e drain test: all 50 calls ran 10 000
iterations, with `has_pending_write=false` and `event_writable=true`.

The loop now stops as soon as nothing is queued or the socket blocks. The
forced first pass is unchanged, so a stream still gets its pass to retire,
emit the GOAWAY or flush TLS records.

Thread CPU time of one `shut_down_sessions()` call, measured on
`test_h2_continuation_survives_a_graceful_drain_mid_reassembly` alone in
an isolated network namespace, 3 runs per build:

  release: median 3.94 ms -> 7 us per pass (total 397 ms -> 1.3 ms)
  debug:   median 79.5 ms -> 116 us per pass (total 5.55 s -> 15 ms)

The cost is paid per draining session and per tick, and every other event
on the worker waits behind it. Under the e2e suite on one CPU it is what
exhausted the default 5 s drain budget in #1623.

`an_idle_draining_h2_session_makes_one_writable_pass_per_shutdown_tick`
counts the `writable()` calls of one pass. It fails with "got 10000" when
the old exit condition is restored.

The added lines shift `lib/src/protocol/mux/mod.rs`, which drifts 16
numeric citations in LIFECYCLE.md and doc/testing.md. Each is replaced by
the symbol its prose already names (`Mux::ready_inner`,
`Mux::timeout_inner`, `Mux::shutting_down_inner`), so they no longer move
with the line numbers.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 3ee0b6d into main Sep 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/drain-pass-cost branch September 28, 2026 06:30
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.

e2e: continuation-during-graceful-drain test fails under CPU contention (single GOAWAY)

1 participant