Rails Resque workers fail with PGError: server closed the connection unexpectedly

Posted by gc on Stack Overflow See other posts from Stack Overflow or by gc
Published on 2010-04-10T00:27:45Z Indexed on 2010/04/10 0:33 UTC
Read the original article Hit count: 935

Filed under:
|
|
|
|

I have site running rails application and resque workers running in production mode, on Ubuntu 9.10, Rails 2.3.4, ruby-ee 2010.01, PostgreSQL 8.4.2

Workers constantly raised errors: PGError: server closed the connection unexpectedly.

My best guess is that master resque process establishes connection to db (e.g. authlogic does that when use User.acts_as_authentic), while loading rails app classes, and that connection becomes corrupted in fork()ed process (on exit?), so next forked children get kind of broken global ActiveRecord::Base.connection

I could reproduce very similar behaviour with this sample code imitating fork/processing in resque worker. (AFAIK, users of libpq recommended to recreate connections in forked process anyway, otherwise it's not safe )

But, the odd thing is that when I use pgbouncer or pgpool-II instead of direct pgsql connection, such errors do not appear.

So, the question is where and how should I dig to find out why it is broken for plain connection and is working with connection pools? Or reasonable workaround?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rails