"PG::Error: server closed the connection unexpectedly" on BEGIN transaction on Ubuntu

855 views
Skip to first unread message

Matt Wynne

unread,
Mar 6, 2012, 7:35:50 AM3/6/12
to rub...@googlegroups.com
Hi,

I have a Rails 3.05 app using the PG gem (0.13.1) against postgres 8.4 on Ubuntu 10.04.3 LTS (based on the Vagrant lucid32 box).

I'm experiencing a bug running my specs which I normally only see the first time after the machine boots up. When ActiveRecord calls begin_db_transaction on the ActiveRecord PostgressqlAdapter, I get this error:

     ActiveRecord::StatementInvalid:
       PG::Error: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
       : BEGIN

The full backtrace is here: https://gist.github.com/1986034

When I run the same spec again, it works. I can restart postgres and it still works. The only reliable way I've found to reproduce it is to reboot the server.

I want to understand why this is happening. Can anyone give me any clues?

cheers,
Matt

Matt Wynne

unread,
Mar 6, 2012, 10:31:48 AM3/6/12
to rub...@googlegroups.com
I think I have identified the cause of this. I was using another library (sunspot rails) which forked the existing Rails process. I presume that this forking caused the database connection to be dropped by the server, so when the main Rails process tried to use it, it was gone.

cheers,
Matt


Lars Kanis

unread,
Mar 6, 2012, 11:43:16 AM3/6/12
to rub...@googlegroups.com
Your identified cause sounds plausible. PostgreSQL-docs
(http://www.postgresql.org/docs/9.1/interactive/libpq-connect.html)
say to this:

"On Unix, forking a process with open libpq connections can lead to
unpredictable results because the parent and child processes share the
same sockets and operating system resources. For this reason, such
usage is not recommended, though doing an exec from the child process
to load a new executable is safe."

Rails usually only opens one connection. You may trigger another
connection establishment in the forked process.

Regards,
Lars

2012/3/6 Matt Wynne <ma...@mattwynne.net>:

> --
> You received this message because you are subscribed to the Google Groups
> "ruby-pg" group.
> To post to this group, send email to rub...@googlegroups.com.
> To unsubscribe from this group, send email to
> ruby-pg+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/ruby-pg?hl=en.

Reply all
Reply to author
Forward
0 new messages