thuehlinger / daemons

Ruby daemons gem official repository
MIT License
648 stars 71 forks source link

Sequel postgres connection fails when server run as daemon #31

Closed snitko closed 9 years ago

snitko commented 9 years ago

Not sure if this is the right repo to post this issue to, so feel free to remove it.

I've been using Sequel gem (http://sequel.jeremyevans.net/) with postgres adapter inside Goliath, which in turn uses daemons gem and I've noticed a very weird behavior.

If Goliath is run as a daemon, it fails with the following error:

Sequel::DatabaseDisconnectError: PG::ConnectionBad: PQconsumeInput() SSL connection has been closed unexpectedly

whereas if it is run normally, this never happens.

This error also doesn't happen with sqlite, both in normal and daemonized modes. Any hints as to why this might be happening?

sodabrew commented 9 years ago

Are you using Ruby 2.0 or higher? If so, what may be happening is the code is opening a connection to Pg first, then calling daemonize, then trying to use the connection. In Ruby 2.0+, sockets are close-on-exec by default, which means the connection to Pg is getting closed during the fork.

The simplest solution is to open connections after daemonizing, but you may have to rework some error-reporting code because you probably expect the process to exit 1 on the command line if it cannot talk to Pg.

This doesn't happen with sqlite because it will (re)open a local file handle when it needs to (I think - this would match the symptoms).

snitko commented 9 years ago

You are right. I suspected that, but couldn't quite put my finger on it. Thank you.