Skip to content

Commit 8120ed5

Browse files
committed
Make resource-warning __del__ code safer during shutdown
1 parent 59c4cf0 commit 8120ed5

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

redis/asyncio/client.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,18 @@ async def __aexit__(self, exc_type, exc_value, traceback):
480480

481481
_DEL_MESSAGE = "Unclosed Redis client"
482482

483-
def __del__(self, _warnings: Any = warnings) -> None:
483+
# passing _warnings and _grl as argument default since they may be gone
484+
# by the time __del__ is called at shutdown
485+
def __del__(
486+
self, _warn: Any = warnings.warn, _grl: Any = asyncio.get_running_loop
487+
) -> None:
484488
if hasattr(self, "connection") and (self.connection is not None):
485-
_warnings.warn(
486-
f"Unclosed client session {self!r}", ResourceWarning, source=self
487-
)
488-
context = {"client": self, "message": self._DEL_MESSAGE}
489-
asyncio.get_running_loop().call_exception_handler(context)
489+
_warn(f"Unclosed client session {self!r}", ResourceWarning, source=self)
490+
try:
491+
context = {"client": self, "message": self._DEL_MESSAGE}
492+
_grl().call_exception_handler(context)
493+
except RuntimeError:
494+
pass
490495

491496
async def close(self, close_connection_pool: Optional[bool] = None) -> None:
492497
"""

redis/asyncio/cluster.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,14 +418,16 @@ def __await__(self) -> Generator[Any, None, "RedisCluster"]:
418418

419419
_DEL_MESSAGE = "Unclosed RedisCluster client"
420420

421-
def __del__(self) -> None:
421+
def __del__(
422+
self, _warn: Any = warnings.warn, _grl: Any = asyncio.get_running_loop
423+
) -> None:
422424
if hasattr(self, "_initialize") and not self._initialize:
423-
warnings.warn(f"{self._DEL_MESSAGE} {self!r}", ResourceWarning, source=self)
425+
_warn(f"{self._DEL_MESSAGE} {self!r}", ResourceWarning, source=self)
424426
try:
425427
context = {"client": self, "message": self._DEL_MESSAGE}
426-
asyncio.get_running_loop().call_exception_handler(context)
428+
_grl().call_exception_handler(context)
427429
except RuntimeError:
428-
...
430+
pass
429431

430432
async def on_connect(self, connection: Connection) -> None:
431433
await connection.on_connect()
@@ -954,17 +956,18 @@ def __eq__(self, obj: Any) -> bool:
954956

955957
_DEL_MESSAGE = "Unclosed ClusterNode object"
956958

957-
def __del__(self) -> None:
959+
def __del__(
960+
self, _warn: Any = warnings.warn, _grl: Any = asyncio.get_running_loop
961+
) -> None:
958962
for connection in self._connections:
959963
if connection.is_connected:
960-
warnings.warn(
961-
f"{self._DEL_MESSAGE} {self!r}", ResourceWarning, source=self
962-
)
964+
_warn(f"{self._DEL_MESSAGE} {self!r}", ResourceWarning, source=self)
965+
963966
try:
964967
context = {"client": self, "message": self._DEL_MESSAGE}
965-
asyncio.get_running_loop().call_exception_handler(context)
968+
_grl().call_exception_handler(context)
966969
except RuntimeError:
967-
...
970+
pass
968971
break
969972

970973
async def disconnect(self) -> None:

0 commit comments

Comments
 (0)