Skip to content

Commit 5043b69

Browse files
authored
Fixed issue with invoking _close() on closed event loop (#3438)
* 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
1 parent db8918c commit 5043b69

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

redis/asyncio/connection.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,13 @@ def __del__(self, _warnings: Any = warnings):
214214
_warnings.warn(
215215
f"unclosed Connection {self!r}", ResourceWarning, source=self
216216
)
217-
self._close()
217+
218+
try:
219+
asyncio.get_running_loop()
220+
self._close()
221+
except RuntimeError:
222+
# No actions been taken if pool already closed.
223+
pass
218224

219225
def _close(self):
220226
"""

redis/asyncio/sentinel.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ def __init__(self, **kwargs):
2929
super().__init__(**kwargs)
3030

3131
def __repr__(self):
32-
pool = self.connection_pool
33-
s = (
34-
f"<{self.__class__.__module__}.{self.__class__.__name__}"
35-
f"(service={pool.service_name}"
36-
)
32+
s = f"<{self.__class__.__module__}.{self.__class__.__name__}"
3733
if self.host:
3834
host_info = f",host={self.host},port={self.port}"
3935
s += host_info

tests/test_asyncio/test_sentinel.py

+20
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,23 @@ async def mock_disconnect():
264264

265265
assert calls == 1
266266
await pool.disconnect()
267+
268+
269+
@pytest.mark.onlynoncluster
270+
async def test_repr_correctly_represents_connection_object(sentinel):
271+
pool = SentinelConnectionPool("mymaster", sentinel)
272+
connection = await pool.get_connection("PING")
273+
274+
assert (
275+
str(connection)
276+
== "<redis.asyncio.sentinel.SentinelManagedConnection,host=127.0.0.1,port=6379)>" # noqa: E501
277+
)
278+
assert connection.connection_pool == pool
279+
await pool.release(connection)
280+
281+
del pool
282+
283+
assert (
284+
str(connection)
285+
== "<redis.asyncio.sentinel.SentinelManagedConnection,host=127.0.0.1,port=6379)>" # noqa: E501
286+
)

0 commit comments

Comments
 (0)