Skip to content

fix(tui): make the stream-manager client tests deterministic - #578

Merged
huichen merged 1 commit into
mainfrom
claude/tui-flake-fix
Sep 12, 2026
Merged

huichen merged 1 commit into
mainfrom
claude/tui-flake-fix

Conversation

@huichen

@huichen huichen commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

Why

tui rpc::grpc_client::tests::subscribe_stream_event_bookkeeping was flaky — roughly one full-suite run in eight — and five of its siblings shared the same root cause. Both causes are in the test scaffolding, not in the client.

1. The mock killed the stream on every resubscribe

EventfulMock::stream_events handed its single event channel to the first subscription and answered every later one with an empty pending() stream, while the original receiver was dropped along with the abandoned stream. So a poke that landed while the stream manager was subscribing left the client reading a stream that would never yield anything, and the test's next tx.send(..).unwrap() panicked with:

called `Result::unwrap()` on an `Err` value: SendError { .. }

That is now reproduced deterministically by a new regression test before the fix (poke_during_live_subscription_resubscribes_without_losing_events) and passes after it.

2. "A subscription exists" is not proof of delivery

The tests slept 100–200 ms to let the client subscribe, then assumed the subscription they waited for was the live one. A poke that lands while the manager is subscribing makes it return Poked at the top of its loop before it ever reads a message, so events sent to that subscription are dropped with it. Sleeping longer cannot fix that — only proving delivery can.

What changed (test code only, tui/src/rpc/grpc_client.rs)

  • EventSource replaces the single-shot channel: every stream_events call gets a live stream of its own (like the real agent), and a send goes to every live subscription, pruning the ones the client dropped. It also counts subscriptions and notifies on each, so tests can wait for the client's own (re)subscription.
  • attach() replaces the sleep-then-guess setup: it sets the session, pokes once, then probes with a ping and requires two round trips in a row — the first proves the subscription is reading, the second that it survived the poke still in flight behind it. Nothing pokes afterwards, so a subscription that answers both keeps receiving for the rest of the test.
  • recv_until() waits for the event an assertion depends on (the client forwards an event only after its run bookkeeping, so a received event proves the state it implies). That removes the sleeps that were timing the client, including the one in front of a negative assertion (agent_end for a different run must keep the run active), which now sends a sentinel ping behind it instead.
  • Every wait is bounded and fails with a message instead of hanging; send errors when nothing is subscribed rather than dropping the event silently.
  • spin_until_bool is gone (superseded by recv_until), along with its self-test.

Evidence

  • Full suite: 110 runs with no failures after the fix, including 40 runs with a second suite running concurrently and 20 under an added CPU load job. Pre-fix the same loop failed within ~17 runs (the failure was captured as the SendError above).
  • New regression test fails on the old scaffold, passes on the new one.
  • cargo fmt -p future-tui --check, cargo clippy -p future-tui --all-targets -- -D warnings, cargo test -p future-tui all clean (809 lib + 13 integration tests).

No product code changed: the client, the stream manager and the mock's contract are as before — the tests now synchronize on delivery instead of on wall-clock guesses.

`rpc::grpc_client::tests::subscribe_stream_event_bookkeeping` and its
siblings were flaky (about one full-suite run in eight). Two causes, both in
the test scaffolding:

1. The mock's `stream_events` handed its single event channel to the *first*
   subscription and answered every resubscribe with an empty `pending()`
   stream — while the original receiver was dropped with the abandoned
   stream. A poke that landed while the stream manager was subscribing
   therefore left the client on a dead stream, and the test's next send
   panicked with `SendError { .. }` (reproduced deterministically by a new
   regression test).

2. The tests slept (100-200 ms) to let the client subscribe, then assumed the
   subscription they had waited for was the live one. A poke that lands while
   the manager is subscribing makes it return `Poked` at the top of its loop
   *before* it ever reads a message, so "a subscription exists" is no proof
   that events will be delivered: they are dropped with the abandoned stream.

The mock now serves every subscription a live stream of its own, like the
real agent, and delivers each event to all live subscriptions. Tests attach
through a helper that probes with a `ping` and requires two round trips in a
row — the first proves the subscription is reading, the second that it
survived the poke still in flight behind it — instead of sleeping. Assertions
that followed a sleep now wait for the event itself, and every wait is
bounded and fails with a message rather than hanging.

This also drops `spin_until_bool` (superseded by the `recv_until` helper) and
removes two sleeps whose only purpose was timing the client.

Verified by running the full suite 110 times (including under heavy parallel
load and with two suites at once) with no failures; the pre-fix state failed
within ~17 runs.
@huichen
huichen enabled auto-merge (squash) September 12, 2026 16:01
@huichen
huichen merged commit 96f0be1 into main Sep 12, 2026
12 checks passed
@huichen
huichen deleted the claude/tui-flake-fix branch September 12, 2026 16:06
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