Skip to content

Commit

Permalink
Checkin even stale connections from reap
Browse files Browse the repository at this point in the history
Previously when we reaped connections, we checked if they were inactive
(connected and responding to ping in most adapters) and if so we would
remove the connection instead of checking it back in.

Removing the connection would not wake a thread waiting on the queue for
a connection. This commit changes that to always re-check in the
connection.
  • Loading branch information
jhawthorn committed Nov 16, 2023
1 parent a4d5f1d commit 972ca8f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Expand Up @@ -432,12 +432,8 @@ def reap
end

stale_connections.each do |conn|
if conn.active?
conn.reset!
checkin conn
else
remove conn
end
conn.reset!
checkin conn
end
end

Expand Down
24 changes: 24 additions & 0 deletions activerecord/test/cases/connection_pool_test.rb
Expand Up @@ -215,6 +215,30 @@ def test_reap_inactive
@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!

Expand Down

0 comments on commit 972ca8f

Please sign in to comment.