Skip to content

SolidCable stops to work after some time #71

Closed

Description

@alanoliveira

I have a small application I was checking how does it behave using solid_cable instead of redis, but with solid_cable it stops to send messages after some time (sometimes ~30 minutes, sometimes a few hours).

It doesn't throw any error, SolidCable polls one last time then stop (enqueuing/trimming continue running normally).

My app have low activity

  • 100 users/hour
  • 250 active ws connections (it seems some people let the browser tab opened)
  • 5 ws messages/minute
  • most of messages have a few kB, only one kind of message can reach ~40kB
  • works fine with redis

Environment

solid_cable: 3.0.11
rails: 8.0.2
ruby: 3.4.5
postgres: 16.2

How to reproduce

Run the server.rb, then the cli.rb and go get a coffee (It can take some time).
You can confirm the issue is happening when the cli.rb starts only to receive ping messages.
Observation: the number of connections, channels and size of message are extremely inflate to accelerate the process

# server.rb
require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  gem 'rails', '~> 8.0.0'
  gem 'puma'
  gem 'solid_cable'
  gem 'sqlite3'
  gem 'pg'
end

require 'rackup'
require 'rails'
require 'active_job/railtie'
require 'active_record/railtie'
require 'action_cable/engine'
require 'solid_cable'

# ENV['DATABASE_URL'] = 'postgres://postgres:postgres@localhost/test_cable'
ENV['DATABASE_URL'] = 'sqlite3:storage/data'
MESSAGE = File.read(__FILE__) * 100
CABLE_CFG = {
  adapter: 'solid_cable',
  polling_interval: '0.2.seconds',
  message_retention: '1.day'
}

def SolidCable.cable_config
  ActiveSupport::OrderedOptions.new.update(CABLE_CFG)
end

class TestApp < Rails::Application
  config.load_defaults Rails::VERSION::STRING.to_f
  config.eager_load = true
  config.secret_key_base = 'secret_key_base'
  config.action_cable.cable = CABLE_CFG.with_indifferent_access
  config.logger = Logger.new($stdout)
  config.log_level = 'info'
  config.action_cable.disable_request_forgery_protection = true
end

class HelloChannel < ActionCable::Channel::Base
  def subscribed
    stream_from params['target']
    ActionCable.server.broadcast params['target'], MESSAGE
  end
end

Rails.application.initialize!

ActiveRecord::Schema.define do
  create_table 'solid_cable_messages', force: :cascade do |t|
    t.binary 'channel', limit: 1024, null: false
    t.binary 'payload', limit: 536_870_912, null: false
    t.datetime 'created_at', null: false
    t.integer 'channel_hash', limit: 8, null: false
    t.index ['channel'], name: 'index_solid_cable_messages_on_channel'
    t.index ['channel_hash'], name: 'index_solid_cable_messages_on_channel_hash'
    t.index ['created_at'], name: 'index_solid_cable_messages_on_created_at'
  end
end

handler = Rackup::Handler.get(:puma)
handler.run Rails.application, Port: 3000, Threads: '5:5'
# cli.rb
require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  gem 'faye-websocket', '~> 0.12.0'
end

require 'faye/websocket'
require 'eventmachine'
require 'json'

URL = 'ws://127.0.0.1:3000/cable'
CONNECTIONS = 500

def create_ws_conn
  Faye::WebSocket::Client.new(URL, 'http').tap do |ws|
    $con_idx_gen = $con_idx_gen.to_i + 1
    conn_idx = $con_idx_gen

    ws.on :open do |event|
      puts "Connection #{conn_idx} opened"
    end

    ws.on :message do |event|
      puts "Connection #{conn_idx} received: #{event.data.slice(0, 50)}"
    end

    ws.on :close do |event|
      puts "Connection #{conn_idx} closed (#{event.code})"
    end

    ws.on :error do |event|
      puts "Connection #{conn_idx} error (#{event.message})"
    end

    ws.protocol { 'http' }
  end
end

def new_message
  {
    command: 'subscribe',
    identifier: {
      channel: 'HelloChannel',
      target: "target##{rand(1000)}"
    }.to_json
  }.to_json
end

EM.run do
  # initialize with a LOT of connections
  connections = CONNECTIONS.times.map { create_ws_conn }

  Thread.new do
    loop do
      # 1/10 chance of a new user connect
      connections.delete_at(rand(connections.length)).close if rand(10).zero? && connections.any?
      # 1/10 chance of a user disconnect
      connections << create_ws_conn if rand(10).zero?
      # 1/60 chance of a connected user subscribe to the channel
      connections.each { it.send(new_message) if rand(60).zero? }
      sleep 1
    end
  end
end

According on my observations it occur more frequently on this conditions:

  • using pg
  • big messages
  • lot of users
  • lot of channels

I could not understand why, but this thread stops to run

Activity

  1. npezza93 commented on Aug 14, 2025

    @npezza93
    Collaborator

    Thanks @alanoliveira for the report! This should be fixed now in the latest release.

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