Skip to content

Test: Fix two flaky test failures (refresh_lock waiter, sse-starlette shutdown flag leak) - #709

Merged
satoseino merged 2 commits into
mainfrom
fix/flaky-unit-test-case
Nov 27, 2026
Merged

satoseino merged 2 commits into
mainfrom
fix/flaky-unit-test-case

Conversation

@satoseino

@satoseino satoseino commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes two unrelated CI flakes. Both depend on runner timing and neither is caused by a code change.

1. test_waiter_observes_result_without_acquiring_second_lease (registry-pkgs)

It failed on main in run 45930997158 and earlier in 44538549010, with OAuthCoordinationError: Token refresh already in progress; retry. The test used the file-wide 0.1s REFRESH_LOCK_WAIT_SECONDS, but its holder-to-waiter handoff goes through several asyncio.to_thread calls. On a busy runner with coverage on, those took longer than 100ms. The test doesn't check the timeout, so it now sets a 5s budget. The other tests still use 0.1s, including the one that checks the timeout.

2. 22 SSE test failures in registry

These failed in run 46056676626, in test_mcpgw_stateless, test_mcp_downstream and test_protocol_gate. The errors were initialize SSE body was truncated and Child exited without calling task_status.started().

Cause: test_mcp_downstream_pool.py runs a real uvicorn.Server in-process. While it serves, uvicorn sets the process's SIGTERM handler to server.handle_exit. sse-starlette's shutdown watcher finds the server through that handler. When the test sets should_exit at teardown and the watcher's 0.5s check runs before the loop closes, it sets the process-global AppStatus.should_exit = True, and nothing resets it. Every later EventSourceResponse in the session then closes with no events. I reproduced CI's exact 22-failure set locally by making the watcher check faster.

Changes:

  • The pool test serves through _NoSignalServer, a uvicorn.Server subclass whose capture_signals does nothing, so the watcher can't find the server.
  • A new autouse fixture in registry/tests/conftest.py resets AppStatus.should_exit after each test, in case another test leaks it later.

After the fix the reproduction passes (169 passed), and the full registry suite passes (3633 passed).

test_waiter_observes_result_without_acquiring_second_lease inherited the
module's 0.1s REFRESH_LOCK_WAIT_SECONDS patch, but its holder-to-waiter
handoff spans several asyncio.to_thread hops. On a loaded CI runner that
exceeded 100ms and raised OAuthCoordinationError (CI run 45930997158).
The test doesn't exercise the timeout, so give it a 5s budget.
test_mcp_downstream_pool runs a real uvicorn.Server in-process, which binds
SIGTERM to server.handle_exit while serving. sse-starlette's shutdown watcher
finds the server through that handler; when the test sets should_exit at
teardown and the watcher's 0.5s poll lands before the loop closes, it sets the
process-global AppStatus.should_exit, which is never reset. Every later
EventSourceResponse then closes with no events, failing 22 SSE tests in
test_mcpgw_stateless, test_mcp_downstream and test_protocol_gate (CI run
46056676626).

- Serve the pool test through a uvicorn.Server subclass whose
  capture_signals is a no-op, so the watcher can't reach the server.
- Add an autouse conftest fixture that resets AppStatus.should_exit after
  each test, as a guard against future leaks.
@satoseino satoseino changed the title test: Deflake refresh_lock waiter handoff test Test: Fix two flaky test failures (refresh_lock waiter, sse-starlette shutdown flag leak) Nov 27, 2026
@satoseino
satoseino merged commit a0355d9 into main Nov 27, 2026
7 checks passed
@satoseino
satoseino deleted the fix/flaky-unit-test-case branch November 27, 2026 20:10
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