Skip to content

Commit

Permalink
Fixed issue with invoking _close() on closed event loop (#3438)
Browse files Browse the repository at this point in the history
* Fixed issue with invoking _close() on closed event loop

* Removed unused import

* Revert weakref changes

* Codestyle fix

* Added test coverage

* Codestyle fixes

* Codestyle fixes

* Removed failure check that fails in 3.12

* Codestyle fixes

* Codestyle fixes
  • Loading branch information
vladvildanov authored Dec 5, 2024
1 parent db8918c commit 5043b69
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
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
)

0 comments on commit 5043b69

Please sign in to comment.