Repository navigation
Closed
Description
Traceback (most recent call last):
File "/backend/src/services/redis_service.py", line 94, in get_cached_profiles
fetched_profiles = await self.client.json().mget(keys, Path.root_path())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/redis/asyncio/client.py", line 616, in execute_command
return await conn.retry.call_with_retry(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/redis/asyncio/retry.py", line 59, in call_with_retry
return await do()
^^^^^^^^^^
File "/opt/venv/lib/python3.12/site-packages/redis/asyncio/client.py", line 589, in _send_command_parse_response
await conn.send_command(*args)
File "/opt/venv/lib/python3.12/site-packages/redis/asyncio/connection.py", line 505, in send_command
await self.send_packed_command(
File "/opt/venv/lib/python3.12/site-packages/redis/asyncio/connection.py", line 480, in send_packed_command
self._writer.writelines(command)
File "/usr/local/lib/python3.12/asyncio/streams.py", line 349, in writelines
self._transport.writelines(data)
File "/usr/local/lib/python3.12/asyncio/selector_events.py", line 1182, in writelines
self._write_ready()
TypeError: 'NoneType' object is not callable
I am experiencing this issue especially in peak traffic.
My redis service is initialized this way:
import redis.asyncio as redis
@lru_cache()
def get_redis_pool():
settings = get_settings()
return redis.ConnectionPool.from_url(settings.REDIS_URL_WITH_PASSWORD, decode_responses=True)
@lru_cache()
def get_redis_client():
redis_pool = get_redis_pool()
client = redis.Redis(connection_pool=redis_pool)
return client
# Dependency to create RedisService
async def get_redis_service(
redis_client: redis.Redis = Depends(get_redis_client),
firestore_db: firestore.AsyncClient = Depends(get_firestore_db),
):
from src.services.redis_service import RedisService
return RedisService(redis_client, firestore_db)
Am I missing something in my redis client?
I saw an issue similar to this here but upgrading python to 3.12.9 didn't help.
python 3.12.9
redis 5.2.1
Reacted by Alessio and Shanth Koka
Metadata
Metadata
Assignees
Labels
No labels
Activity
vladvildanov commentedon Mar 7, 2025
@aelamran Hi, I think there might be an issue in which client are you using sync or async? There might be an issue if you're trying to use async client outside of running event loop which lead to the fact that some references are None.
Please check whether you creating a correct instance of client
aelamran commentedon Mar 7, 2025
Sorry, I shared how I am setting my client without sharing the module import, it's indeed async:
import redis.asyncio as redisanuraag-khare commentedon Mar 18, 2025
Hey @aelamran , I am facing the same issues, it happens randomly so canβt properly reproduce it. Did you find any solutions?
diegounreal commentedon Sep 13, 2025
@aelamran @anuraag-khare - this issue seems to be always associated with
Connection reset by peererror (which makes sense - it must be unexpected loss of connection that closes the fd) and is a simple use after close (wrtier fd is closed thus the write function is set to None). This happens randomly, can't repro this locally obviously. From code point of view - can't really see an issue on redis-py side but on the other hand i would imagine bug in the core asyncio would cause more pain in other libraries if this was e.g. missing exception propagation in tasks. This must be caused by some additional env factors.for me this started to happen (or at least maybe i have only noticed it ...) when i upgraded python from
3.10to3.12and redis from4.xto5.x(now6.x- same issues).Apart from key facts:
anything you could share to narrow this down ? will spend some time on this.
lh0156 commentedon Jul 26, 2026
Iβm investigating a narrow fix for the asyncio connection race described here.
When the event loop closes a transport concurrently with a write, asyncio can raise a low-level
TypeErrorfromStreamWriter.writelines()because its internal_write_readycallback has already been cleared. I propose translating only this write-pathAttributeError/TypeErrorinto redis-pyβsConnectionError, after which the existing disconnect-and-retry flow can handle the broken connection consistently.The change would:
writelines();Iβll proceed with this focused approach unless there is a preferred error classification.
petyaslavova commentedon Aug 28, 2026
Fixed with #4288