Skip to content

Commit fa3a0ca

Browse files
Avoid stacktrace on process exit in Client.__del__() (#3397)
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__ Co-authored-by: petyaslavova <[email protected]>
1 parent ea01a30 commit fa3a0ca

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
@@ -570,7 +570,10 @@ def __exit__(self, exc_type, exc_value, traceback):
570570
self.close()
571571

572572
def __del__(self):
573-
self.close()
573+
try:
574+
self.close()
575+
except Exception:
576+
pass
574577

575578
def close(self) -> None:
576579
# In case a connection property does not yet exist

redis/cluster.py

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

665665
def __del__(self):
666-
self.close()
666+
try:
667+
self.close()
668+
except Exception:
669+
pass
667670

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

0 commit comments

Comments
 (0)