Skip to content

Commit 84435dd

Browse files
committed
fix: HostConnectionPool._maybe_trash_connection not to lock connection
and pool Locking connection a pool can lead to a deadlock. Consider that connection.close also locks connection. This commit also fixes condition for closing connection right away. Instead of checking there is no in-flight requests it now checks if in-flight requests less or equal than orphaned requests, since orphaned requests still counts as in-flight and can be safely disregarded when connection is dropped.
1 parent 7cc1a6c commit 84435dd

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

cassandra/pool.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,14 +1180,14 @@ def _maybe_trash_connection(self, connection):
11801180
new_connections.remove(connection)
11811181
self._connections = new_connections
11821182

1183-
with connection.lock:
1184-
if connection.in_flight == 0:
1185-
log.debug("Skipping trash and closing unused connection (%s) to %s", id(connection), self.host)
1186-
connection.close()
1187-
1188-
# skip adding it to the trash if we're already closing it
1189-
return
1190-
1183+
if did_trash:
1184+
with connection.lock:
1185+
no_pending_requests = connection.in_flight <= len(connection.orphaned_request_ids)
1186+
if no_pending_requests:
1187+
log.debug("Skipping trash and closing unused connection (%s) to %s", id(connection), self.host)
1188+
connection.close()
1189+
return
1190+
with self._lock:
11911191
self._trash.add(connection)
11921192

11931193
if did_trash:

0 commit comments

Comments
 (0)