Skip to content

Commit 63b1148

Browse files
committed
fix: HostConnectionPool.return_connection not to lock connection and
pool Locking connection and pool can lead to deadlock. Consider that connection.close locks connection.
1 parent 2f0ec39 commit 63b1148

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

cassandra/pool.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,15 +1134,18 @@ def return_connection(self, connection, stream_was_orphaned=False):
11341134
self.shutdown()
11351135
else:
11361136
self._replace(connection)
1137-
else:
1138-
if connection in self._trash:
1139-
with connection.lock:
1140-
if connection.in_flight == 0:
1141-
with self._lock:
1142-
if connection in self._trash:
1143-
self._trash.remove(connection)
1144-
log.debug("Closing trashed connection (%s) to %s", id(connection), self.host)
1145-
connection.close()
1137+
elif connection in self._trash:
1138+
with connection.lock:
1139+
no_pending_requests = connection.in_flight <= len(connection.orphaned_request_ids)
1140+
if no_pending_requests:
1141+
with self._lock:
1142+
close_connection = False
1143+
if connection in self._trash:
1144+
self._trash.remove(connection)
1145+
close_connection = True
1146+
if close_connection:
1147+
log.debug("Closing trashed connection (%s) to %s", id(connection), self.host)
1148+
connection.close()
11461149
return
11471150

11481151
core_conns = self._session.cluster.get_core_connections_per_host(self.host_distance)

0 commit comments

Comments
 (0)