From ecf10fe16eb381791adff407417da23aaeee6add Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Mon, 18 Nov 2024 11:24:57 +0200 Subject: [PATCH 01/10] Fixed issue with invoking _close() on closed event loop --- redis/asyncio/connection.py | 8 +++++++- redis/asyncio/sentinel.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) 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..d0e953ec5b 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -117,7 +117,7 @@ def __init__(self, service_name, sentinel_manager, **kwargs): self.is_master = kwargs.pop("is_master", True) self.check_connection = kwargs.pop("check_connection", False) super().__init__(**kwargs) - self.connection_kwargs["connection_pool"] = weakref.proxy(self) + self.connection_kwargs["connection_pool"] = self self.service_name = service_name self.sentinel_manager = sentinel_manager self.master_address = None From 4ba798ac7bfe66cc670c92bbe3a23c9b52e16464 Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Mon, 18 Nov 2024 11:33:09 +0200 Subject: [PATCH 02/10] Removed unused import --- redis/asyncio/sentinel.py | 1 - 1 file changed, 1 deletion(-) diff --git a/redis/asyncio/sentinel.py b/redis/asyncio/sentinel.py index d0e953ec5b..71ac16f8a4 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -1,6 +1,5 @@ import asyncio import random -import weakref from typing import AsyncIterator, Iterable, Mapping, Optional, Sequence, Tuple, Type from redis.asyncio.client import Redis From c0d24e6d10389bffe50e1a47502b43b05ac96651 Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Thu, 5 Dec 2024 11:31:12 +0200 Subject: [PATCH 03/10] Revert weakref changes --- redis/asyncio/sentinel.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/redis/asyncio/sentinel.py b/redis/asyncio/sentinel.py index 71ac16f8a4..97f8ddbcfe 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -1,5 +1,6 @@ import asyncio import random +import weakref from typing import AsyncIterator, Iterable, Mapping, Optional, Sequence, Tuple, Type from redis.asyncio.client import Redis @@ -28,10 +29,8 @@ 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}" ) if self.host: host_info = f",host={self.host},port={self.port}" @@ -116,7 +115,7 @@ def __init__(self, service_name, sentinel_manager, **kwargs): self.is_master = kwargs.pop("is_master", True) self.check_connection = kwargs.pop("check_connection", False) super().__init__(**kwargs) - self.connection_kwargs["connection_pool"] = self + self.connection_kwargs["connection_pool"] = weakref.proxy(self) self.service_name = service_name self.sentinel_manager = sentinel_manager self.master_address = None From 05e48d3f1b0cc44c44cb6647e8f5a132e70eb4d1 Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Thu, 5 Dec 2024 11:37:14 +0200 Subject: [PATCH 04/10] Codestyle fix --- redis/asyncio/sentinel.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/redis/asyncio/sentinel.py b/redis/asyncio/sentinel.py index 97f8ddbcfe..f1d2cab3f1 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -29,9 +29,7 @@ def __init__(self, **kwargs): super().__init__(**kwargs) def __repr__(self): - s = ( - f"<{self.__class__.__module__}.{self.__class__.__name__}" - ) + s = f"<{self.__class__.__module__}.{self.__class__.__name__}" if self.host: host_info = f",host={self.host},port={self.port}" s += host_info From dcb2d981ffcf737aa44392f3fed359ba49d5a896 Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Thu, 5 Dec 2024 12:16:14 +0200 Subject: [PATCH 05/10] Added test coverage --- tests/test_asyncio/test_sentinel.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index 51e59d69d0..be8bb95224 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -264,3 +264,27 @@ 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) + == "" + ) + assert connection.connection_pool == pool + await pool.release(connection) + + del pool + + assert ( + str(connection) + == "" + ) + with pytest.raises( + ReferenceError, match="weakly-referenced object no longer exists" + ): + assert connection.connection_pool From c403a9f888d47de6510e9f9b1726786536ad3fab Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Thu, 5 Dec 2024 12:20:21 +0200 Subject: [PATCH 06/10] Codestyle fixes --- tests/test_asyncio/test_sentinel.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index be8bb95224..a0ccb7fa22 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -272,8 +272,8 @@ async def test_repr_correctly_represents_connection_object(sentinel): connection = await pool.get_connection("PING") assert ( - str(connection) - == "" + str(connection) + == "" # noqa: E501 ) assert connection.connection_pool == pool await pool.release(connection) @@ -281,10 +281,10 @@ async def test_repr_correctly_represents_connection_object(sentinel): del pool assert ( - str(connection) - == "" + str(connection) + == "" # noqa: E501 ) with pytest.raises( - ReferenceError, match="weakly-referenced object no longer exists" + ReferenceError, match="weakly-referenced object no longer exists" ): assert connection.connection_pool From fa3889218743a13b94d3bd8b5661dd415153adec Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Thu, 5 Dec 2024 12:22:24 +0200 Subject: [PATCH 07/10] Codestyle fixes --- tests/test_asyncio/test_sentinel.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index a0ccb7fa22..fe5bd22902 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -272,8 +272,8 @@ async def test_repr_correctly_represents_connection_object(sentinel): connection = await pool.get_connection("PING") assert ( - str(connection) - == "" # noqa: E501 + str(connection) + == "" # noqa: E501 ) assert connection.connection_pool == pool await pool.release(connection) @@ -281,10 +281,10 @@ async def test_repr_correctly_represents_connection_object(sentinel): del pool assert ( - str(connection) - == "" # noqa: E501 + str(connection) + == "" # noqa: E501 ) with pytest.raises( - ReferenceError, match="weakly-referenced object no longer exists" + ReferenceError, match="weakly-referenced object no longer exists" ): assert connection.connection_pool From f5040d80230db8af99a706e00b64a0305e8c8892 Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Thu, 5 Dec 2024 12:41:45 +0200 Subject: [PATCH 08/10] Removed failure check that fails in 3.12 --- tests/test_asyncio/test_sentinel.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index fe5bd22902..a83758283c 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -1,4 +1,5 @@ import socket +import weakref from unittest import mock import pytest @@ -283,8 +284,4 @@ async def test_repr_correctly_represents_connection_object(sentinel): assert ( str(connection) == "" # noqa: E501 - ) - with pytest.raises( - ReferenceError, match="weakly-referenced object no longer exists" - ): - assert connection.connection_pool + ) \ No newline at end of file From cc619c05a7729fc4bd88d0229c29488a903349c0 Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Thu, 5 Dec 2024 12:43:43 +0200 Subject: [PATCH 09/10] Codestyle fixes --- tests/test_asyncio/test_sentinel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index a83758283c..85128cb88c 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -284,4 +284,4 @@ async def test_repr_correctly_represents_connection_object(sentinel): assert ( str(connection) == "" # noqa: E501 - ) \ No newline at end of file + ) From bd71391806f1c6d70a86bb89046f0fe4b1c91734 Mon Sep 17 00:00:00 2001 From: vladvildanov Date: Thu, 5 Dec 2024 12:45:30 +0200 Subject: [PATCH 10/10] Codestyle fixes --- tests/test_asyncio/test_sentinel.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index 85128cb88c..e553fdb00b 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -1,5 +1,4 @@ import socket -import weakref from unittest import mock import pytest