Skip to content

Commit

Permalink
Restore try/except in __del__ methods
Browse files Browse the repository at this point in the history
Fixed #1339
  • Loading branch information
andymccurdy committed May 20, 2020
1 parent 7c0a67a commit afb3b81
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* (in development)
* Restore try/except clauses to __del__ methods. These will be removed
in 4.0 when more explicit resource management if enforced. #1339
* 3.5.2 (May 14, 2020)
* Tune the locking in ConnectionPool.get_connection so that the lock is
not held while waiting for the socket to establish and validate the
Expand Down
16 changes: 11 additions & 5 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3410,10 +3410,13 @@ def __exit__(self, exc_type, exc_value, traceback):
self.reset()

def __del__(self):
# if this object went out of scope prior to shutting down
# subscriptions, close the connection manually before
# returning it to the connection pool
self.reset()
try:
# if this object went out of scope prior to shutting down
# subscriptions, close the connection manually before
# returning it to the connection pool
self.reset()
except Exception:
pass

def reset(self):
if self.connection:
Expand Down Expand Up @@ -3762,7 +3765,10 @@ def __exit__(self, exc_type, exc_value, traceback):
self.reset()

def __del__(self):
self.reset()
try:
self.reset()
except Exception:
pass

def __len__(self):
return len(self.command_stack)
Expand Down
15 changes: 12 additions & 3 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ def __init__(self, socket_read_size):
self._buffer = None

def __del__(self):
self.on_disconnect()
try:
self.on_disconnect()
except Exception:
pass

def on_connect(self, connection):
"Called when the socket connects"
Expand Down Expand Up @@ -374,7 +377,10 @@ def __init__(self, socket_read_size):
self._buffer = bytearray(socket_read_size)

def __del__(self):
self.on_disconnect()
try:
self.on_disconnect()
except Exception:
pass

def on_connect(self, connection):
self._sock = connection._sock
Expand Down Expand Up @@ -534,7 +540,10 @@ def repr_pieces(self):
return pieces

def __del__(self):
self.disconnect()
try:
self.disconnect()
except Exception:
pass

def register_connect_callback(self, callback):
self._connect_callbacks.append(callback)
Expand Down

0 comments on commit afb3b81

Please sign in to comment.