Skip to content

Commit 905fa8a

Browse files
committed
Make resource-warning __del__ code safer during shutdown
1 parent 1426399 commit 905fa8a

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
@@ -531,13 +531,18 @@ async def __aexit__(self, exc_type, exc_value, traceback):
531531

532532
_DEL_MESSAGE = "Unclosed Redis client"
533533

534-
def __del__(self, _warnings: Any = warnings) -> None:
534+
# passing _warnings and _grl as argument default since they may be gone
535+
# by the time __del__ is called at shutdown
536+
def __del__(
537+
self, _warn: Any = warnings.warn, _grl: Any = asyncio.get_running_loop
538+
) -> None:
535539
if hasattr(self, "connection") and (self.connection is not None):
536-
_warnings.warn(
537-
f"Unclosed client session {self!r}", ResourceWarning, source=self
538-
)
539-
context = {"client": self, "message": self._DEL_MESSAGE}
540-
asyncio.get_running_loop().call_exception_handler(context)
540+
_warn(f"Unclosed client session {self!r}", ResourceWarning, source=self)
541+
try:
542+
context = {"client": self, "message": self._DEL_MESSAGE}
543+
_grl().call_exception_handler(context)
544+
except RuntimeError:
545+
pass
541546

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

redis/asyncio/cluster.py

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

423423
_DEL_MESSAGE = "Unclosed RedisCluster client"
424424

425-
def __del__(self) -> None:
425+
def __del__(
426+
self, _warn: Any = warnings.warn, _grl: Any = asyncio.get_running_loop
427+
) -> None:
426428
if hasattr(self, "_initialize") and not self._initialize:
427-
warnings.warn(f"{self._DEL_MESSAGE} {self!r}", ResourceWarning, source=self)
429+
_warn(f"{self._DEL_MESSAGE} {self!r}", ResourceWarning, source=self)
428430
try:
429431
context = {"client": self, "message": self._DEL_MESSAGE}
430-
asyncio.get_running_loop().call_exception_handler(context)
432+
_grl().call_exception_handler(context)
431433
except RuntimeError:
432-
...
434+
pass
433435

434436
async def on_connect(self, connection: Connection) -> None:
435437
await connection.on_connect()
@@ -958,17 +960,18 @@ def __eq__(self, obj: Any) -> bool:
958960

959961
_DEL_MESSAGE = "Unclosed ClusterNode object"
960962

961-
def __del__(self) -> None:
963+
def __del__(
964+
self, _warn: Any = warnings.warn, _grl: Any = asyncio.get_running_loop
965+
) -> None:
962966
for connection in self._connections:
963967
if connection.is_connected:
964-
warnings.warn(
965-
f"{self._DEL_MESSAGE} {self!r}", ResourceWarning, source=self
966-
)
968+
_warn(f"{self._DEL_MESSAGE} {self!r}", ResourceWarning, source=self)
969+
967970
try:
968971
context = {"client": self, "message": self._DEL_MESSAGE}
969-
asyncio.get_running_loop().call_exception_handler(context)
972+
_grl().call_exception_handler(context)
970973
except RuntimeError:
971-
...
974+
pass
972975
break
973976

974977
async def disconnect(self) -> None:

0 commit comments

Comments
 (0)