Skip to content

Solid Cable doesn't recover from Postgres connection failures #74

Closed

Description

@ebeigarts

How to reproduce:

Run multiple times the following query in psql while the server is running to simulate a database restart or crash:

SELECT pg_terminate_backend(pid) FROM pg_stat_activity;

In the error logs you will see something like this:

15:58:26 web.1    |   SolidCable::Message Load (24.3ms)  SELECT "solid_cable_messages".* FROM "solid_cable_messages" WHERE "solid_cable_messages"."channel_hash" IN (2107490384321251451, -5756308531068617540) AND "solid_cable_messages"."id" >= 18108 ORDER BY "solid_cable_messages"."id" ASC /*application='MitigateChatBot'*/
15:58:26 web.1    |   SolidCable::Message Load (14.1ms)  SELECT "solid_cable_messages".* FROM "solid_cable_messages" WHERE "solid_cable_messages"."channel_hash" IN (2107490384321251451, -5756308531068617540) AND "solid_cable_messages"."id" >= 18108 ORDER BY "solid_cable_messages"."id" ASC /*application='MitigateChatBot'*/
15:58:26 web.1    | #<Thread:0x00000001535cb058@solid_cable_listener /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/solid_cable-3.0.12/lib/action_cable/subscription_adapter/solid_cable.rb:54 run> terminated with exception (report_on_exception is true):
15:58:26 web.1    | /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:167:in 'PG::Connection#exec': PQconsumeInput() FATAL:  terminating connection due to administrator command (ActiveRecord::ConnectionFailed)
15:58:26 web.1    | server closed the connection unexpectedly
15:58:26 web.1    |      This probably means the server terminated abnormally
15:58:26 web.1    |      before or while processing the request.
15:58:26 web.1    |
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:167:in 'ActiveRecord::ConnectionAdapters::PostgreSQL::DatabaseStatements#perform_query'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:571:in 'block (2 levels) in ActiveRecord::ConnectionAdapters::DatabaseStatements#raw_execute'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:1081:in 'block in ActiveRecord::ConnectionAdapters::AbstractAdapter#with_raw_connection'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/rubies/ruby-3.4.5/lib/ruby/gems/3.4.0/gems/activesupport-8.1.1/lib/active_support/concurrency/null_lock.rb:9:in 'ActiveSupport::Concurrency::NullLock#synchronize'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:1050:in 'ActiveRecord::ConnectionAdapters::AbstractAdapter#with_raw_connection'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:570:in 'block in ActiveRecord::ConnectionAdapters::DatabaseStatements#raw_execute'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/rubies/ruby-3.4.5/lib/ruby/gems/3.4.0/gems/activesupport-8.1.1/lib/active_support/notifications/instrumenter.rb:58:in 'ActiveSupport::Notifications::Instrumenter#instrument'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/sentry-rails-6.1.2/lib/sentry/rails/tracing.rb:56:in 'Sentry::Rails::Tracing::SentryNotificationExtension#instrument'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:1201:in 'ActiveRecord::ConnectionAdapters::AbstractAdapter#log'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:569:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#raw_execute'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:613:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#internal_execute'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:555:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#internal_exec_query'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:708:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#select'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:76:in 'ActiveRecord::ConnectionAdapters::DatabaseStatements#select_all'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:275:in 'block in ActiveRecord::ConnectionAdapters::QueryCache#select_all'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:313:in 'block (2 levels) in ActiveRecord::ConnectionAdapters::QueryCache#cache_sql'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:90:in 'ActiveRecord::ConnectionAdapters::QueryCache::Store#compute_if_absent'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:311:in 'block in ActiveRecord::ConnectionAdapters::QueryCache#cache_sql'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/rubies/ruby-3.4.5/lib/ruby/gems/3.4.0/gems/activesupport-8.1.1/lib/active_support/concurrency/null_lock.rb:9:in 'ActiveSupport::Concurrency::NullLock#synchronize'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:310:in 'ActiveRecord::ConnectionAdapters::QueryCache#cache_sql'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:275:in 'ActiveRecord::ConnectionAdapters::QueryCache#select_all'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/querying.rb:68:in 'ActiveRecord::Querying#_query_by_sql'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:1473:in 'block (2 levels) in ActiveRecord::Relation#exec_main_query'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:463:in 'ActiveRecord::ConnectionAdapters::ConnectionPool#with_connection'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/connection_handling.rb:313:in 'ActiveRecord::ConnectionHandling#with_connection'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:1472:in 'block in ActiveRecord::Relation#exec_main_query'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:1494:in 'ActiveRecord::Relation#skip_query_cache_if_necessary'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:1456:in 'ActiveRecord::Relation#exec_main_query'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:1434:in 'block in ActiveRecord::Relation#exec_queries'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:1494:in 'ActiveRecord::Relation#skip_query_cache_if_necessary'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:1428:in 'ActiveRecord::Relation#exec_queries'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:1201:in 'ActiveRecord::Relation#load'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation.rb:353:in 'ActiveRecord::Relation#records'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/activerecord-8.1.1/lib/active_record/relation/delegation.rb:100:in 'ActiveRecord::Delegation#each'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/solid_cable-3.0.12/lib/action_cable/subscription_adapter/solid_cable.rb:128:in 'ActionCable::SubscriptionAdapter::SolidCable::Listener#broadcast_messages'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/solid_cable-3.0.12/lib/action_cable/subscription_adapter/solid_cable.rb:66:in 'block (2 levels) in ActionCable::SubscriptionAdapter::SolidCable::Listener#listen'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/solid_cable-3.0.12/lib/action_cable/subscription_adapter/solid_cable.rb:146:in 'ActionCable::SubscriptionAdapter::SolidCable::Listener#with_polling_volume'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/solid_cable-3.0.12/lib/action_cable/subscription_adapter/solid_cable.rb:66:in 'block in ActionCable::SubscriptionAdapter::SolidCable::Listener#listen'
15:58:26 web.1    |      from <internal:kernel>:168:in 'Kernel#loop'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/solid_cable-3.0.12/lib/action_cable/subscription_adapter/solid_cable.rb:63:in 'ActionCable::SubscriptionAdapter::SolidCable::Listener#listen'
15:58:26 web.1    |      from /Users/ebeigarts/.rvm/gems/ruby-3.4.5@mitigate-chat-bot/gems/solid_cable-3.0.12/lib/action_cable/subscription_adapter/solid_cable.rb:58:in 'block in ActionCable::SubscriptionAdapter::SolidCable::Listener#initialize'

After the query raises an error:

  • There are no more SolidCable::Message Load (24.3ms) SELECT "solid_cable_messages".* queries, because the listener thread has crashed
  • Other requests are working correctly
  • Nothing gets broadcasted until the web server is restarted

Redis adapter listens to server errors and has retries:

https://github.com/rails/rails/blob/main/actioncable/lib/action_cable/subscription_adapter/redis.rb#L167-L172

Postgres adapter does not have any error handling/retries:

https://github.com/rails/rails/blob/main/actioncable/lib/action_cable/subscription_adapter/postgresql.rb#L88-L112

Solid queue adapter also does not have error handling/retries:

https://github.com/rails/solid_cable/blob/main/lib/action_cable/subscription_adapter/solid_cable.rb#L62-L76

It would be great to have similar error handling/retries to Redis adapter in Solid Cable adapter to make it more resilient to database connection issues.

Activity

  1. npezza93 commented on Dec 12, 2025

    @npezza93
    Collaborator

    Hey @ebeigarts, thanks for the report. ill check it out and see what we can do to recover here

  2. ebeigarts commented on Dec 15, 2025

    @ebeigarts
    Author

    Our current workaround:

    # config/initializers/solid_cable.rb
    # Fix Solid Cable doesn't recover from Postgres connection failures
    # TODO Remove when this issue is resolved: https://github.com/rails/solid_cable/issues/74
    module SolidCableDatabaseReconnect
      def broadcast_messages
        super
      rescue ActiveRecord::ActiveRecordError => e
        Rails.logger.error "[SolidCableDatabaseReconnect] Reconnecting to the database..."
        Sentry.capture_exception(e)
        sleep 5
        retry
      end
    end
    
    ActionCable::SubscriptionAdapter::SolidCable::Listener.prepend SolidCableDatabaseReconnect

    I'm not sure if it makes sense to add reconnect_attempts as in redis, I think it should try to recover indefinitely. I think this is what most users expect and would be the best DX.

    Options:

    1. Add reconnect_attempts as in redis, but then it would still be broken after max reconnect attempts are reached :(
    2. Add reconnect_attempts as in redis, but also stop puma after the reconnect_attempts are reached
    3. Add reconnect_attempts as in redis, but also add health checks, so that /up displays an error
    4. Retry indefinitely + Rails.logger.error
  3. InteNs commented on May 21, 2026

    @InteNs

    @npezza93 any idea when we can expect a new gem release with this fix?

    edit: it seems the workaround doesn't fix it for us as we get a ActiveRecord::ConnectionNotEstablished when our database restarts, not ActiveRecord::ConnectionFailed should i create a new issue for this?

  4. npezza93 commented on May 21, 2026

    @npezza93
    Collaborator

    @InteNs Ill cut a release this weekend. I also added connection not established and timeout error to the list of retryable errors in beb66b2. Give main a try again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions