Skip to content

Commit

Permalink
ensure reap does not enter infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ttstarck committed Aug 17, 2024
1 parent 7d48bcb commit 20a021b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/connection_pool/timed_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def shutdown(reload: false, &block)
def reap(idle_seconds, &block)
raise ArgumentError, "reap must receive a block" unless block
raise ArgumentError, "idle_seconds must be a number" unless idle_seconds.is_a?(Numeric)
raise ConnectionPool::PoolShuttingDownError if @shutdown_block

loop do
idle.times do
conn =
@mutex.synchronize do
raise ConnectionPool::PoolShuttingDownError if @shutdown_block
Expand Down
16 changes: 16 additions & 0 deletions test/test_connection_pool_timed_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,20 @@ def test_reap_with_multiple_connections_and_idle_seconds_outside_range
assert_empty called
assert_equal 2, stack.idle
end

def test_reap_does_not_loop_continuously
stack = ConnectionPool::TimedStack.new(1) { Object.new }
stack.pop.tap { |conn| stack.push(conn) }

close_attempts = 0
stack.reap(0) do |conn|
if close_attempts >= 1
flunk "Reap is stuck in a loop"
end
close_attempts += 1
stack.push(conn)
end

assert_equal 1, close_attempts
end
end

0 comments on commit 20a021b

Please sign in to comment.