Merge pull request #50190 from jhawthorn/retry_checkout_after_reap

Retry new connection on checkout after reap
This commit is contained in:
John Hawthorn 2023-12-08 17:10:08 -08:00 committed by GitHub
commit c80fa6a069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -668,7 +668,13 @@ module ActiveRecord
conn
else
reap
@available.poll(checkout_timeout)
# Retry after reaping, which may return an available connection,
# remove an inactive connection, or both
if conn = @available.poll || try_to_checkout_new_connection
conn
else
@available.poll(checkout_timeout)
end
end
rescue ConnectionTimeoutError => ex
raise ex.set_pool(self)

View File

@ -216,6 +216,30 @@ module ActiveRecord
@pool.connections.each { |conn| conn.close if conn.in_use? }
end
def test_inactive_are_returned_from_dead_thread
ready = Concurrent::CountDownLatch.new
@pool.instance_variable_set(:@size, 1)
child = new_thread do
@pool.checkout
ready.count_down
stop_thread
end
pass_to(child) until ready.wait(0)
assert_equal 1, active_connections(@pool).size
child.terminate
child.join
@pool.checkout
assert_equal 1, active_connections(@pool).size
ensure
@pool.connections.each { |conn| conn.close if conn.in_use? }
end
def test_idle_timeout_configuration
@pool.disconnect!