From 3242c55680da8c63249413bedc5ea033f87cb811 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Mon, 15 Apr 2024 13:03:27 -0400 Subject: [PATCH] Avoid calling closed? outside of synchronize block (#534) * Avoid calling closed? outside of synchronize block `closed?` calls `process` on the connection which is not safe because we have not synchronised the connection pool. Another thread might concurrently checkout the connection and start sending commands as well. * Update lib/sshkit/backends/connection_pool/cache.rb Co-authored-by: Matt Brictson --------- Co-authored-by: Matt Brictson --- lib/sshkit/backends/connection_pool/cache.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sshkit/backends/connection_pool/cache.rb b/lib/sshkit/backends/connection_pool/cache.rb index 11a09075..df09a29f 100644 --- a/lib/sshkit/backends/connection_pool/cache.rb +++ b/lib/sshkit/backends/connection_pool/cache.rb @@ -36,8 +36,8 @@ def push(conn) def evict # Peek at the first connection to see if it is still fresh. If so, we can # return right away without needing to use `synchronize`. - first_expires_at, first_conn = connections.first - return if (first_expires_at.nil? || fresh?(first_expires_at)) && !closed?(first_conn) + first_expires_at, _first_conn = connections.first + return if (first_expires_at.nil? || fresh?(first_expires_at)) connections.synchronize do fresh, stale = connections.partition do |expires_at, conn|