Skip to content

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

Merged
FarhanAliRaza merged 4 commits into
mainfrom
claude/vibrant-noether-44n31l
Nov 6, 2026
Merged

FarhanAliRaza merged 4 commits into
mainfrom
claude/vibrant-noether-44n31l

Conversation

@masenf

@masenf masenf commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

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 Refresh outdated dependencies flagged on the release branch #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:

Configuration Cancels lost
defaults 26 / 40
protocol=2 28 / 40
socket_timeout=None 0
redis-py 7.4.1 0
Python 3.14, defaults 0

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

View guided diff Turn on auto-fix

redis-py 8 defaults to the RESP3 wire protocol, and its asyncio client
hangs under that protocol on Python 3.11: a redis-backed state manager's
teardown parks forever in the event-loop selector (#7026). Python 3.12+
is unaffected, and so is RESP2, which redis-py 7 spoke.

Build every redis client Reflex creates with protocol=2 on 3.11 only,
widen the dependency bound to <9.0 (#6764), and drop redis from the
outdated-dependency hold list. redis-py 8 also types GET/MGET replies
as bytes | str, so narrow the three call sites added since the earlier
narrowing in #7019.

Reproduced on 3.11 with redis-py 8.1.0 and REFLEX_REDIS_URL set: the
full unit suite hung in a test_redis.py fixture teardown before the pin,
and the new test_prerequisites regression test fails there without it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ESN8DhGB34eCs9kKZZbDdg
The RESP2 pin did not stop the hang: with it in place the unit suite still
timed out in redis-backed fixture teardowns and inside
test_initialize_with_state on 3.11, and the wire protocol turned out to be
unrelated.

The actual cause is redis-py 8 giving every connection a 5s socket timeout
where redis-py 7 had none. send_packed_command then wraps each send in
asyncio.wait_for, and Python 3.11's wait_for drops an outside cancellation
that lands in the same loop iteration its inner task completes. The lock
subscription task keeps running with cancelling()=1, so
StateManagerRedis.close() awaits it forever. Cancelling a task mid-send
on a real connection loses the cancel at 26 of 40 loop offsets with the
defaults and 28 of 40 with protocol=2, and at none with socket_timeout=None,
with redis-py 7.4.1, or on 3.14, which rebuilt wait_for.

So on 3.11 build every client with socket_timeout=None, which restores
redis-py 7's behavior there, and leave later interpreters on redis-py's
default. Replace the kwarg test accordingly and add a behavioral test
that 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.
With the fix the full 3.11 unit suite passes with redis-py 8.1.0 and no
timeouts.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ESN8DhGB34eCs9kKZZbDdg
@masenf
masenf requested a review from a team as a code owner November 6, 2026 08:05
@greptile-apps

greptile-apps Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with the previous test finding fully addressed.

Summary

Allow redis-py 8 while keeping socket timeouts disabled to avoid the Python 3.11 shutdown hang.

  • Reflex supports redis-py 8 and keeps task cancellation working during Redis sends.

Reviews (3) · Last reviewed commit: "Fail the cancel test when the task error..." · Reviewed by Greptile

Comment thread tests/units/test_prerequisites.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 8 files

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

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

Comment thread tests/units/test_prerequisites.py Resolved
@codspeed

codspeed Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.91%

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

❌ 1 regressed benchmark
✅ 147 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_state_access[read_cached_computed] 47.1 ms 49.5 ms -4.91%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing claude/vibrant-noether-44n31l (efcfac8) with main (31951ab)

Open in CodSpeed

@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 6, 2026
The timeout loses cancellations only on Python 3.11, but keeping it on
3.12+ gives those users a timeout that redis-py 7 never had and makes
behavior depend on the Python version. One setting keeps redis-py 7's
behavior everywhere and removes the version branch.
@FarhanAliRaza FarhanAliRaza changed the title Allow redis-py 8; drop its socket timeout on Python 3.11 to fix the cancellation hang Allow redis-py 8; drop its socket timeout to fix the Python 3.11 cancellation hang Nov 6, 2026
asyncio.wait puts a task in `done` however it ended, so a task that died
of a redis error passed the test without proving the cancel landed.
@FarhanAliRaza
FarhanAliRaza merged commit babf7ee into main Nov 6, 2026
169 of 170 checks passed
@FarhanAliRaza
FarhanAliRaza deleted the claude/vibrant-noether-44n31l branch November 6, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

redis-py 8 hangs on Python 3.10/3.11 — blocks the redis v8 upgrade (#6764) support redis v8

3 participants