Repository navigation
perf(mux): stop spinning empty writes on every draining H2 session - #1645
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 03:27
FlorentinDUBOIS
force-pushed
the
perf/drain-pass-cost
branch
from
September 28, 2026 04:41
b8ddabf to
51547b6
Compare
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
force-pushed
the
perf/drain-pass-cost
branch
from
September 28, 2026 05:25
51547b6 to
f6a6e6a
Compare
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.
What
During a soft stop,
Server::shut_down_sessionscallsshutting_down()on every draining session at each 100 ms tick. For an H2 frontend,Mux::drive_frontend_shutdown_ioforces onewritable()pass, then kept looping until the WRITABLE event bit cleared. The forced pass sets that bit itself (signal_pending_write), andwritable()clears it only onWouldBlock. For an idle draining session with a writable socket, the bit never cleared, so every tick ran allMAX_LOOP_ITERATIONS(10 000) emptywritable()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:
perfis absent, and samply needsperf_event_paranoid <= 1where 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 thatshut_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:Before/after. Thread CPU time of one
shut_down_sessions()call ontest_h2_continuation_survives_a_graceful_drain_mid_reassembly, run alone in an isolated network namespace, 3 runs per cell:51547b63)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_tickbuilds an idle draining H2Muxwith a live stream and budget left, runs oneshutting_down()pass, and countswritable()calls through acfg(test)thread-local counter. With the old exit condition restored: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_secondskeeps its 5 s default, and the test is unchanged.main@3a79685e1c4b4fbetest_h2_continuation_survives_a_graceful_drain_mid_reassembly(#1623)test_h2_graceful_shutdown_completes_large_transfere2e_h2_flow_per_stream_idle_timeouttest_h1_to_h2_basic_requesttest_rewrite_path_h1_h2test_h1_to_h2_backend_smoketest_h2_goaway_graceful_draintest_h2_graceful_shutdown_deadline_configurable_shorttest_h2_flood_threshold_live_patchtest_h2_settings_ack_timeout_goaways_the_frontend(#1624)After rebasing onto
85f0ae33, two more runs of the same command on51547b63passed the whole suite both times (249/249). After rebasing onto96b4bca9(#1640), one more run onf6a6e6a7: 254/254. The singletest_h2_flood_threshold_live_patchfailure 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_frontendis 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_patchfailed 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_writereturnsRetainPendingBack(invariant 16) without settingexpect_write, sohas_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 whilefrontend.readiness().filter_interest()is non-empty, andRetainPendingBackkeeps bothinterestand thesignal_pending_writeevent bit. Soready()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, ion one connection, 1 MiB bodies (64 DATA frames each), isolated namespace, default 5 s drain budget. Three runs per tree.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 inLIFECYCLE.md, 1 indoc/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/mainpasses. CHANGELOG entry under Fixed.Validation
cargo build --locked,cargo clippy --all-targets --locked -- -D warnings,cargo +nightly fmt --all -- --check: okcargo test --workspace --lib --locked --exclude sozu-e2e: ok (113 + 184 + 1299)check_doc_citations.py:--self-testexit 0; broken fixture exit 1 with the failure report;--base origin/mainexit 0; plain run exit 0sozu_e2e h2_ --skip tests::fuzz_tests::onf6a6e6a7(isolated namespace): 246 passedmain.Closes #1623
Signed-off-by: Florentin Dubois [email protected]