Thanks for all the legwork here @eviltrout it made it way easier to fix.
Fixed per:
https://github.com/discourse/discourse/commit/7ca08216bdf6436764a2b8964d06f4bcd84e5eb4
How I debugged this?
I edited my local copy of active_record/connection_adapters/abstract/connnection_pool.rb
In def checkout I added:
def checkout(checkout_timeout = @checkout_timeout)
c = checkout_and_verify(acquire_connection(checkout_timeout))
puts "CHECKOUT-#{c.object_id}" + "-"* 10
puts caller.grep(/discourse/)
puts "-" * 10
c
end
In def checkin I added:
def checkin(conn)
puts "CHECKIN-#{conn.object_id}" + "-"* 10
puts caller.grep(/discourse/)
puts "-" * 10
...
This revealed 2 spots that were not returning connections to the pool.
Minor problem was on boot, plugins sometimes opened connections to the db without giving connection back to pool.
Major issue was the defer queue that was never releasing its connection (fixed here)
The defer queue exists to kick off lightweight background work we have the is too light to belong in Sidekiq (like increasing user view counts and stuff like that). In a puma environment it runs in a background thread, in a unicorn environment it runs between requests.
Enjoy ![]()