Skip to content

Commit d2b739c

Browse files
authored
Merge pull request rails#52235 from fatkodima/fix-deadlock-in-checkout
Fix a deadlock in `ConnectionPool#checkout`
2 parents 21280a1 + 803c78d commit d2b739c

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,14 @@ def clear_reloadable_connections!
541541
# - ActiveRecord::ConnectionTimeoutError no connection can be obtained from the pool.
542542
def checkout(checkout_timeout = @checkout_timeout)
543543
if @pinned_connection
544-
synchronize do
545-
@pinned_connection.verify!
546-
# Any leased connection must be in @connections otherwise
547-
# some methods like #connected? won't behave correctly
548-
unless @connections.include?(@pinned_connection)
549-
@connections << @pinned_connection
544+
@pinned_connection.lock.synchronize do
545+
synchronize do
546+
@pinned_connection.verify!
547+
# Any leased connection must be in @connections otherwise
548+
# some methods like #connected? won't behave correctly
549+
unless @connections.include?(@pinned_connection)
550+
@connections << @pinned_connection
551+
end
550552
end
551553
end
552554
@pinned_connection

0 commit comments

Comments
 (0)