From 29c1f014943b25638a6052f6f3cdb583d6b73935 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 8 Jun 2018 13:16:25 +0800 Subject: [PATCH] Fix broken pool drainer after the upgrade. See https://github.com/rails/rails/blob/6cd5cc375a5c78d08463254460b324a17d078586/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb#L467 We think there is a leak at the moment because a discarded pool belonging to the parent process shouldn't be present in the forked process. --- lib/freedom_patches/pool_drainer.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/freedom_patches/pool_drainer.rb b/lib/freedom_patches/pool_drainer.rb index dc9a2f499ce..583358cc4b8 100644 --- a/lib/freedom_patches/pool_drainer.rb +++ b/lib/freedom_patches/pool_drainer.rb @@ -26,13 +26,15 @@ class ActiveRecord::ConnectionAdapters::ConnectionPool # if idle_time is specified only connections idle for N seconds will be drained def drain(idle_time = nil, max_age = nil) synchronize do - @available.clear - @connections.delete_if do |conn| - try_drain?(conn, idle_time, max_age) - end + if @available && @connections + @available.clear + @connections.delete_if do |conn| + try_drain?(conn, idle_time, max_age) + end - @connections.each do |conn| - @available.add conn if !conn.in_use? + @connections.each do |conn| + @available.add conn if !conn.in_use? + end end end