We have discovered that every time a unicorn worker is restarted, rails throws
"PG::ConnectionBad: connection is closed” errors.
We are using preload: true, and have before and after fork rules to close and
reopen the connections:
---------------------------------------------------
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if File.exist?(old_pid) && server.pid != old_pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |_server, _worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
child_pid = _server.config[:pid].sub('.pid', ".#{_worker.nr}.pid")
system("echo #{Process.pid} > #{child_pid}")
end
---------------------------------------------------
Yet it seems that something is holding onto a dead connection and trying to use
it.
We have definitely correlated it to worker restarts - we have a monit process
in place to restart individual workers if they exceeded a memory threshold, and
when this number was too low, they were getting recycled often and we saw a
very high number of these errors. When we raised the threshold, the error
almost completely disappeared (but it still happens sometimes when a worker is
recycled).
How can we troubleshoot this?
Thanks!