Skip to content

Commit

Permalink
Avoid calling closed? outside of synchronize block (#534)
Browse files Browse the repository at this point in the history
* 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 <matt@123mail.org>

---------

Co-authored-by: Matt Brictson <matt@123mail.org>
  • Loading branch information
djmb and mattbrictson authored Apr 15, 2024
1 parent f0b7819 commit 3242c55
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/sshkit/backends/connection_pool/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 3242c55

Please sign in to comment.