diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index 2188481dd97354913190467c0ea922a0f6121912..cbafd1810970257d6e99e4b0c635002556b6e1c6 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -650,6 +650,7 @@ def remove(conn) # or a thread dies unexpectedly. def reap stale_connections = synchronize do + return unless @connections @connections.select do |conn| conn.in_use? && !conn.owner.alive? end.each do |conn| @@ -674,6 +675,7 @@ def flush(minimum_idle = @idle_timeout) return if minimum_idle.nil? idle_connections = synchronize do + return unless @connections @connections.select do |conn| !conn.in_use? && conn.seconds_idle >= minimum_idle end.each do |conn| diff --git a/activerecord/test/cases/reaper_test.rb b/activerecord/test/cases/reaper_test.rb index f8ba819e996e5f33be82252a2c26cc98543f84cd..cb33affd55f7efddb9e8d29ac71ce67be4b15177 100644 --- a/activerecord/test/cases/reaper_test.rb +++ b/activerecord/test/cases/reaper_test.rb @@ -82,6 +82,37 @@ def test_connection_pool_starts_reaper assert_not_predicate conn, :in_use? end + def test_reaper_works_after_pool_discard + spec = ActiveRecord::Base.connection_pool.spec.dup + spec.config[:reaping_frequency] = "0.0001" + + 2.times do + pool = ConnectionPool.new spec + + conn, child = new_conn_in_thread(pool) + + assert_predicate conn, :in_use? + + child.terminate + + wait_for_conn_idle(conn) + assert_not_predicate conn, :in_use? + + pool.discard! + end + end + + # This doesn't test the reaper directly, but we want to test the action + # it would take on a discarded pool + def test_reap_flush_on_discarded_pool + spec = ActiveRecord::Base.connection_pool.spec.dup + pool = ConnectionPool.new spec + + pool.discard! + pool.reap + pool.flush + end + def test_connection_pool_starts_reaper_in_fork spec = ActiveRecord::Base.connection_pool.spec.dup spec.config[:reaping_frequency] = "0.0001"