Skip to content
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
3 changes: 1 addition & 2 deletions redis/asyncio/sentinel.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -117,7 +116,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sort of change has me wonder why it was a weakref in the first place – probably for a reason?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concerns here as @akx. We need to ensure with a test that it doesn't create a circular reference and prevents garbage collecting unused SentinelConnectionPool objects.

Copy link
Collaborator Author

@vladvildanov vladvildanov Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't it already covered by current tests in test_sentinel.py? I mean, GC is always take in action after the tests been executed. I also had a doubts about this, but I didn't find any reason why weakref was used and current tests works as expected. Do you have a specific test case in mind that is worth to add?

self.service_name = service_name
self.sentinel_manager = sentinel_manager
self.master_address = None
Expand Down
Loading