Skip to content

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

Merged
FarhanAliRaza merged 12 commits into
mainfrom
farhan/trusting-hypatia-4tz406
Nov 20, 2026
Merged

FarhanAliRaza merged 12 commits into
mainfrom
farhan/trusting-hypatia-4tz406

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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 CI Cause Fix
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: ConnectionResetError Granian'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 == 2 After 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) → JSONDecodeError A 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

The blocking-detection tests flaked on loaded runners (macOS, Windows):

- probe_queued stayed set after its probe ran, so a handler could block
  before the monitor queued the probe that times it, a polling gap or more
  late. Totals came out short (0.116 >= 0.12) and a 0.055s block could go
  unseen. Track a probe that is still pending instead.
- The import test sampled while the import machinery found and compiled the
  new module, which can take past the threshold on a slow file system. Hold
  the sample until the module body runs.
- The event processor tests held the loop for a fixed 0.12s, so a monitor
  thread descheduled for that long reported nothing. Hold it until the
  monitor warns, for at most five seconds.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
…eed limit

The first cold process of a Windows run compiles the bytecode of every module
it imports and timed out at 30 seconds, though a script takes about a second.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
A request the old worker accepted as the reload stopped it was reset with
the worker (ConnectionResetError). The app now answers with its worker's pid,
and the test sends its request once that worker has exited, while the slow
replacement still loads, so it must queue in the supervisor's backlog.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
…t one

A real redis delivers keyspace events on their own connection, so after a
lease closes the next event may be the state write before the lock release
(assert 1 == 2 in test_modify_oplock). Wait until the expected count arrives.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
A 502 from the API failed the changes job, and with it every gate behind
the filter, with a JSONDecodeError. Retry the call up to three times.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
@FarhanAliRaza
FarhanAliRaza requested a review from a team as a code owner November 19, 2026 16:12
@FarhanAliRaza FarhanAliRaza added the skip-changelog For doc/internal changes label Nov 19, 2026 — with Claude
@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issues were found.

Summary

This PR fixes a Redis update race and makes CI tests more reliable and faster. The latest change rechecks the subscription deadline after a timed wait ends.

  • Redis-backed connections wait for the update listener before storing socket records.
  • Changed-path checks retry temporary GitHub API failures.
  • Blocking-monitor tests hold the event loop until the warning appears.
  • Redis tests wait for the keyspace events their operation needs.
  • Cold-process serializer tests print thread stacks when a script stalls.
  • Supervisor tests finish sooner by using shorter test-only waits.
  • The Granian reload test waits for the old worker before sending its request.

The previous findings are addressed. No new actionable issues were found.

Reviews (5) · Last reviewed commit: "fix(token-manager): keep waiting until t..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread tests/units/utils/test_exec.py Resolved
@codspeed

codspeed Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 20 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing farhan/trusting-hypatia-4tz406 (5a365d6) with main (2fc174b)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

…cket record

on_connect started the lost and found subscriber as a background task and
stored the socket record before redis confirmed the subscription. Another
instance publishing for the token in that window had its delta dropped by
redis, which flaked test_redis_token_manager_lost_and_found.

A connect now waits for the confirmation, as the redis state manager does for
its lock subscription, and goes ahead with a warning after 5 seconds. Only an
instance's first connect waits.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
Comment thread tests/units/utils/test_token_manager.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread reflex/utils/token_manager.py Resolved
Comment thread reflex/app.py Resolved
The supervisor tests spent about 32 seconds of every unit run waiting on the
production drain budget (5s), record grace (2s) and a 15ms-per-line consumer.
Scripts that only need some limit to end the drain now set it short, as the
others already did, and the slow consumer reads at 5ms per line, still far
slower than the writer. The file now runs in about 19 seconds.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
…of waiting

An earlier commit here put the Windows timeouts down to bytecode compilation,
but collection has already imported sqlalchemy, sqlmodel and reflex.app in the
parent, so that is not it. A slow interpreter and a hang in the import locks
these tests exercise look the same from outside. The script now dumps every
thread's stack and exits after 60 seconds, so a failure shows which it was,
and the parent waits 90 seconds instead of 180.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
…t each connect

While redis could not confirm the lost and found subscription, every connect
waited the full five seconds. Connects now wait only until five seconds after
the subscriber started. The timeout test moves to module level, and the
changed-paths retry says how long it sleeps.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
@FarhanAliRaza FarhanAliRaza changed the title Fix flaky unit tests and retry the changed-paths API call Fix flaky tests, a lost-update race in the redis token manager, and slow supervisor tests Nov 19, 2026
Comment thread reflex/utils/token_manager.py Resolved
A subscriber that failed after its first five seconds was retried inside
the same task, so connects did not wait for its new subscription and the
race came back. Every attempt now sets the deadline connects wait until.

The post-handler blocking test now compares both warnings in full.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
On Windows, where the monotonic clock moves in 15.6ms ticks, a subscription
wait could time out while the loop clock still read just before the
deadline. The next connect then waited again and warned a second time,
failing test_redis_token_manager_waits_for_an_unconfirmed_subscription_once
on windows-latest. The wait now re-reads the deadline until it has passed,
and a connect warns only when it waited and gave up. A Linux run with a
simulated 15.6ms clock failed 3 of 5 times before and 0 of 25 after.

Claude-Session: https://claude.ai/code/session_01LwJUCTbMr47R4wgWUN4W91
@FarhanAliRaza
FarhanAliRaza merged commit 44d1d69 into main Nov 20, 2026
168 checks passed
@FarhanAliRaza
FarhanAliRaza deleted the farhan/trusting-hypatia-4tz406 branch November 20, 2026 19:09
benedikt-bartscher added a commit to benedikt-bartscher/reflex that referenced this pull request Nov 22, 2026
Port upstream's EventNamespace changes to the moved namespace modules, so
both transports get them. The X-Forwarded-For validation of reflex-dev#7880 runs in
build_static_router_data, with _is_port and _forwarded_client_ip next to
_trusted_forwarded_value; _clip moves there too for its debug line, and
reflex.app imports it back. The suppress_warnings parameter of reflex-dev#7921 is on
BaseEventNamespace.emit_update, and the awaited lost-and-found
subscription of reflex-dev#7938 runs in BaseEventNamespace.handle_connect.

The X-Forwarded-For tests move to tests/units/test_event_namespace.py.
The modify_state test of reflex-dev#7921 builds a WebsocketEventNamespace and reads
the warning from reflex.event_namespace, which logs it now.
benedikt-bartscher added a commit to benedikt-bartscher/reflex that referenced this pull request Nov 22, 2026
…inification

- reflex-dev#7583's field subscription goes on the state's local, which a resolver
  can prefix with `$rx_`; the two hook tests expect the field list.
- reflex-dev#7938's wait for the lost-and-found subscription runs after the per-SID
  setup, which has to happen before the connect's first await, and still
  before link_token_to_sid stores the socket record.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changelog For doc/internal changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants