Skip to content

Listener retry budget (reconnect_attempt) isn't reset by a successful reconnect unless a message is broadcast #83

Closed

Description

@planesrow-82

Environment

  • solid_cable 4.0.1
  • Rails 8.1.3, Ruby 3.3.11
  • PostgreSQL 16 (local reproduction); adapter: solid_cable in config/cable.yml
  • reconnect_attempts: [0, 1, 5, 10]

What we observed

With a non-default reconnect_attempts, the listener recovers from a database outage as expected — but the retry counter is only reset inside the per-message loop of broadcast_messages:

# lib/action_cable/subscription_adapter/solid_cable.rb  (v4.0.1)
::SolidCable::Message.broadcastable(current_channels.keys, last_id).each do |message|
  # ...
  broadcast(message.channel, message.payload) if should_broadcast_message
  self.reconnect_attempt = 0   # <- only runs when there is a message to deliver
  self.last_id = message.id
end

So the reset runs only when at least one message is actually broadcast. A successful reconnect followed by any number of successful-but-empty polls leaves reconnect_attempt at whatever value the previous outage left it. On a low-traffic app, two outages separated by a quiet period share a single retry budget: once the first consumes it, the listener gives up on the first error of the next outage regardless of how generous reconnect_attempts is — and the give-up path produces no log output.

Reproduction

Instrumented Listener#listen / #retry_connecting? with timestamped logging (process-local, no gem changes), then stopped and started Postgres twice with no broadcasts in between:

06:56:55.069  listen RAISED ActiveRecord::ConnectionNotEstablished (Connection refused)   # outage A begins
06:56:55.069  retry_connecting? attempt=1/4 -> RETRY
06:56:56.191  retry_connecting? attempt=2/4 -> RETRY        (after 1s sleep)
06:57:01.307  retry_connecting? attempt=3/4 -> RETRY        (after 5s sleep)
06:57:11.433  retry_connecting? attempt=4/4 -> RETRY        (after 10s sleep)
06:57:11.433  listen ENTER                                   # Postgres is back — RECOVERED
              ... ~18 seconds of healthy polling, no messages broadcast ...
06:57:29.294  listen RAISED ActiveRecord::ConnectionNotEstablished                        # outage B begins
06:57:29.294  retry_connecting? attempt=5/4 -> GIVE UP       # zero retries granted
              listener thread ends; no log output

The listener recovered from outage A on attempt 4 and polled healthily for ~18s, yet outage B was terminal on its first error because the counter was still at 4.

Expected

A successful reconnect — or at minimum a successful poll — restores the retry budget, so each independent outage gets the full reconnect_attempts allowance.

Actual

The budget is only restored when a message is broadcast, so a healthy-but-quiet listener carries a spent counter into the next outage and can terminate immediately.

Why this looks inconsistent

#81 ("reconnect_attempt not reset to 0 after successful retry") reported that the counter isn't reset after a successful reconnect, and #82 added the reset to address it. Because the reset was placed inside the per-message loop, it only runs when a message is delivered — so the original scenario (a successful retry/poll with no pending message) still doesn't reset. We may be misreading the intent — happy to be corrected — but the current placement looks narrower than resetting "after successfully accessing the DB," and the two diverge on a quiet app.

Suggested minimal fix

Reset once the poll query succeeds, rather than per delivered message — e.g. after the broadcastable loop in broadcast_messages:

def broadcast_messages
  current_channels = channels.dup

  ::SolidCable::Message.broadcastable(current_channels.keys, last_id).each do |message|
    # ... existing loop, minus the per-message reset ...
  end

  self.reconnect_attempt = 0   # any completed poll proves the DB is reachable
end

Related, minor

When retry_connecting? gives up, the rescue completes and the thread ends silently — the only visible symptom is that broadcasts stop until the process restarts. The delegate :logger, to: :@adapter on the listener (line 65 in v4.0.1) also appears unused. A logger&.error on the give-up path would make listener death observable to operators. Mentioning it in case it's helpful; happy to open a separate issue for that.

Happy to open a PR for the reset fix (and/or the log line) if that's useful.

Activity

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