Skip to content

test(e2e): take the run-queue wait out of the issue 806 reconnect budget - #1818

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
test/deflake-issue-806-and-tls-half-close
Oct 3, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
test/deflake-issue-806-and-tls-half-close

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Summary

test_issue_806 failed intermittently on loaded hosts. Every failure was the 100 ms reconnect budget, and in every one the re-route itself was correct: status 200, and the surviving backend served (1, 1). Per-thread schedstat showed the time went to threads that were runnable but waiting for a CPU. Sōzu was not waiting on anything. The budget was timing the scheduler.

  • e2e/src/sched.rs (new): current_tid and run_queue_delay. The second function sums field 2 of /proc/self/task/<tid>/schedstat ("time spent waiting on a runqueue"). A thread it cannot read counts as zero, so the test subtracts less, never more.
  • Worker::server_tid and BackendHandle::tid record the kernel thread ids.
  • try_backend_stop compares the round trip less the run-queue wait of the client, worker and surviving-backend threads, and prints both numbers. A blocked wait (a sleep, a timer, a pending connect) is not run-queue time, so it stays in the measurement.
  • The AsyncBackend mock now blocks in poll on its listener, its clients and a stop waker, where it used to spin on non-blocking accept/read. A request now wakes it right away, and it no longer holds a CPU for the whole test. Every handler starts with a read, so skipping passes where nothing is readable changes no reply.
  • doc/testing.md: describes the technique. CHANGELOG.md: entry under Fixed.

The assertion and the 100 ms budget are unchanged.

Measurements (60 busy loops on a 20-CPU host, load average 80-110)

runs failed
before (origin/main) 17 / 20, all through the budget branch, raw 117-272 ms
after (final code, batch 1) 0 / 20
after (final code, batch 2) 0 / 20

Two more batches, run with an earlier variant that used a 10 ms poll timeout in place of the waker, also gave 0 / 20 each.

Across the 4 000 trials of the two final batches, the raw round trip went over 100 ms 88 times. The value compared against the budget stayed at or under 3.96 ms (median 0.8 ms, p99 1.9 ms).

Red check (the existing doc's 150 ms sleep before the connect in Mux::dial_backend):

  • idle host: 151.909271ms (152.047532ms less 138.261µs of run-queue wait), red on iteration 1
  • next to 60 busy loops: red on iteration 1 in 10 / 10 runs, compared value 109-151 ms

Known leniency: summing three threads' waits counts twice any wait that overlaps another one. Which threads overlap is inferred, not measured. Under load the sum exceeded the raw round trip by up to 252 ms in one trial of 2 000. That only lowers the compared value, and only on a loaded host; the subtraction is saturating. On an idle host the wait is well under 1 ms.

Validation: cargo +nightly fmt --check, cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings, full e2e suite --features opentelemetry,splice,simd: 622 passed, 0 failed. check_doc_citations.py --base origin/main OK.

Why test_plain_client_half_close_after_request_receives_large_response is not touched

Its intermittent failure (CI run 37019159928, nightly: status_ok=true body=8284694/8388608 ending=closed) comes from a proxy defect. The test is not at fault, so it is left alone here and reported separately.

Reproduced locally under taskset -c 0 with 4 busy loops on the same CPU: 1/30, 1/60, 1/80 and 2/150 failures. With 60 unpinned busy loops: 0/50. An instrumented backend confirmed it read the whole request, wrote all 8 MiB (write_all Ok, 0 unread bytes) and then closed cleanly. Sōzu still sent the client only part of the body before its FIN. The stock mux debug log shows:

MUX CLOSE  readiness=I("-WEH-")&R("R--H-")
Session close with 1 active stream(s)
  active stream[0]: state=Linked(Token(5)) ... POST /large
access log: ... 200 | session close   (8151951 of 8388713 bytes)

A temporary debug line on the MAX_LOOP_ITERATIONS branch of Mux::ready_inner (lib/src/protocol/mux/mod.rs, the bare return SessionResult::Close after the has_pending_write() check) fired on both failing trials, with frontend I(-WEH-)&R(R--H-) and backend I(R-EH-)&R(---H-). Read against the code, this points to three spots:

  1. The backend's FIN has arrived (dead), but the dead && !readable && !has_buffer_pressure guard keeps that backend open while it is under buffer pressure (the client is slow).
  2. Its filter_interest() is ---H-, which is never empty. So all_backends_readiness_are_empty stays false and the inner loop never breaks; it spins up to 10 000 iterations inside one ready() call.
  3. The budget branch checks self.frontend.has_pending_write(), not has_pending_write_including_streams. So it returns Close while the end of the body is still in the stream's buffer, and increments http.infinite_loop.error.

Inference, not verified: the client's half-close does not seem to drive the spin. The frontend had already dropped READABLE interest. If so, any slow client receiving a large response from a backend that closes after responding could be cut off this way.

`test_issue_806` (`try_backend_stop`, e2e/src/tests/tests.rs) compared the
wall-clock round trip of the request that follows the backend stop with a
100 ms budget. Beside 60 busy loops on a 20-CPU host (load average 80-110)
it failed 17 of 20 runs, every time through the budget branch with the
re-route itself correct (status 200, the surviving backend serving (1, 1)),
at 117-365 ms. Per-thread schedstat showed the time was spent runnable but
waiting for a CPU: the worker thread alone waited 12-167 ms of it, and the
mock backend, which spins on non-blocking accept/read, most of the rest.

- `e2e/src/sched.rs`: `current_tid` and `run_queue_delay`, the sum of the
  second field of `/proc/self/task/<tid>/schedstat` (time spent waiting on a
  runqueue) for the given threads; an unreadable thread counts as zero.
- `Worker::server_tid` and `BackendHandle::tid` record the kernel id of the
  server and backend threads.
- `doc/testing.md` describes the technique.
- `try_backend_stop` compares the round trip less the run-queue wait of the
  client, worker and surviving backend threads, and prints both. A blocked
  wait (a sleep, a timer, a pending connect) is not run-queue time: the
  documented red check, a 150 ms sleep before the connect in
  `Mux::dial_backend`, still fails on the first iteration, at 151.9 ms idle
  and 109-151 ms in 10 of 10 runs beside 60 busy loops.
- `AsyncBackend` blocks in `poll` on its listener, its clients and a stop
  waker instead of spinning, so a request wakes it instead of waiting for its
  next time slice, and it no longer holds a CPU for the whole test.

Measured at the same load: 17/20 runs failed before, 0/20 after in each of
two 20-run batches of the final code (and of two more with an earlier
10 ms poll timeout in place of the waker); over those 4 000 trials the raw
round trip exceeded 100 ms 88 times while the compared value stayed under
4 ms. The full e2e suite passes (622 passed).

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 1ab986e into main Oct 3, 2026
28 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Oct 3, 2026
…losed (#1820)

## Summary

A slow client could get a truncated response, ending in a clean close, when the backend had already written the whole response and closed. Nothing was logged at the default level; the only trace was `http.infinite_loop.error`. This is the intermittent failure of `tls_tests::test_plain_client_half_close_after_request_receives_large_response` (CI run 37019159928, 8,284,694 of 8,388,608 body bytes).

**Cause** (`lib/src/protocol/mux/mod.rs` on `main`):
- `:3218`: `HUP | ERROR` are always in a backend's interest. A backend that is dead but kept for its buffered bytes (`:3113-3115`, `has_buffer_pressure`) therefore has `filter_interest() == HUP`, so `all_backends_readiness_are_empty` stays false.
- When the client's socket would block (or an H2 stream window is exhausted), nothing can progress, and the inner loop runs to `MAX_LOOP_ITERATIONS` within one `ready()` call.
- `:3425`/`:3435`: the budget branch only consults the connection-level `has_pending_write()`. That is false for a plain-TCP H1 frontend, and for an H2 connection whose DATA is still in `back.out`. The branch returns `Close`, and the body tail is never sent.

Instrumentation at the budget branch, from the unit repro below: frontend `I(RWEH-) R(-----)`, backend `I(R-EH-) R(---H-)` with `has_buffer_pressure == true`, `frontend.has_pending_write() == false`, one block in `back.out`. This matches the readiness that PR #1818 captured in the failing CI-shaped runs.

## Fix

- `Connection::has_loop_work`: a backend counts as loop work for READABLE or WRITABLE, or for a HUP or ERROR, unless it is kept for buffered bytes (`has_buffer_pressure`). Only a dead backend waiting for the client to drain the stream buffer stops counting; `try_resume_reading` wakes it. A HUP sozu raises itself mid-pass (`force_disconnect` after an H2 backend protocol error) still brings the loop back to close the backend and answer the client.
- `Mux::wait_for_client_at_loop_limit`: reaching the budget logs a `warn!` with the frontend and backend readiness. If output is still queued (connection level, or in an open stream's `back.out`/`back.blocks`), the session keeps WRITABLE interest, drops the stale event, arms the frontend timeout, and waits for the next writable edge. A session with nothing queued is closed, and so is one that reaches the limit again with no byte written to the client since, so a spin that recurs on every wake-up costs at most two budgets. The outer loop no longer re-enters a spent budget. The budget stays as the safety bound. The shared `has_pending_write_including_streams` H1 arm is deliberately left unchanged, because widening it would also change every delayed-close and timeout path.
- Corrected the stale comment that said interest never holds HUP/ERROR. `mux/LIFECYCLE.md` (close triggers, §7.4 step 6, invariant 12) and `doc/lifetime_of_a_session.md` now describe the exit condition and the budget. CHANGELOG entry added.

This is a different defect from #1640, where a backend FIN in the middle of the body was forwarded as a complete response. Here the backend sends the response whole and the proxy dropped its tail.

## Tests (red on `main`, green here)

- `a_slow_client_receives_the_whole_response_of_a_backend_that_closed` and `a_half_closed_slow_client_...`: the H1 frontend socket would block (small send/receive buffers), and the backend wrote 160 KiB and closed. On `main` the first pass returns `Close` and the client gets 32,805 of 163,883 bytes. With only the spin fix reverted: `ran 9999 iterations`.
- `the_loop_limit_waits_for_the_client_while_a_stream_holds_its_response`: red with the old `has_pending_write()`-only check. `the_loop_limit_closes_a_session_with_nothing_queued` is the guard: a recycled slot's leftover bytes do not keep a session.
- e2e `test_h2_window_stalled_client_receives_the_whole_response_of_a_closed_backend`: the H2 client keeps the default 65,535-byte windows until the backend has closed. On `main`: `http.infinite_loop.error=1 reopened=false body=65535/131072`. Here: `http.infinite_loop.error=0 body=131072/131072 ended=true`.

Limitation: the H1 frontend has no dedicated e2e. The kernel autotunes sōzu's frontend send buffer and no listener setting controls it, so a client that only reads slowly does not reach the state deterministically. I measured this: with the client stalled on a 4 MiB response, the frontend was still writable when the backend's FIN arrived, and stepped reads of 8 MiB never went red on `main`. H1 is covered by the two unit tests and by the stress run below. Flow control stalls the H2 frontend whatever the buffer sizes.

- e2e `test_idle_h2_backend_sending_data_on_stream_zero_is_answered_at_once` and `test_idle_h2_backend_sending_headers_on_an_idle_stream_is_answered_at_once`: an idle H2 backend sends a bad frame and keeps its socket open. With 5000055 (the first version of this PR, which ignored every backend HUP): `answered=false ending=timeout elapsed=12.0s`. Here: a 5xx in about 0.51 s.
- `a_second_loop_limit_without_progress_closes_the_session`: pins the bounded wait.
- The slow-client unit tests count limit hits with a test-only counter, so they also check in release builds. Red in release with the fix reverted: 284 and 262 hits.

The H1 `force_disconnect` path (`ConnectionH1::end_stream`) is not a regression: it clears the backend's interest before it raises HUP, so `filter_interest()` was already empty on `main`, and nothing counted that HUP before this PR either. With `has_loop_work` it now counts, so that backend is closed in the same pass rather than on the next event.

## Validation

- `cargo +nightly fmt --check`: OK
- `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings`: OK
- `RUSTDOCFLAGS="-D warnings" cargo +1.93.1 doc --no-deps --locked -p sozu-lib`: OK
- `cargo +1.93.1 test -p sozu-lib --lib --all-features --locked`: 1597 passed
- full e2e `--features opentelemetry,splice,simd`: 625 passed
- `check_doc_citations.py --root . --base origin/main`: OK
- `test_plain_client_half_close_after_request_receives_large_response`, 150 runs under `taskset -c 0` with 4 busy loops pinned to CPU 0: **0/150 failures** (450 exchanges) with the first version, and **0/100** with the current head. The same setup gave 1/30, 1/60, 1/80 and 2/150 on `main`.

Fixes #1819



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