Skip to content

Commit afb3b81

Browse files
committed
Restore try/except in __del__ methods
Fixed #1339
1 parent 7c0a67a commit afb3b81

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* (in development)
2+
* Restore try/except clauses to __del__ methods. These will be removed
3+
in 4.0 when more explicit resource management if enforced. #1339
14
* 3.5.2 (May 14, 2020)
25
* Tune the locking in ConnectionPool.get_connection so that the lock is
36
not held while waiting for the socket to establish and validate the

redis/client.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,10 +3410,13 @@ def __exit__(self, exc_type, exc_value, traceback):
34103410
self.reset()
34113411

34123412
def __del__(self):
3413-
# if this object went out of scope prior to shutting down
3414-
# subscriptions, close the connection manually before
3415-
# returning it to the connection pool
3416-
self.reset()
3413+
try:
3414+
# if this object went out of scope prior to shutting down
3415+
# subscriptions, close the connection manually before
3416+
# returning it to the connection pool
3417+
self.reset()
3418+
except Exception:
3419+
pass
34173420

34183421
def reset(self):
34193422
if self.connection:
@@ -3762,7 +3765,10 @@ def __exit__(self, exc_type, exc_value, traceback):
37623765
self.reset()
37633766

37643767
def __del__(self):
3765-
self.reset()
3768+
try:
3769+
self.reset()
3770+
except Exception:
3771+
pass
37663772

37673773
def __len__(self):
37683774
return len(self.command_stack)

redis/connection.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ def __init__(self, socket_read_size):
296296
self._buffer = None
297297

298298
def __del__(self):
299-
self.on_disconnect()
299+
try:
300+
self.on_disconnect()
301+
except Exception:
302+
pass
300303

301304
def on_connect(self, connection):
302305
"Called when the socket connects"
@@ -374,7 +377,10 @@ def __init__(self, socket_read_size):
374377
self._buffer = bytearray(socket_read_size)
375378

376379
def __del__(self):
377-
self.on_disconnect()
380+
try:
381+
self.on_disconnect()
382+
except Exception:
383+
pass
378384

379385
def on_connect(self, connection):
380386
self._sock = connection._sock
@@ -534,7 +540,10 @@ def repr_pieces(self):
534540
return pieces
535541

536542
def __del__(self):
537-
self.disconnect()
543+
try:
544+
self.disconnect()
545+
except Exception:
546+
pass
538547

539548
def register_connect_callback(self, callback):
540549
self._connect_callbacks.append(callback)

0 commit comments

Comments
 (0)