Repository navigation
Allow redis-py 8; drop its socket timeout to fix the Python 3.11 cancellation hang - #7757
Conversation
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
[Medium impact] The PR appears safe to merge, with the previous test finding fully addressed. SummaryAllow redis-py 8 while keeping socket timeouts disabled to avoid the Python 3.11 shutdown hang.
Reviews (3) · Last reviewed commit: "Fail the cancel test when the task error..." · Reviewed by Greptile |
There was a problem hiding this comment.
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
Merging this PR will degrade performance by 4.91%1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
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.
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.
Closes #7026. Closes #6764.
All Submissions:
Type of change
Changes To Core Features:
What changed
redis >=6.4,<9.0; the lock moves only redis 7.4.1 → 8.1.0. redis is removed from thecheck-outdated-depshold list.reflex/utils/prerequisites.py: a shared_redis_client_kwargs()builds all three clients. It passessocket_timeout=Noneon every Python version, which keeps redis-py 7's behavior (no socket timeout) everywhere. The connect timeout keeps redis-py's default.casts for redis-py 8's stricterbytes | strreply 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/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 whenREFLEX_REDIS_URLis unset, so it runs in the redis CI legs.Root cause of the hang
It is not RESP3 and not retry backoff. redis-py 8 gives every connection a 5 s
socket_timeoutwhere redis-py 7 had none, sosend_packed_commandwraps each send inasyncio.wait_for. Python 3.11'swait_fordrops an outside cancellation that lands in the same loop iteration its inner task completes (rewritten on top ofasyncio.timeoutin 3.12, which is why 3.12+ is unaffected). The lock-subscription task gets cancelled byStateManagerRedis.close(), the cancel is lost, the task keeps reading withcancelling() == 1, andclose()awaits it forever.Cancelling a task mid-send on a real connection at 40 loop offsets, Python 3.11, redis-py 8.1.0:
protocol=2socket_timeout=NoneThe first commit tried pinning RESP2; the suite still hung with it, so the second commit replaces it. The third commit applies
socket_timeout=Noneon every Python version instead of only 3.11 (see below). All are kept so the history shows why.Verification
REFLEX_REDIS_URLtimed out in redis-backed fixture teardowns and insidetest_initialize_with_state, as the issue describes.REFLEX_OPLOCK_ENABLEDleg.check_min_deps.py reflexpass.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