Skip to content

Commit 2f0ec39

Browse files
committed
fix: make HostConnection.return_connection not to lock connection and
and pool Locking connection and pool at the same time can lead to deadlock. Consider that connection.close also locks connection.
1 parent d540e14 commit 2f0ec39

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

cassandra/pool.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -580,16 +580,18 @@ def return_connection(self, connection, stream_was_orphaned=False):
580580
return
581581
self._is_replacing = True
582582
self._session.submit(self._replace, connection)
583-
else:
584-
if connection in self._trash:
585-
with connection.lock:
586-
if connection.in_flight == len(connection.orphaned_request_ids):
587-
with self._lock:
588-
if connection in self._trash:
589-
self._trash.remove(connection)
590-
log.debug("Closing trashed connection (%s) to %s", id(connection), self.host)
591-
connection.close()
592-
return
583+
elif connection in self._trash:
584+
with connection.lock:
585+
no_pending_requests = connection.in_flight <= len(connection.orphaned_request_ids)
586+
if no_pending_requests:
587+
with self._lock:
588+
close_connection = False
589+
if connection in self._trash:
590+
self._trash.remove(connection)
591+
close_connection = True
592+
if close_connection:
593+
log.debug("Closing trashed connection (%s) to %s", id(connection), self.host)
594+
connection.close()
593595

594596
def on_orphaned_stream_released(self):
595597
"""

0 commit comments

Comments
 (0)