Fix the `test_each_browser_gets_only_the_sessions_it_watches` flake by waiting for the queued input it asserts on

Merged
DS
18d ago
+2-0

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Why we make these changes

test_each_browser_gets_only_the_sessions_it_watches failed in CI with assert ('queued', 'queued') == ('running', 'queued').

Root cause: the wait matched an earlier live message. b reads queued twice: once when its go input queues, before its turn starts, and again when and then queues behind the running turn. The test asserted on the first live message where b reads queued. When a's read loop had not yet taken its go as b's go queued, that message is the early one, published while a still read queued.

Fix: the test first waits for b to read running, then for the queued that and then publishes. By then a's turn has started and is held at its gate, so a reads running on any scheduling. No production change: WebService.live_changed publishes each state change in order, and the test now waits for the state it asserts.

The teardown TimeoutError follows from the failed assertion, not a second defect. The failure leaves both turns held at their gates, and /exit is not a during_turn command, so it queues behind a's turn. Fleet.exit's running.result(timeout=10) then times out (10.02 s in CI).

New public surface

None.

Verification

  • Forced ordering. A local-only patch queues a's go without waking a's read loop until b's go has queued. That is the CI ordering. On main the test fails with CI's exact assertion, then the same teardown TimeoutError. On this branch it passes.
  • Repeated. Unforced on this branch, uv run --with=pytest-repeat pytest tests/clai2/test_web_sessions.py::test_each_browser_gets_only_the_sessions_it_watches --count=30, without -n: 30 passed.
Forcing patch (local only, not committed)
# before the two `go` posts
inbox = fleet.launch._inbox
inbox.put = inbox._entries.append  # queue without waking the read loop
# ... the two `go` posts ...
del inbox.put
fleet.portal.call(inbox._ready.set)

live messages socket one read up to the asserted one, as (session, state, busy):

messages
maina queued busy, b idle → a queued busy, b queued busy (asserted)
this branch… → a running busy, b queued busy (asserted)

What changes for existing users

Nothing. The diff is one wait in one test.

PR decisions

2026-11-16 · our worker fixes #13424 · iter -

  • Decision: David chose to fix the flake with a managed worker rather than the outside volunteer on the issue
  • Why: David's call, relayed by the managing session
  • Source: https://github.com/pydantic/pydantic-ai/issues/13424
  • Session: a8a7e6a5-2e49-44b1-8f1f-65639818590c

2026-11-16 · no claim comment on #13424 · iter -

  • Decision: Post nothing on the issue; the PR's Closes line links it
  • Why: Managing session's call; it overrides the flake-claim comment step of pushing-commits-to-the-repo
  • Source: https://github.com/pydantic/pydantic-ai/issues/13424
  • Session: a8a7e6a5-2e49-44b1-8f1f-65639818590c

2026-11-16 · fix the test's wait, not production · iter -

  • Decision: Wait for b to read running before waiting for its queued; no production change
  • Why: Production publishes each state in order; the wait matched the live message from b's own go queuing, before either turn started (forced ordering reproduces CI's ('queued', 'queued'))
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43489664177/job/132107318724
  • Session: a8a7e6a5-2e49-44b1-8f1f-65639818590c

2026-11-16 · pre-push review 1 of 3 at 03720be22b28cc1eacc19df0f28716dd62c62720 · iter -

2026-11-16 · pre-push review 1 of 3 at 03720be22b28cc1eacc19df0f28716dd62c62720 · iter -

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

Summary by cubic

Fixes the flaky test_each_browser_gets_only_the_sessions_it_watches by waiting for the queued input it actually asserts on.

  • The old wait matched b's first queued state, published when its own go input queued before either turn started.
  • The test now waits for b to read running first, then for the queued that and then publishes, so a always reads running.
  • No production change; the diff is one additional wait in one test. Closes #13424.