Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 ActiveRecord throws PG::UnableToSend on Ubuntu 13.04

We have a Ruby v.2.0.0-p247 on Rails v4.0.1 application using pg gem v0.17.0.

The application runs smoothly under Mac OS X Mavericks v10.9 with PostgreSQL Server v9.2.4 installed using HomeBrew but it throws the following exception under Ubuntu v13.04 using PostgreSQL Server 9.1:

PG::UnableToSend: server closed the connection unexpectedly

This probably means the server terminated abnormally before or while processing the request.

The exception occurs after transactional queries (form submission).

I tried the following with database.yml:

  1. Adding reconnect: true
  2. Adding port: 5432
  3. Adding socket: /var/run/postgresq/SOCKET_FILE

And tried the following with PostgreSQL configuration under Ubuntu:

  1. Disabling SSL.
  2. Changing TCP keepalives parameters to pump timeout.
  3. Changing log level to DEBUG and search for possible errors on PostgreSQL Server.

Also tried:

  1. Downgrade to pg gem v0.16.0.
  2. Update all Ubuntu 13.04 packages to latest versions.

What could possible be wrong?

UPDATES:

12/03/2013: Some suggested checking firewall settings. ufw status said that ufw is disabled. 12/08/2013: After trying out with a vanilla Rails app and a lot of mangling with the current application, the problem is originating from rails4/activerecord-session_store gem. Line 47 in lib/active_record/session_store/session.rb is the culprit.

like image 427
Omar Ali Avatar asked Dec 03 '13 11:12

Omar Ali


2 Answers

This basically happens when you use an old version of launchy , and as per this issue on launchy's git repo quoting @infertux

In the rare case when exec fails to run the command - typically when the file cannot be opened raising Errno::ENOENT - Launchy would raise an exception but not showing any output

You can check your Gemfile.lock to see if you're using a version of launchy below 2.4.1, and I doubt that you're using letter_opener Gem in your development environment which depends on launchy so updating letter_opener to 1.2.0 will update launchy to a version above 2.4.0 mostly 2.4.2 which has this issue fixed

All credit goes to @infertux

like image 154
Ahmed Youssef Avatar answered Sep 25 '22 14:09

Ahmed Youssef


It would really help if you provided your database.yml file

The default connection method is a unix domain socket, not to be confused with a TCP/IP socket. The unix domain socket connection method is used by default.

You should check that the unix user that you are trying to run rails under has sufficient permissions to access the domain socket (typically at /var/run/postgresql/.s.PGSQL.5432)

Try typing the following as your rails user:

psql

If you get a database connection error then its likely a permissions problem if postgres is actually running.

You can check your /etc/postgresql.conf file and have postgres configure the group and permissions on the socket when it starts:

unix_socket_directory = '/var/run/postgresql'       # dont worry if yours is different
#unix_socket_group = ''                             # default is usually ok
#unix_socket_permissions = 0777                     # uncomment this if needed

Another option is to add the user to the group that has write access to the socket, vs allowing all users on the machine access with the 0777 permissions setting above. You may want to create a postres_users group for this purpose if the default Ubuntu groups provide insufficient granularity for your needs.

like image 38
Andrew Hacking Avatar answered Sep 26 '22 14:09

Andrew Hacking