From da048436dd52873c3381474beee969c1a6cd2ab4 Mon Sep 17 00:00:00 2001 From: Nicolas Noirbent Date: Tue, 1 Oct 2024 09:01:33 +0200 Subject: [PATCH] Avoid stacktrace on process exit in Client.__del__() Client.close() may call ConnectionPool.release() or ConnectionPool.disconnect(); both methods may end up calling os.getpid() (through ConnectionPool._checkpid() or threading.Lock() (through ConnectionPool.reset()). As mentioned in the Python documentation [1], at interpreter shutdown, module globals (in this case, the os and threading module references) may be deleted or set to None before __del__() methods are called. This causes an AttributeError to be raised when trying to run e.g. os.getpid(); while the error is ignored by the interpreter, the traceback is still printed out to stderr. Closes #3014 [1] https://docs.python.org/3/reference/datamodel.html#object.__del__ --- redis/client.py | 5 ++++- redis/cluster.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index bf3432e7eb..e746bb3ae6 100755 --- a/redis/client.py +++ b/redis/client.py @@ -517,7 +517,10 @@ def __exit__(self, exc_type, exc_value, traceback): self.close() def __del__(self): - self.close() + try: + self.close() + except Exception: + pass def close(self): # In case a connection property does not yet exist diff --git a/redis/cluster.py b/redis/cluster.py index fbf5428d40..e82af79841 100644 --- a/redis/cluster.py +++ b/redis/cluster.py @@ -664,7 +664,10 @@ def __exit__(self, exc_type, exc_value, traceback): self.close() def __del__(self): - self.close() + try: + self.close() + except Exception: + pass def disconnect_connection_pools(self): for node in self.get_nodes():