Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with invoking _close() on closed event loop #3438

Merged
merged 11 commits into from
Dec 5, 2024
8 changes: 7 additions & 1 deletion redis/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
6 changes: 1 addition & 5 deletions redis/asyncio/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/test_asyncio/test_sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
== "<redis.asyncio.sentinel.SentinelManagedConnection,host=127.0.0.1,port=6379)>" # noqa: E501
)
assert connection.connection_pool == pool
await pool.release(connection)

del pool

assert (
str(connection)
== "<redis.asyncio.sentinel.SentinelManagedConnection,host=127.0.0.1,port=6379)>" # noqa: E501
)
Loading