The Fix
pip install redis==7.1.0
Based on closed redis/redis-py issue #2511 · PR/commit linked
@@ -502,8 +502,6 @@ async def read_from_socket(
# return True to indicate that data was read.
return True
- except asyncio.CancelledError:
- raise
except (socket.timeout, asyncio.TimeoutError):
Traceback (most recent call last):
File "/usr/lib/python3.10/unittest/async_case.py", line 67, in _callTearDown
self._callAsync(self.asyncTearDown)
File "/usr/lib/python3.10/unittest/async_case.py", line 79, in _callAsync
return self._asyncioTestLoop.run_until_complete(fut)
File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/usr/lib/python3.10/unittest/async_case.py", line 101, in _asyncioLoopRunner
ret = await awaitable
File "/home/toby/dev/saq/tests/test_worker.py", line 64, in asyncTearDown
await cleanup_queue(self.queue)
File "/home/toby/dev/saq/tests/helpers.py", line 11, in cleanup_queue
await queue.redis.flushdb()
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 487, in execute_command
return await conn.retry.call_with_retry(
File "/home/toby/dev/redis-py/redis/asyncio/retry.py", line 62, in call_with_retry
await fail(error)
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 476, in _disconnect_raise
raise error
File "/home/toby/dev/redis-py/redis/asyncio/retry.py", line 59, in call_with_retry
return await do()
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 463, in _send_command_parse_response
return await self.parse_response(conn, command_name, **options)
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 505, in parse_response
response = await connection.read_response()
File "/home/toby/dev/redis-py/redis/asyncio/connection.py", line 948, in read_response
response = await self._parser.read_response(
File "/home/toby/dev/redis-py/redis/asyncio/connection.py", line 397, in read_response
raw = await self._buffer.readline()
File "/home/toby/dev/redis-py/redis/asyncio/connection.py", line 323, in readline
await self._read_from_socket()
File "/home/toby/dev/redis-py/redis/asyncio/connection.py", line 266, in _read_from_socket
raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
redis.exceptions.ConnectionError: Connection closed by server.
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\npip install redis==7.1.0\nWhen NOT to use: This fix should not be applied if the application relies on handling BaseException specifically.\n\n
Why This Fix Works in Production
- Trigger: self._callAsync(self.asyncTearDown)
- Mechanism: The change in exception handling from BaseException to Exception caused unit tests to hang indefinitely
- Why the fix works: Addresses the issue by changing the exception handling in the Connection class from BaseException to Exception, preventing unit tests from hanging. (first fixed release: 7.1.0).
Why This Breaks in Prod
- Shows up under Python 3.5.1 in real deployments (not just unit tests).
- The change in exception handling from BaseException to Exception caused unit tests to hang indefinitely
- Surfaces as: Traceback (most recent call last):
Proof / Evidence
- GitHub issue: #2511
- Fix PR: https://github.com/redis/redis-py/pull/2104
- First fixed release: 7.1.0
- Reproduced locally: No (not executed)
- Last verified: 2026-02-07
- Confidence: 0.75
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.28
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Closing as I assume this issue was fixed with all the async and CancelledError fixes. @tobymao Feel free to re-open it if this still happen…”
“after doing some more research, it looks like there was a lengthy discussion about it https://github.com/redis/redis-py/issues/2103 as noted, my unit test shows that redis is…”
Failure Signature (Search String)
- self._callAsync(self.asyncTearDown)
Error Message
Stack trace
Error Message
-------------
Traceback (most recent call last):
File "/usr/lib/python3.10/unittest/async_case.py", line 67, in _callTearDown
self._callAsync(self.asyncTearDown)
File "/usr/lib/python3.10/unittest/async_case.py", line 79, in _callAsync
return self._asyncioTestLoop.run_until_complete(fut)
File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/usr/lib/python3.10/unittest/async_case.py", line 101, in _asyncioLoopRunner
ret = await awaitable
File "/home/toby/dev/saq/tests/test_worker.py", line 64, in asyncTearDown
await cleanup_queue(self.queue)
File "/home/toby/dev/saq/tests/helpers.py", line 11, in cleanup_queue
await queue.redis.flushdb()
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 487, in execute_command
return await conn.retry.call_with_retry(
File "/home/toby/dev/redis-py/redis/asyncio/retry.py", line 62, in call_with_retry
await fail(error)
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 476, in _disconnect_raise
raise error
File "/home/toby/dev/redis-py/redis/asyncio/retry.py", line 59, in call_with_retry
return await do()
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 463, in _send_command_parse_response
return await self.parse_response(conn, command_name, **options)
File "/home/toby/dev/redis-py/redis/asyncio/client.py
... (truncated) ...
Minimal Reproduction
Traceback (most recent call last):
File "/usr/lib/python3.10/unittest/async_case.py", line 67, in _callTearDown
self._callAsync(self.asyncTearDown)
File "/usr/lib/python3.10/unittest/async_case.py", line 79, in _callAsync
return self._asyncioTestLoop.run_until_complete(fut)
File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
return future.result()
File "/usr/lib/python3.10/unittest/async_case.py", line 101, in _asyncioLoopRunner
ret = await awaitable
File "/home/toby/dev/saq/tests/test_worker.py", line 64, in asyncTearDown
await cleanup_queue(self.queue)
File "/home/toby/dev/saq/tests/helpers.py", line 11, in cleanup_queue
await queue.redis.flushdb()
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 487, in execute_command
return await conn.retry.call_with_retry(
File "/home/toby/dev/redis-py/redis/asyncio/retry.py", line 62, in call_with_retry
await fail(error)
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 476, in _disconnect_raise
raise error
File "/home/toby/dev/redis-py/redis/asyncio/retry.py", line 59, in call_with_retry
return await do()
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 463, in _send_command_parse_response
return await self.parse_response(conn, command_name, **options)
File "/home/toby/dev/redis-py/redis/asyncio/client.py", line 505, in parse_response
response = await connection.read_response()
File "/home/toby/dev/redis-py/redis/asyncio/connection.py", line 948, in read_response
response = await self._parser.read_response(
File "/home/toby/dev/redis-py/redis/asyncio/connection.py", line 397, in read_response
raw = await self._buffer.readline()
File "/home/toby/dev/redis-py/redis/asyncio/connection.py", line 323, in readline
await self._read_from_socket()
File "/home/toby/dev/redis-py/redis/asyncio/connection.py", line 266, in _read_from_socket
raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
redis.exceptions.ConnectionError: Connection closed by server.
Environment
- Python: 3.5.1
What Broke
Unit tests hang forever, requiring termination of Redis to stop the process.
Why It Broke
The change in exception handling from BaseException to Exception caused unit tests to hang indefinitely
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install redis==7.1.0
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/redis/redis-py/pull/2104
First fixed release: 7.1.0
Last verified: 2026-02-07. Validate in your environment.
When NOT to Use This Fix
- This fix should not be applied if the application relies on handling BaseException specifically.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
- Pin production dependencies and upgrade only with a reproducible test that hits the failing path.
Version Compatibility Table
| Version | Status |
|---|---|
| 7.1.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.