Skip to content

Commit

Permalink
Merge pull request rails#54052 from SjoerdL/check-pinned-connection-i…
Browse files Browse the repository at this point in the history
…n-checkout

Fix race condition in ConnectionPool#checkout on @pinned_connection
  • Loading branch information
byroot authored Dec 29, 2024
2 parents 2eec91b + b7a5dc4 commit 92b3160
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,25 @@ def clear_reloadable_connections!
# Raises:
# - ActiveRecord::ConnectionTimeoutError no connection can be obtained from the pool.
def checkout(checkout_timeout = @checkout_timeout)
if @pinned_connection
@pinned_connection.lock.synchronize do
synchronize do
return checkout_and_verify(acquire_connection(checkout_timeout)) unless @pinned_connection

@pinned_connection.lock.synchronize do
synchronize do
# The pinned connection may have been cleaned up before we synchronized, so check if it is still present
if @pinned_connection
@pinned_connection.verify!

# Any leased connection must be in @connections otherwise
# some methods like #connected? won't behave correctly
unless @connections.include?(@pinned_connection)
@connections << @pinned_connection
end

@pinned_connection
else
checkout_and_verify(acquire_connection(checkout_timeout))
end
end
@pinned_connection
else
checkout_and_verify(acquire_connection(checkout_timeout))
end
end

Expand Down

0 comments on commit 92b3160

Please sign in to comment.