ruby just dies on "ActiveRecord::Base.establish_connection" - ruby-on-rails

this is a follow up to my other question regarding unicorn. following problem:
i'm using ruby 1.8.7, rails 3.0, pg gem (0.13.0).
when i start the rails console in production mode, i can query records etc, so the
database connection and the pgsql adapter works. i can disconnect the AR connection via ActiveRecord::Base.connection.disconnect!, but as soon as i try to reconnect using ActiveRecord::Base.establish_connection, ruby just dies. no error, no log output, no seg fault, it just quits (with status 1). i tried setting the host in my database.yml to the postgres unix socket, as well as to 127.0.0.1, doesn't change a thing. what could be the problem, or what can i do to get to the root of this? i'm not sure how to debug this, with absolutely no output or error message.

i solved the problem (more or less) by downgrading pg to version 0.11.0. this works for me for now. cost me 1 and a 1/2 days ...

Why are you closing explicitely the connection to the database ? Rails/AR handles the connection pool for you. The rack middleware is supposed to release connections at the end of the request processing.

Related

Why does my Rails server exit right away when I try to start it?

It was working just a couple of days ago and then I let my laptop's battery go dead over the weekend while the server was running.
Now when I try to start my rails server with the command
bundle exec rails s
It does a few things and then I get the message
Exiting
/Users/me/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1216:in `initialize': FATAL: no pg_hba.conf entry for host "fe80::1%lo0", user "city72", database "city72_development", SSL off (PG::ConnectionBad)
+ lots more stuff
... then tons more information about .rbenv and railties and active-record and stuff that I don't usually have to deal with.
Any ideas what I might have messed up when my server got shutdown by letting my battery die? Maybe it's just a coincidence that my battery died and something is all of a sudden wrong with my gems?
you probably got a new ip-adress after booting again and the security line in the pg_hba.conf line of the postgresql server no longer matches your address.
You can find this file in the main data folder of your postgresql server. (Well usually at least, the location is configurable)

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:
Adding reconnect: true
Adding port: 5432
Adding socket: /var/run/postgresq/SOCKET_FILE
And tried the following with PostgreSQL configuration under Ubuntu:
Disabling SSL.
Changing TCP keepalives parameters to pump timeout.
Changing log level to DEBUG and search for possible errors on PostgreSQL Server.
Also tried:
Downgrade to pg gem v0.16.0.
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.
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
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.
It looks to me like there's confusion about the connection method. PostgreSQL supports the two methods:
Socket
TCP/IP
These are completely, utterly different. :-)
In your question, you show a socket setting, but mention TCP. I suggest to focus on each of these two ways of connecting in isolation and see which produces results. I.e., create two version branches pg-socket and pg-tcpip just to make things clear. Then, clean up your config file and attempt to connect via both methods.
Often, a socket connection is easier because you just need to know the pathname of the socket "file". No fireware settings are necessary (because no TCP/IP networking is involved.)
Update your question after doing this and tell us how each of the two methods did for you.
Have you set keepalives as specified here http://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS similar issue was solved with it at: https://bitbucket.org/ged/ruby-pg/issue/170/pg-unabletosend-connection-timed-out

Rails Connecting to database specified by database.yml

I recently restarted POW. On reloading the Rails app I'm working on, it hung.
Checking Rails' logs I get the following repeating every 5-20 seconds:
Connecting to database specified by database.yml
I can interact with the Postgres db without issue through the Rails console, so there is no problem there. I haven't changed anything in my database.yml for weeks, so I don't think the problem is there.
What might be the problem and how can I debug it?
Turns out it was caused by POW
I had set '$powder always_restart` which was causing some sort of loop.
I reset this using `$ powder no_restarts'
Change your production database temporary to your development settings.
Then run in your terminal:
rails console production
Most of the times it is not about your database, but probably a syntax error. Anyways you can debug it like this.

Rails produces "PGError: server closed the connection unexpectedly" after some timeout

I have the setup of my Rails app as following:
Rails: 3.0.5 (under Apache proxy) running on RHEL 5.6
Postgres: 8.4, running on Windows Server 2008
The 2 servers are on the same LAN.
The problem is, after some idle time, when I make a new request to the Rails app, it gives me the following error:
ActiveRecord::StatementInvalid (PGError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
From what I have researched, it seems that the database connections are dropped after some timeout by Postgres. During this time, from the Rails side,
if I make a request to Rails (1st request), it will display the connection error as above
if I make another request to Rails (2nd request), Rails seems to reconnect to Postgres and functions correctly.
It means that I will always experience the first connection error then will have all normal operation again, which is very serious in my case since I'd like to deliver a non-error response to my client.
I have looked in following questions and answers, but they do not seem to be appropriate for my case:
"PGError: no connection to the server" after idle (Ubuntu for DB server)
How to use tcp_keepalives settings in Postgresql? (keepalives in MacOS)
Do you have any advice in order to make my app free from db connection errors? Thank you.
We had this problem on Heroku, a lot. As a hackish solution, here's what we did. Put the following in your ApplicationController:
prepend_before_filter :confirm_connection
def confirm_connection
c = ActiveRecord::Base.connection
begin
c.select_all "SELECT 1"
rescue ActiveRecord::StatementInvalid
ActiveRecord::Base.logger.warn "Reconnecting to database"
c.reconnect!
end
end
Basically, tests the connection on each controller hit. Scalable? Not really. But it fixed the problem for us.
In database.yml, do you have the reconnect: true option set for the connection? ex:
production:
adapter: postgresql
database: myapp
username: deploy
password: password
reconnect: true
I'm not sure about the specific error, but without this option you need to handle a class of expected db errors yourself.
I faced this error while going live on production.I just opened my production console and tried to connect the PG db and it worked.
RAILS_ENV=production rails c
User.new
and it worked after this.
hope it helps someone ;)

Mysterious Mongrel Rails crash

Running Mongrel 1.1.5 on Rails 2.1.2 using PostgreSQL 8.3 via ruby-pg 0.7.9 on OS X 10.4 server...
Added restful_authentication and exception_notification, the latter of which appears to be doing me no good when Mongrel simply dies with "Illegal instruction" every time I select /RESOURCE_NAME/new.
Doesn't matter what resource.
In development.log I see it's declaring its intent to render /layouts/RESOURCE_NAME.html.erb and then ... nothing. No stack trace, no anything. server.log is empty.
This is something of a showstopper and I'm not sure what to do. I tried running webbrick, a la:
script/server webbrick --force
But it ignores my wishes and loads mongrel anyway.
Any pointers would be appreciated.
My guess would be that it's dying when it drops from Ruby down into the Postgress C-library to talk to the database. Possible causes of this might be
You've got the ruby-pg gem for a different OS or version somehow.
Your ruby-pg doesn't match the Postgres libs on your system.
Something else...
Illegal instruction usally happens when you try run a binary from an other OS/ABI/Architecture.
Maybe 32bit vs 64bit? Check your libs!

Resources