1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #36999 from rails/reaper_fork2

Fix error on reap/flush for closed connection pool
This commit is contained in:
John Hawthorn 2019-08-20 14:00:36 -07:00 committed by GitHub
commit 95cf3d9a57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View file

@ -648,6 +648,7 @@ module ActiveRecord
# 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|
@ -672,6 +673,7 @@ module ActiveRecord
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|

View file

@ -82,6 +82,37 @@ module ActiveRecord
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"