From ae5b957d5cde7d894928601cb8e2bee4fd6fb713 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 | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index bf3432e7eb..3cc9a18d3c 100755 --- a/redis/client.py +++ b/redis/client.py @@ -517,7 +517,18 @@ def __exit__(self, exc_type, exc_value, traceback): self.close() def __del__(self): - self.close() + try: + self.close() + except AttributeError as exc: + if exc.name in ("getpid", "Lock"): + # cf https://github.com/redis/redis-py/issues/3014 + # Most likely means the "os" or "threading" reference in + # connection.py was unloaded before this method was called; this + # can happen on process exit, cf the warning in + # https://docs.python.org/3/reference/datamodel.html#object.__del__ + pass + else: + raise def close(self): # In case a connection property does not yet exist