Skip to content

Commit

Permalink
ensure reap does not enter infinite loop (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttstarck authored Aug 26, 2024
1 parent 7d48bcb commit 4fcffb0
Show file tree
Hide file tree
Showing 2 changed files with 19 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
17 changes: 17 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,21 @@ 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(2) { Object.new }
stack.push(Object.new)
stack.push(Object.new)

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

assert_equal 2, close_attempts
end
end

0 comments on commit 4fcffb0

Please sign in to comment.