Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 95fee79

Browse files
committed
Add in safety checks and better connection cleanup in certain situations when executing commands
1 parent 9eb7f62 commit 95fee79

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

rediscluster/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ def _execute_command(self, *args, **kwargs):
668668
)
669669
except TimeoutError:
670670
log.exception("TimeoutError")
671+
connection.disconnect()
671672

672673
if ttl < self.RedisClusterRequestTTL / 2:
673674
time.sleep(0.05)
@@ -679,6 +680,7 @@ def _execute_command(self, *args, **kwargs):
679680
self.connection_pool.disconnect()
680681
self.connection_pool.reset()
681682
self.refresh_table_asap = True
683+
connection = None
682684

683685
raise e
684686
except MovedError as e:
@@ -702,6 +704,10 @@ def _execute_command(self, *args, **kwargs):
702704
log.exception("AskError")
703705

704706
redirect_addr, asking = "{0}:{1}".format(e.host, e.port), True
707+
except BaseException as e:
708+
log.exception("BaseException")
709+
connection.disconnect()
710+
raise e
705711
finally:
706712
if connection is not None:
707713
self.connection_pool.release(connection)

rediscluster/connection.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ def get_connection(self, command_name, *keys, **options):
234234

235235
self._in_use_connections[node['name']].add(connection)
236236

237+
try:
238+
# ensure this connection is connected to Redis
239+
connection.connect()
240+
# connections that the pool provides should be ready to send
241+
# a command. if not, the connection was either returned to the
242+
# pool before all data has been read or the socket has been
243+
# closed. either way, reconnect and verify everything is good.
244+
try:
245+
if connection.can_read():
246+
raise ConnectionError('Connection has data')
247+
except ConnectionError:
248+
connection.disconnect()
249+
connection.connect()
250+
if connection.can_read():
251+
raise ConnectionError('Connection not ready')
252+
except BaseException:
253+
# release the connection back to the pool so that we don't
254+
# leak it
255+
self.release(connection)
256+
raise
257+
237258
return connection
238259

239260
def make_connection(self, node):

0 commit comments

Comments
 (0)