Skip to content

self._write_ready() 'NoneType' object is not callableΒ #3546

Closed

Description

@aelamran
 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

Activity

  1. vladvildanov commented on Mar 7, 2025

    @vladvildanov
    Collaborator

    @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

  2. aelamran commented on Mar 7, 2025

    @aelamran
    Author

    @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

    Sorry, I shared how I am setting my client without sharing the module import, it's indeed async:
    import redis.asyncio as redis

  3. anuraag-khare commented on Mar 18, 2025

    @anuraag-khare

    Hey @aelamran , I am facing the same issues, it happens randomly so can’t properly reproduce it. Did you find any solutions?

  4. diegounreal commented on Sep 13, 2025

    @diegounreal

    @aelamran @anuraag-khare - this issue seems to be always associated with Connection reset by peer error (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.10 to 3.12 and redis from 4.x to 5.x (now 6.x - same issues).

    Apart from key facts:

    • async code
    • redis-py version > 4
    • python version 3.12.x
    • Docker (python slim ?)
    • most likely running in cloud

    anything you could share to narrow this down ? will spend some time on this.

  5. lh0156 commented on Jul 26, 2026

    @lh0156
    Contributor

    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 TypeError from StreamWriter.writelines() because its internal _write_ready callback has already been cleared. I propose translating only this write-path AttributeError/TypeError into redis-py’s ConnectionError, after which the existing disconnect-and-retry flow can handle the broken connection consistently.

    The change would:

    • keep the current writer path and retry policy unchanged;
    • close the connection through the existing error path;
    • add a deterministic async regression test with a writer whose callback disappears during writelines();
    • avoid catching unrelated application/type errors outside the socket write path.

    I’ll proceed with this focused approach unless there is a preferred error classification.

  6. petyaslavova commented on Aug 28, 2026

    @petyaslavova
    Collaborator

    Fixed with #4288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions