From ec3c5fceb83cd28bfa8d5296ce2f11428f8368ad Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Sat, 10 Oct 2026 16:02:24 +0200 Subject: [PATCH 1/2] Reconnect soon after the server drops the cable connection Action Cable's monitor leaves a dropped connection alone for at least six seconds and then waits for its poll timer, so that the clients of a server that went away don't all come back at once. Half of the people in a room are back after ten seconds and the last after sixteen, and from the fifth second the room is offline and the composer disabled. Often the server is there all along: it was replaced behind a proxy, as ONCE does on an update, or it closed the connection itself, as it does to the members of a room that is deleted and to someone removed from a room. The room page now tries once, one to two seconds after the connection drops, at a moment of its own. If the server said not to reconnect it doesn't, and if the attempt fails Action Cable carries on as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017uAnU1dboBiJTZ23xmGpPe --- .../controllers/refresh_room_controller.js | 24 ++++++++++++++- test/system/reconnecting_test.rb | 29 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/system/reconnecting_test.rb diff --git a/app/javascript/controllers/refresh_room_controller.js b/app/javascript/controllers/refresh_room_controller.js index 3c177b1e6..7db7884d7 100644 --- a/app/javascript/controllers/refresh_room_controller.js +++ b/app/javascript/controllers/refresh_room_controller.js @@ -4,6 +4,8 @@ import { cable } from "@hotwired/turbo-rails" import { pageIsTurboPreview } from "helpers/turbo_helpers" const OFFLINE_AFTER_DISCONNECTED_TIMEOUT = 5_000 +const RECONNECT_AFTER_DISCONNECTED_TIMEOUT = 1_000 +const RECONNECT_AFTER_DISCONNECTED_JITTER = 1_000 const REFRESH_AFTER_HIDDEN_TIMEOUT = 60_000 export default class extends Controller { @@ -12,6 +14,7 @@ export default class extends Controller { #lastLoadedAt #offlineTimer = null + #reconnectTimer = null #hiddenAt = null async connect() { @@ -21,12 +24,13 @@ export default class extends Controller { this.channel = await cable.subscribeTo({ channel: "HeartbeatChannel" }, { connected: this.#channelConnected.bind(this), - disconnected: this.#channelDisconnected.bind(this) + disconnected: this.#channelDropped.bind(this) }) } } disconnect() { + clearTimeout(this.#reconnectTimer) this.channel?.unsubscribe() } @@ -56,6 +60,7 @@ export default class extends Controller { this.#refresh("connection") clearTimeout(this.#offlineTimer) + clearTimeout(this.#reconnectTimer) this.dispatch("online", { target: window }) } @@ -65,6 +70,23 @@ export default class extends Controller { }, OFFLINE_AFTER_DISCONNECTED_TIMEOUT) } + #channelDropped({ willAttemptReconnect } = {}) { + this.#channelDisconnected() + if (willAttemptReconnect) this.#reconnectSoon() + } + + // Action Cable leaves a dropped connection alone for at least six seconds, so that the clients + // of a server that went away don't all come back at once. But the server is often still there: + // it was replaced behind a proxy, or it closed the connection itself, as it does to the members + // of a room that's deleted. So try once, sooner, each client at a moment of its own. If that + // fails, Action Cable carries on as it always has. + #reconnectSoon() { + clearTimeout(this.#reconnectTimer) + this.#reconnectTimer = setTimeout(() => { + this.channel.consumer.connect() + }, RECONNECT_AFTER_DISCONNECTED_TIMEOUT + Math.random() * RECONNECT_AFTER_DISCONNECTED_JITTER) + } + #refresh(reason) { get(this.urlValue, { query: { since: this.#lastLoadedAt, reason: reason }, responseKind: "turbo-stream" }) } diff --git a/test/system/reconnecting_test.rb b/test/system/reconnecting_test.rb new file mode 100644 index 000000000..0c11286c6 --- /dev/null +++ b/test/system/reconnecting_test.rb @@ -0,0 +1,29 @@ +require "application_system_test_case" + +class ReconnectingTest < ApplicationSystemTestCase + setup do + sign_in "jz@37signals.com" + join_room rooms(:designers) + end + + test "a connection the server drops is back before the room goes offline" do + users(:jz).reset_remote_connections + assert_cable_disconnected + + # The room goes offline, and the composer with it, five seconds after the connection is lost. + assert_selector "turbo-cable-stream-source[connected]", count: 3, visible: false, wait: 4 + end + + test "a connection the server closes for good stays closed" do + ActionCable.server.remote_connections.where(current_user: users(:jz)).disconnect reconnect: false + assert_cable_disconnected + + sleep 2.5 # Past the moment a dropped connection is tried again. + assert_cable_disconnected + end + + private + def assert_cable_disconnected + assert_no_selector "turbo-cable-stream-source[connected]", visible: false + end +end From b59f1705a2f9d53a45078f9a0f159944105720da Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Sat, 10 Oct 2026 16:30:42 +0200 Subject: [PATCH 2/2] Clear the room's offline timer before setting it again A connection can now drop twice within five seconds: the early attempt opens a socket, and that socket closes before the subscriptions are back. Each drop set the offline timer over the last one, and getting the connection back cleared only the newest. The older timer then took the room offline, and disabled the composer, while it was connected. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017uAnU1dboBiJTZ23xmGpPe --- .../controllers/refresh_room_controller.js | 1 + test/system/reconnecting_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/app/javascript/controllers/refresh_room_controller.js b/app/javascript/controllers/refresh_room_controller.js index 7db7884d7..955c7ca61 100644 --- a/app/javascript/controllers/refresh_room_controller.js +++ b/app/javascript/controllers/refresh_room_controller.js @@ -65,6 +65,7 @@ export default class extends Controller { } #channelDisconnected() { + clearTimeout(this.#offlineTimer) this.#offlineTimer = setTimeout(() => { this.dispatch("offline", { target: window }) }, OFFLINE_AFTER_DISCONNECTED_TIMEOUT) diff --git a/test/system/reconnecting_test.rb b/test/system/reconnecting_test.rb index 0c11286c6..737bdc109 100644 --- a/test/system/reconnecting_test.rb +++ b/test/system/reconnecting_test.rb @@ -22,6 +22,20 @@ class ReconnectingTest < ApplicationSystemTestCase assert_cable_disconnected end + test "a room that loses its connection twice before getting it back isn't left offline" do + page.execute_script(<<~JS) + const element = document.querySelector("[data-controller~='refresh-room']") + const { channel } = Stimulus.getControllerForElementAndIdentifier(element, "refresh-room") + + channel.disconnected({ willAttemptReconnect: false }) + channel.disconnected({ willAttemptReconnect: false }) + channel.connected() + JS + + sleep 5.5 # Past the moment a lost connection takes the room offline. + assert_no_selector "fieldset[data-composer-target='fields'][disabled]" + end + private def assert_cable_disconnected assert_no_selector "turbo-cable-stream-source[connected]", visible: false