diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index ddbd22c95d..8c3123ac04 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -214,7 +214,13 @@ def __del__(self, _warnings: Any = warnings): _warnings.warn( f"unclosed Connection {self!r}", ResourceWarning, source=self ) - self._close() + + try: + asyncio.get_running_loop() + self._close() + except RuntimeError: + # No actions been taken if pool already closed. + pass def _close(self): """ diff --git a/redis/asyncio/sentinel.py b/redis/asyncio/sentinel.py index 5d4608ed2f..f1d2cab3f1 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -29,11 +29,7 @@ def __init__(self, **kwargs): super().__init__(**kwargs) def __repr__(self): - pool = self.connection_pool - s = ( - f"<{self.__class__.__module__}.{self.__class__.__name__}" - f"(service={pool.service_name}" - ) + s = f"<{self.__class__.__module__}.{self.__class__.__name__}" if self.host: host_info = f",host={self.host},port={self.port}" s += host_info diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index 51e59d69d0..e553fdb00b 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -264,3 +264,23 @@ async def mock_disconnect(): assert calls == 1 await pool.disconnect() + + +@pytest.mark.onlynoncluster +async def test_repr_correctly_represents_connection_object(sentinel): + pool = SentinelConnectionPool("mymaster", sentinel) + connection = await pool.get_connection("PING") + + assert ( + str(connection) + == "" # noqa: E501 + ) + assert connection.connection_pool == pool + await pool.release(connection) + + del pool + + assert ( + str(connection) + == "" # noqa: E501 + )