rails: PG::UnableToSend (SSL SYSCALL error: EOF detected)

Hi, I’m seeing intermittent Postgres errors in my rails app:

PG::UnableToSend (SSL SYSCALL error: EOF detected):
  activerecord (4.2.1) lib/active_record/connection_adapters/postgresql_adapter.rb:215:in `exec'
  activerecord (4.2.1) lib/active_record/connection_adapters/postgresql_adapter.rb:215:in `dealloc'
  activerecord (4.2.1) lib/active_record/connection_adapters/postgresql_adapter.rb:198:in `block in clear'
  activerecord (4.2.1) lib/active_record/connection_adapters/postgresql_adapter.rb:197:in `each_value'
  activerecord (4.2.1) lib/active_record/connection_adapters/postgresql_adapter.rb:197:in `clear'
  activerecord (4.2.1) lib/active_record/connection_adapters/postgresql_adapter.rb:258:in `clear_cache!'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract_adapter.rb:291:in `reconnect!'
  activerecord (4.2.1) lib/active_record/connection_adapters/postgresql_adapter.rb:275:in `reconnect!'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract_adapter.rb:328:in `verify!'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:456:in `block in checkout_and_verify'
...

These errors are resulting in very long-running requests. I’ve googled this error, but all the results points to a problem (#12867) that was been fixed as of version 4.1.0, or to problems with gems I’m not using (Sideqik, Sequel). I haven’t found any recent reports of a similar problem.

This is a low-traffic application, and I can’t see any pattern as to when the errors occur, but it may be after a period of inactivity.

I’m running the following:

  • Rails 4.2.1
  • pg 0.18.2
  • ruby 2.2.0
  • PostgreSQL 9.1.11, with max_connections=200, current connections ~90
  • Ubuntu 14.04.2
  • Passenger 5.0.8

Stacktrace here.

Does anyone have any idea what might be causing this, or how to track down a solution? Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

I made the problem go away by adding the following to my database config. But it would still be nice to address the cause of problem.

  variables:
    tcp_keepalives_idle: 60
    tcp_keepalives_interval: 60
    tcp_keepalives_count: 100

In my experience, PG::UnableToSend error has occurred when stateful firewall was terminating opened connection before default TCP keepalive would start. Have solved it by adding PG server-side TCP keepalive configuration options to postgresql.conf.

Example:

tcp_keepalives_count = 10
tcp_keepalives_idle = 30
tcp_keepalives_interval = 30

Two keepalive requests per minute are very cheap, but this can be tweaked either way. Setup can be verified with $ netstat --timers -tn command

This way there’s nothing extra to configure in database.yml and it will work for any PG client type or flavor.