title: Rails ActiveRecord with_connection idle db connections leaked?
body: <p>I have a small rails app that spawns new threads to update itself in the background until a certain condition is met.</p>
  
  <pre><code>class SomeModel &lt; ApplicationRecord
    def self.some_action
      if some_condition
        Thread.new do
          begin
            sleep some_arbitrary_time
            ActiveRecord::Base.connection_pool.with_connection do
              update(attr_x: true)
              some_action
            end
          ensure
            ActiveRecord::Base.connection_pool.release_connection
          end
        end
      end
    end
  end
  </code></pre>
  
  <p>I have tried <code>ActiveRecord::with_connection</code> <code>releasing</code> <code>closing</code> connections, but in the end, there are always idle connections.</p>
  
  <p>Checking the db or ActiveRecord show several idle connections</p>
  
  <pre><code>db_development=# select state from pg_stat_activity where pid &lt;&gt; pg_backend_pid();
   state
  -------
   idle
   idle
   idle
   idle
  
  ActiveRecord::Base.connection_pool.stat
  {"size":10,"connections":5,"busy":1,"dead":0,"idle":4,"waiting":0,"checkout_timeout":5}
  </code></pre>
  
  <p>It is my understanding that the <code>reaping</code> process only takes care of dead connections, idle connections will stay there consuming pool resources.</p>
  
  <p>A more simple example (based of a particular <a href="https://github.com/rails/rails/issues/14470" rel="nofollow noreferrer">rails issue</a>) shows that there are idle processes after the use of new Threads and <code>with_connection</code>.</p>
  
  <p>Before the test</p>
  
  <pre><code>collab-playlist_development=# select state from pg_stat_activity where pid &lt;&gt; pg_backend_pid();
   state
  -------
  (0 rows)
  
  20.times.map do 
    Thread.new do 
      ActiveRecord::Base.connection_pool.with_connection do 
        SomeModel.count 
        end 
      end 
    end
  </code></pre>
  
  <p>After the test</p>
  
  <pre><code>db_development=# select state from pg_stat_activity where pid &lt;&gt; pg_backend_pid();
   state
  -------
   idle
   idle
   idle
   idle
   idle
   idle
   idle
   idle
   idle
   idle
  
  ActiveRecord::Base.connection_pool.stat
  {"size":10,"connections":10,"busy":0,"dead":0,"idle":10,"waiting":0,"checkout_timeout":5}
  </code></pre>
  
  <p>Is there a way to close those idle connections to free up pool resources?</p>
  
  <p>Some references (aside from the rails doc) that shed some light into this issue, but no resolution:</p>
  
  <p>SO</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/23596183/how-to-manage-opening-and-closing-database-connections-while-working-with-active/26733385">How to manage opening and closing database connections while working with activerecords and multiple threads</a></li>
  <li><a href="https://stackoverflow.com/questions/11248808/connection-pool-issue-with-activerecord-objects-in-rufus-scheduler">Connection pool issue with ActiveRecord objects in rufus-scheduler
  </a></li>
  </ul>
  
  <p>Blog posts</p>
  
  <ul>
  <li><a href="http://engineering.midtrans.com/reliable-concurrency-with-rails/" rel="nofollow noreferrer">Reliable concurrency with rails</a></li>
  <li><a href="https://bibwild.wordpress.com/2014/07/17/activerecord-concurrency-in-rails4-avoid-leaked-connections/" rel="nofollow noreferrer">Avoid leaked connections rails 4</a></li>
  </ul>