What causes ActiveRecord breaking Postgres connection after forking?
NickName:Koraktor Ask DateTime:2015-01-12T06:47:47

What causes ActiveRecord breaking Postgres connection after forking?

I have a Rake task in a Rails 4.2 project that uses fork. My problem is that after the forked process has finished (i.e. after Process.wait) I get the following Postgres error when I try to access the database again:

PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly

At first I suspected ActiveRecord to automatically close the connection once the forked process finishes. But after reading the code of AR's connection_pool.rb it seems that forked processes should use their own connections:

A connection was established in an ancestor process that must have subsequently forked. We can't reuse the connection, but we can copy the specification and establish a new connection with it.

(from ActiveRecord::ConnectionAdapters::ConnectionHandler#pool_for_owner)

Nevertheless, forking renders the connection useless.

I tried to prevent the forked process from accessing the database at all and verified that the old connections cannot be reused with the following code after forking:

ActiveRecord::Base.default_connection_handler = nil
ActiveRecord::Base.connection_handler = nil

Any suggestions on how to solve this?

Copyright Notice:Content Author:「Koraktor」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27892975/what-causes-activerecord-breaking-postgres-connection-after-forking

More about “What causes ActiveRecord breaking Postgres connection after forking?” related questions

What causes ActiveRecord breaking Postgres connection after forking?

I have a Rake task in a Rails 4.2 project that uses fork. My problem is that after the forked process has finished (i.e. after Process.wait) I get the following Postgres error when I try to access ...

Show Detail

Unable to get through Postgres connection with ActiveRecord

For sake of Lord, I'm unable to understand why postgres is complaining about password authentication(using ActiveRecord). sudo -u postgres psql rdb This work (no password is asked) local all ...

Show Detail

Proper activerecord connection pool size with sidekiq and postgres for multiple sidekiq processes?

I'm running 7 sidekiq processes (currency set to 40) plus a passenger webserver, connecting to a postgres database. Rails pool setting is set to 100 and and postgres max_connections setting is also...

Show Detail

Does phusion passenger use forking, and if so, where to set after_fork config?

Does phusion passenger use forking? If so, where should I set an after_fork configuration, as recommended by Heroku for unicorn (re: Setting up PostGIS with Rails)? From Heroku's docs: Additional...

Show Detail

Forking Django DB connections

I have an application which receives data over a TCP connection and writes it to a postgres database. I then use a django web front end to provide a gui to this data. Since django provides useful

Show Detail

Affected rows for ActiveRecord::Base.connection.execute with Postgres

Is there a way to get the number of affected rows resulting from a SQL operation using ActiveRecord::Base.connection.execute? I found this answer for MySQL adapters, but it doesn't work with Post...

Show Detail

Updating Array in Postgres using ActiveRecord

I'm currently trying to update an array field in Postgres using Rails/ActiveRecord. The following is just one of many iterations of code I've tried: current_acct.acct_status_will_change! current_...

Show Detail

Connection pool issue with ActiveRecord objects in rufus-scheduler

I'm using rufus-scheduler to run a number of frequent jobs that do some various tasks with ActiveRecord objects. If there is any sort of network or postgresql hiccup, even after recovery, all the

Show Detail

Define ActiveRecord connection for given Resque queue

Problem Domain Heroku Cedar stack, with multiple databases. RDS for main database, and Postgres for a second Analytics database. Server runs using the read/write RDS and Postgres databases. Nightl...

Show Detail

ActiveRecord store attribute in rails 4.2 breaking on serialization

I am upgrading a Rails 3 project to Rails 4 and it is breaking on one of my models that uses an ActiveRecord Store property. My model looks like the following: class ModelClass < ActiveRecord::...

Show Detail