Skip to content

Commit ae5b957

Browse files
committed
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 redis#3014 [1] https://docs.python.org/3/reference/datamodel.html#object.__del__
1 parent 2e46613 commit ae5b957

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Diff for: redis/client.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,18 @@ def __exit__(self, exc_type, exc_value, traceback):
517517
self.close()
518518

519519
def __del__(self):
520-
self.close()
520+
try:
521+
self.close()
522+
except AttributeError as exc:
523+
if exc.name in ("getpid", "Lock"):
524+
# cf https://github.com/redis/redis-py/issues/3014
525+
# Most likely means the "os" or "threading" reference in
526+
# connection.py was unloaded before this method was called; this
527+
# can happen on process exit, cf the warning in
528+
# https://docs.python.org/3/reference/datamodel.html#object.__del__
529+
pass
530+
else:
531+
raise
521532

522533
def close(self):
523534
# In case a connection property does not yet exist

0 commit comments

Comments
 (0)