Skip to content

Commit da04843

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

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

redis/client.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,10 @@ 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 Exception:
523+
pass
521524

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

redis/cluster.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,10 @@ def __exit__(self, exc_type, exc_value, traceback):
664664
self.close()
665665

666666
def __del__(self):
667-
self.close()
667+
try:
668+
self.close()
669+
except Exception:
670+
pass
668671

669672
def disconnect_connection_pools(self):
670673
for node in self.get_nodes():

0 commit comments

Comments
 (0)