unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* Dead PostgreSQL connections on worker restart
@ 2016-04-15  3:26 Adam Fields
  2016-04-15  5:42 ` Eric Wong
  0 siblings, 1 reply; 3+ messages in thread
From: Adam Fields @ 2016-04-15  3:26 UTC (permalink / raw)
  To: unicorn-public

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!



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-04-21 19:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-15  3:26 Dead PostgreSQL connections on worker restart Adam Fields
2016-04-15  5:42 ` Eric Wong
2016-04-21 19:42   ` Eric Wong

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).