Skip to content

Error: Event loop is closed when running asyncio + sentinel examples #3431

Closed
@pedroplytix

Description

@pedroplytix

Version: redis-py: 5.2 / redis 6.2.7

Platform: Python 3.12.7 on Debian (Docker container: python:3.12.7-slim-bookworm)

Description: Running the example provided in redis-py documentation to connect to sentinel using asyncio:

import asyncio
from redis.asyncio.sentinel import Sentinel

async def main():
    sentinel = Sentinel([("localhost", 26379), ("sentinel2", 26379)])
    r = sentinel.master_for("mymaster")

    ok = await r.set("key", "value")
    assert ok
    val = await r.get("key")
    assert val == b"value"


if __name__ == "__main__":
    asyncio.run(main())

I get this error:

Exception ignored in: <function AbstractConnection.__del__ at 0x7f53bc6e14e0>
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/redis/asyncio/connection.py", line 215, in __del__
    f"unclosed Connection {self!r}", ResourceWarning, source=self
                          ^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/redis/asyncio/sentinel.py", line 35, in __repr__
    f"(service={pool.service_name}"
                ^^^^^^^^^^^^^^^^^
ReferenceError: weakly-referenced object no longer exists

If I try to close the connection after assert val == b"value":

...
    assert val == b"value"
    await r.aclose()
...

I get this error:

Exception ignored in: <function AbstractConnection.__del__ at 0x7ff6587ed580>
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/redis/asyncio/connection.py", line 217, in __del__
    self._close()
  File "/usr/local/lib/python3.12/site-packages/redis/asyncio/connection.py", line 224, in _close
    self._writer.close()
  File "/usr/local/lib/python3.12/asyncio/streams.py", line 358, in close
    return self._transport.close()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/selector_events.py", line 1210, in close
    super().close()
  File "/usr/local/lib/python3.12/asyncio/selector_events.py", line 875, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 795, in call_soon
    self._check_closed()
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 541, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

Checking with older redis-py versions, I've checked that this code worked fine in redis==5.0.0 and redis==5.0.1, but started to fail in redis==5.0.2

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