Allow redis-py 8; drop its socket timeout to fix the Python 3.11 cancellation hang

Merged
MA
1mo ago
+88-29

Closes #7026. Closes #6764.

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

What changed

  • redis >=6.4,<9.0; the lock moves only redis 7.4.1 → 8.1.0. redis is removed from the check-outdated-deps hold list.
  • reflex/utils/prerequisites.py: a shared _redis_client_kwargs() builds all three clients. It passes socket_timeout=None on every Python version, which keeps redis-py 7's behavior (no socket timeout) everywhere. The connect timeout keeps redis-py's default.
  • Three casts for redis-py 8's stricter bytes | str reply types at call sites added after the narrowing in #7019 (pyright is clean against 8.1.0).
  • Tests in tests/units/test_prerequisites.py: one asserts the kwarg on every client; one cancels a task mid-send at 40 loop offsets against a real server. It fails without the fix on 3.11 and skips when REFLEX_REDIS_URL is unset, so it runs in the redis CI legs.
  • News fragment.

Root cause of the hang

It is not RESP3 and not retry backoff. redis-py 8 gives every connection a 5 s socket_timeout where redis-py 7 had none, so send_packed_command wraps each send in asyncio.wait_for. Python 3.11's wait_for drops an outside cancellation that lands in the same loop iteration its inner task completes (rewritten on top of asyncio.timeout in 3.12, which is why 3.12+ is unaffected). The lock-subscription task gets cancelled by StateManagerRedis.close(), the cancel is lost, the task keeps reading with cancelling() == 1, and close() awaits it forever.

Cancelling a task mid-send on a real connection at 40 loop offsets, Python 3.11, redis-py 8.1.0:

ConfigurationCancels lost
defaults26 / 40
protocol=228 / 40
socket_timeout=None0
redis-py 7.4.10
Python 3.14, defaults0

The first commit tried pinning RESP2; the suite still hung with it, so the second commit replaces it. The third commit applies socket_timeout=None on every Python version instead of only 3.11 (see below). All are kept so the history shows why.

Verification

  • Before: the full unit suite on 3.11 + redis-py 8.1.0 with REFLEX_REDIS_URL timed out in redis-backed fixture teardowns and inside test_initialize_with_state, as the issue describes.
  • After: the full 3.11 suite passes with no timeouts in both the plain redis leg and the REFLEX_OPLOCK_ENABLED leg.
  • ruff, pyright, the changelog check, and check_min_deps.py reflex pass.

Why no socket timeout on every version

The timeout loses cancellations only on 3.11, but a 3.11-only setting would give 3.12+ users a 5 s timeout that redis-py 7 never had and make behavior depend on the Python version. One setting keeps today's behavior everywhere. redis-py 8 still enables TCP keepalive by default, so a dead connection is still detected. A per-command timeout can return on every version once redis-py's send path stops using asyncio.wait_for.


Generated by Claude Code