Skip to content

test(e2e): finish the TLS handshake before the SETTINGS-ACK test polls - #1636

Merged
FlorentinDUBOIS merged 3 commits into
mainfrom
test/e2e-drain-settings-contention
Sep 28, 2026
Merged

FlorentinDUBOIS merged 3 commits into
mainfrom
test/e2e-drain-settings-contention

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

Three independent test-only commits, one per cause. No production code changes. #1623 stays open: see the last section.

#1624: test_h2_settings_ack_timeout_goaways_the_frontend

Cause (a test bug). The test set its 100 ms POLL_READ_TIMEOUT on the socket right after connecting, before the TLS handshake. rustls runs the handshake lazily from the first write_all (Stream::complete_prior_io, rustls 0.23.45), so the preface write waited for the ServerHello under that 100 ms read timeout. An instrumented failing run printed:

DBG SA preface result=Err(Os { code: 11, kind: WouldBlock, .. }) after 111.731718ms handshaking=true

Fix. Complete the handshake explicitly (complete_io while is_handshaking()) under the connection's own timeout, the same one every h2_handshake caller gets. Then set the short poll timeout. The assertions are unchanged.

test_async: count whole keep-alive responses

try_async read each response with a single Client::receive(), which counts one response per read() under the 100 ms timeout that Client::connect installs. A late response goes uncounted, and a split one is counted twice. Client::receive_response reads exactly one Content-Length-framed response up to an explicit deadline and rejects extra bytes. Its unit tests use a scripted Read, so they need no socket or clock, and they fail 3/3 when the reader is reverted to one response per read(). The test was reported to fail once under load with client0 sent: 100, received: 99. Twenty runs of the test alone on 58550dd4, with four busy loops on its CPU, did not reproduce it, so this fix rests on the code path and the unit tests.

backends_test: kernel-chosen port for the mock backend

run_mock_tcp_server bound fixed ports, 127.0.0.1:1236 and 127.0.0.1:3456. Holding 1236 from another process reproduces the reported failure: panicked at lib/src/backends.rs:1255:48: ... AddrInUse. The helper now binds :0 and returns local_addr(). With both former ports held, the 21 backends_test tests pass.

Measurements for #1624

Each run is the issue's command in a private network namespace (unshare -rn, private loopback), so it cannot collide with other e2e processes on the machine. The machine was also loaded by other jobs.

taskset -c 0 sozu_e2e h2 --skip tests::fuzz_tests:: --test-threads=20
tree runs test_h2_settings_ack_timeout_goaways_the_frontend
main @ 58550dd4 10 + 2 failed 12/12
this fix on 58550dd4 10 + 2 failed 0/12
this branch on d5161919 2 failed 0/2

#1623 is still open

test_h2_continuation_survives_a_graceful_drain_mid_reassembly still fails under the same contention. Its cause is known: the default 5 s h2_graceful_shutdown_deadline_seconds budget runs out while the test waits, and the worker force-closes the session with both streams unanswered. The budget runs out that fast because, under this load, one shut_down_sessions() pass takes 2 to 5 s of wall-clock time for 70 to 160 ms of thread CPU time, against about 25 µs for an idle loop pass. That cost is being investigated separately. The fix belongs there, not in the test's budget.

Validation (on d5161919 + these 3 commits)

  • cargo build --locked: ok
  • cargo clippy --all-targets --locked -- -D warnings: ok
  • cargo +nightly fmt --all -- --check: ok
  • cargo test --workspace --lib --locked --exclude sozu-e2e: ok (113 + 184 + 1289)
  • sozu_e2e h2_ --skip tests::fuzz_tests:: (isolated namespace): 240 passed
  • mock::client::tests + tests::tests::test_async: 4 passed
  • 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

No CHANGELOG.md entry: only tests change.

Closes #1624

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

`try_h2_settings_ack_timeout_goaways_the_frontend` installed its 100 ms
`POLL_READ_TIMEOUT` right after connecting, before any TLS handshake had
happened. rustls runs the handshake lazily from the first `write_all`
(`Stream::complete_prior_io`), so the preface write waited for the
ServerHello under that 100 ms read timeout. When the worker took longer
to answer, which is routine when the H2 suite shares one CPU, the write
failed with `WouldBlock` before the SETTINGS exchange had started.
Instrumented run: `preface result=Err(WouldBlock) after 111.7ms
handshaking=true`. Sozu was not at fault.

The test now completes the handshake explicitly under the connection's
own timeout, the same one every `h2_handshake` caller gets. Only then does
it install the short timeout for its quiet-connection polls. The
assertions are unchanged.

Closes #1624

Signed-off-by: Florentin Dubois <[email protected]>
`try_async` read each keep-alive response with a single
`Client::receive()`, which counts one response per `read()` under the
100 ms socket timeout that `Client::connect` installs. doc/testing.md
forbids asserting on a single read, and under load that read counts
wrong. A response arriving after 100 ms is not counted at all, and one
split across two segments is counted twice. `test_async` was reported
to fail once under load with `client0 sent: 100, received: 99`. Twenty
runs of the test alone on 58550dd, with four busy loops on its CPU, did
not reproduce it, so this fix rests on the code path above and the unit
tests below.

`Client::receive_response` reads until it holds exactly one
`Content-Length`-framed response, however many reads that takes, and
counts it only then. An explicit deadline bounds how long a lost
response takes to fail the run. Bytes past the one response are a
framing error, not a second response. `try_async` still asserts that
every client got exactly `nb_requests` responses.

The reader's unit tests use a scripted `Read` (a fragment, `WouldBlock`,
`TimedOut`, the rest), so they need no socket or clock. They fail 3/3
when the reader is reverted to one response per `read()`.

Signed-off-by: Florentin Dubois <[email protected]>
`backends_test::run_mock_tcp_server` bound fixed ports, `127.0.0.1:1236`
and `127.0.0.1:3456`, with `bind(addr).unwrap()`. Any other process
holding one of them, a concurrent `cargo test` of this crate included,
panicked the test with `AddrInUse` before it asserted anything.
Reproduced by holding 127.0.0.1:1236 in a private network namespace:
`panicked at lib/src/backends.rs:1255:48: ... AddrInUse`.

The helper now binds `127.0.0.1:0` and returns `local_addr()`. The two
tests register that address as the backend they expect to retrieve, so
they assert what they did before. With both former ports held, the 21
`backends::backends_test` tests pass.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the test/e2e-drain-settings-contention branch from da46d2e to 895c45f Compare September 28, 2026 00:06
@FlorentinDUBOIS FlorentinDUBOIS changed the title test(e2e): fix the drain and SETTINGS-ACK tests that fail under CPU contention test(e2e): finish the TLS handshake before the SETTINGS-ACK test polls Sep 28, 2026
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 5db53c6 into main Sep 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the test/e2e-drain-settings-contention branch September 28, 2026 01:10
FlorentinDUBOIS added a commit that referenced this pull request Sep 28, 2026
…1645)

## 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]>
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: SETTINGS-ACK deadline test fails to send its preface under CPU contention

1 participant