Fix flaky tests, a lost-update race in the redis token manager, and slow supervisor tests

Merged
FA
10d ago
+505-144

Fixes flaky tests that fail CI on main and on unrelated PRs. One of them came from a real race in RedisTokenManager, which is also fixed here. The PR also cuts about 13 seconds from every unit test run.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes

Failure seen in CICauseFix
test_token_manager.py::test_redis_token_manager_lost_and_found: Expected 'mock' to have been called once. Called 0 times.A real bug. on_connect started the lost-and-found subscriber in the background and stored the socket record before Redis confirmed the subscription. A delta another instance published in that window was dropped by Redis. Delaying the PSUBSCRIBE by 0.5s reproduces it every time.A connect now waits for the psubscribe confirmation, as the Redis state manager already does for its lock subscription. Each subscription attempt, including a retry after a failure, gives connects at most 5s to wait, so a Redis that can't confirm doesn't stall every connect. News fragment included.
test_lazy_serializer_regressions.py::test_sqlmodel_relationships_from_cold_state[False-None]: subprocess.TimeoutExpired after 30s (Windows)Each script normally takes about 1s, and the cause of the Windows outliers is unknown. It isn't bytecode compilation: collection already imports sqlalchemy, sqlmodel and reflex.app in the parent. From outside, a slow interpreter and a hang in the import locks these tests exercise look the same.Before its 60s limit, the script dumps every thread's stack and exits (faulthandler.dump_traceback_later), so the next failure shows where it was. The parent waits 90s as a backstop.
test_blocking.py::test_blocking_just_over_threshold_is_detected and test_event_processor.py::test_processor_reports_blocking_after_handler_returns: empty caplog (macOS). test_blocking_import_is_logged_with_import_advice: wrong stack (Windows)probe_queued stayed set after its probe ran, so a handler could block before the probe that times it was queued. Under CPU load I reproduced totals of 0.116 >= 0.12. The import test sampled while the import machinery was still finding and compiling the module. The event processor tests held the loop for only a fixed 0.12s.Handlers block only while a probe is pending. The import test holds the sample until the module body runs. The event processor tests hold the loop until the monitor warns, for at most 5s, and compare both warnings in full.
test_exec.py::test_run_granian_backend_holds_requests_across_reload: ConnectionResetErrorGranian's reloader SIGTERMs the old worker, which resets a connection it accepted as it stopped.The app answers with its worker pid. The test sends its request once the old worker has exited, while the slow replacement is still loading.
test_redis.py::test_modify_oplock: assert 1 == 2After a lease closes, the test woke on the next keyspace event, which can be the state write that comes before the lock release.Wait for the expected count of events.
changes job: gh: Server Error (HTTP 502) → JSONDecodeErrorA transient API error failed every gate behind the filter.Try the pull request files API call up to three times, waiting 10s and then 20s.

Faster tests

test_supervisor.py spent about 32s of every unit run waiting out production timeouts: the 5s drain budget, the 2s record grace, and a consumer reading at 15ms per line. Scripts that only need some limit to end the drain now set it short, as others in the file already did. The slow consumer now reads at 5ms per line, still far slower than the writer. The file now runs in about 19s, and 8 of 8 runs under CPU load passed.

Testing

  • Under CPU load, the blocking tests went from 7 failures in 25 runs to 0 in 40. The Granian reload test went from 1 in 25 to 0 in 30.
  • Each new token-manager test fails without its fix: the lost-and-found test (subscription delayed by 0.5s), the unconfirmed-subscription test, and the resubscription test.
  • The subscription wait re-reads its deadline until it has passed. On Windows the clock moves in 15.6ms ticks and a timeout can end just short of the deadline, which made a second connect wait again. With a simulated 15.6ms clock on Linux, the old code failed 3 of 5 runs and the new code failed 0 of 25.
  • The Redis tests pass against both mock and real Redis.
  • The full unit suite passes locally (16,390 passed), and every CI leg passes, including all Windows legs.
  • ruff, pyright and pre-commit pass on the changed files.

Not addressed

reflex-docs-macos fails on most main pushes with Prerender: Request failed for /docs/: The operation was aborted due to timeout. React Router 8.4 uses a fixed 10s per-request prerender timeout with no retries, and its config does not expose either. That needs its own investigation.

https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91