unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Adam Fields <unicorn5958@street86.com>
To: unicorn-public@bogomips.org
Subject: Dead PostgreSQL connections on worker restart
Date: Thu, 14 Apr 2016 23:26:24 -0400	[thread overview]
Message-ID: <E6592304-8CB6-46AB-BDF6-CCAC1732219D@street86.com> (raw)

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!



             reply	other threads:[~2016-04-15  3:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-15  3:26 Adam Fields [this message]
2016-04-15  5:42 ` Dead PostgreSQL connections on worker restart Eric Wong
2016-04-21 19:42   ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E6592304-8CB6-46AB-BDF6-CCAC1732219D@street86.com \
    --to=unicorn5958@street86.com \
    --cc=unicorn-public@bogomips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).