Skip to content

GeneratorExit error after setting callback on Redis instance from ConnectionPool (Python 3.13, asyncio, redis 7.1.0, Ubuntu #3856

Closed

Description

@MM-X-C

When using Python 3.13 and redis==7.1.0 on Ubuntu 20.04, a Redis instance obtained via ConnectionPool raises a GeneratorExit error after setting a callback.

 File "/usr/local/lib/python3.13/site-packages/redis/asyncio/connection.py", line 607, in read_response
    response = await self._parser.read_response(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        disable_decoding=disable_decoding
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
GeneratorExit
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/opt/fit-monitor-web/store/redis_store.py", line 18, in wrapper
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/redis/asyncio/client.py", line 725, in execute_command
    return await conn.retry.call_with_retry(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
    )
    ^
  File "/usr/local/lib/python3.13/site-packages/redis/asyncio/retry.py", line 50, in call_with_retry
    return await do()
           ^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/redis/asyncio/client.py", line 700, in _send_command_parse_response
    return await self.parse_response(conn, command_name, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/redis/asyncio/client.py", line 746, in parse_response
    response = await connection.read_response()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/redis/asyncio/connection.py", line 627, in read_response
    await self.disconnect(nowait=True)
  File "/usr/local/lib/python3.13/site-packages/redis/asyncio/connection.py", line 482, in disconnect
    async with async_timeout(self.socket_connect_timeout):
               ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/asyncio/timeouts.py", line 90, in __aenter__
    raise RuntimeError("Timeout should be used inside a task")
RuntimeError: Timeout should be used inside a task

Activity

  1. petyaslavova commented on Nov 26, 2025

    @petyaslavova
    Collaborator

    Hi @MM-X-C, thanks for bringing this up! Can you please add a small code example that reproduces the issue?

  2. MM-X-C commented on Nov 27, 2025

    @MM-X-C
    Author

    Hi @MM-X-C, thanks for bringing this up! Can you please add a small code example that reproduces the issue?

    When I place the callback method in a class and register it via a class method or instance method, the GeneratorExit error occurs within a few minutes of service startup. When I set the callback as a function instead, it hasn't reproduced yet so far—the situation seems improved, but I'm not sure if it will happen again. I don't know why this is the case.

    from redis import asyncio as aioredis
    
    class RedisConnection:
    
        def __init__(self, host, port, decode_responses=True, db=0, pwd=None, **kwargs):
            self.pool = aioredis.ConnectionPool.from_url(
                f"redis://{host}:{port}/{db}",
                password=pwd,
                decode_responses=decode_responses,
                socket_connect_timeout=1,
                retry_on_timeout=True,
            )
            self.r = aioredis.Redis(connection_pool=self.pool)
            if 'response_callbacks' in kwargs:
                for command, callback_func in kwargs['response_callbacks'].items():
                    if hasattr(self, callback_func):
                        self.r.set_response_callback(command, getattr(self, callback_func))
                    else:
                        logger.error(f"No callback for {command}")
    
  3. MM-X-C commented on Nov 28, 2025

    @MM-X-C
    Author

    Hi @MM-X-C, thanks for bringing this up! Can you please add a small code example that reproduces the issue?

    When I place the callback method in a class and register it via a class method or instance method, the GeneratorExit error occurs within a few minutes of service startup. When I set the callback as a function instead, it hasn't reproduced yet so far—the situation seems improved, but I'm not sure if it will happen again. I don't know why this is the case.

    from redis import asyncio as aioredis
    
    class RedisConnection:
    
        def __init__(self, host, port, decode_responses=True, db=0, pwd=None, **kwargs):
            self.pool = aioredis.ConnectionPool.from_url(
                f"redis://{host}:{port}/{db}",
                password=pwd,
                decode_responses=decode_responses,
                socket_connect_timeout=1,
                retry_on_timeout=True,
            )
            self.r = aioredis.Redis(connection_pool=self.pool)
            if 'response_callbacks' in kwargs:
                for command, callback_func in kwargs['response_callbacks'].items():
                    if hasattr(self, callback_func):
                        self.r.set_response_callback(command, getattr(self, callback_func))
                    else:
                        logger.error(f"No callback for {command}")
    

    It turns out that this issue is unrelated to class instances or class methods; callbacks set on standalone functions can also trigger the same problem. After implementing function-level callbacks, it reproduced today.

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