Skip to content

Show messages missed while disconnected, even after sending one - #405

Open
namespaceMarcello wants to merge 3 commits into
basecamp:mainfrom
namespaceMarcello:catch-up-from-where-the-connection-dropped
Open

namespaceMarcello wants to merge 3 commits into
basecamp:mainfrom
namespaceMarcello:catch-up-from-where-the-connection-dropped

Conversation

@namespaceMarcello

@namespaceMarcello namespaceMarcello commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a room page loses its connection but still reaches the server, a message sent from it hides the messages other people posted earlier in the cut: the page never shows them, until it's reloaded. On reconnecting, the page asks for what was created since the newest message it has, and the message it just sent is that newest message.

With this PR the page asks from the newest message that arrived while it was connected, so the missed messages show, in order.

Impact of the bug:

  • Anyone whose room page loses its connection while others keep posting, and who sends a message before it's back, which the composer allows for 5 seconds after the drop. The server drops all of a user's connections when they sign out on another device, lose access to a room, or a room of theirs is deleted (reset_remote_connections); Action Cable reconnects on its own schedule, 9 to 16 seconds later in the runs below.
  • Live, with real Chrome against a production image of main and a real sign out on a second session: in 3 rounds out of 3 where the page sent a message during the cut, the message another member had posted just before it never showed on that page. With this PR, 0 out of 3. Reconnect soon after the server drops the cable connection #358, which reconnects sooner, shortens the cut to 1 to 3 seconds but doesn't change this: 3 out of 3 on its own, 0 out of 3 with this PR.

Present since the first commit of this repository, so in every release up to v1.5.6.

Tested with three system tests, two of which fail on main, the same runs live on production images, the full unit suite, rubocop, herb, brakeman and the system tests. Risk: which messages the refresh after a reconnection asks for, and where the messages a stream brings land in the list. Reverting the three commits restores today's behavior.

Why it happens

refresh_room_controller.js keeps #lastLoadedAt, the newest updated_at among the messages on the page, and on reconnecting asks Rooms::RefreshesController#show for the messages created since. Every message that comes into the list moves it, including one that comes in while the connection is down: the page's own message, appended from the answer to its POST (messages/create.turbo_stream.erb), or a live message on the room's stream before the HeartbeatChannel confirms. The refresh then starts after it, and the messages created before it during the cut are never asked for.

The change

  • refresh_room_controller.js: #lastLoadedAt moves only while the HeartbeatChannel is connected (#connected, set in its connected and disconnected callbacks). After a cut, the refresh asks from the newest message that arrived while connected, so it also brings again the messages that came in during the cut, and Turbo's append drops the copies already on the page.
  • messages_controller.js: Turbo moves such a message to the end, together with the missed ones, and a message that arrived live as the connection came back, between the refresh's query and its answer, would stay above them all. After a stream renders, every message it brought is put back in its place by sort value, as only the last one was until now. The ids come from the stream when its render takes its turn in the ScrollManager queue. This happens only while the list is at its end: a list that's catching up pages on from its last message, which has to stay the newest it fetched (Show the latest page when a refresh misses more than one #369 relies on this).

Checks

  • test/system/catching_up_after_a_dropped_connection_test.rb, three tests in headless Chrome. The server drops the page's connection (reset_remote_connections, as on signing out elsewhere) and Chrome's request blocking keeps it from coming back while the page still posts:

    • another member posts, then the page sends a message; once reconnected, both show, in order. On main the other member's message never shows.
    • another member posts, the page sends two messages, and one is put in the list as if it arrived live between the refresh's query and its answer: it stays after all three. On main the other member's message never shows; without the messages_controller.js change the live one ends up above the three.
    • another member posts while the page doesn't: it shows once reconnected, and a message shown before the cut isn't fetched again.
  • Breaking one piece at a time fails one of these: counting messages while disconnected, never stopping counting, never counting, not putting messages back in place, putting back only the first, or doing it only when the list isn't at its end. Setting #connected after starting the refresh instead of before passes, as it should: the refresh reads #lastLoadedAt when it starts. Putting messages back whatever the list's state passes these tests, and on a merge with Show the latest page when a refresh misses more than one #369 fails its test of a refresh that misses more than a page: paging then starts after the live message and skips the ones before it.

  • Live, a production image per row, three headless Chromes: an admin's room page, a second session of the admin that signs out (which drops the first page's connection), and a member who posts. Six rounds each; the admin's page also sends a message in the cut on every other round.

    Image Rounds where the page sends Member's message missing Rounds where it doesn't Missing Reconnected after
    main 3 3 3 0 10.7 to 15.4 s
    this PR 3 0 3 0 9.4 to 16.1 s
    main + Reconnect soon after the server drops the cable connection #358 3 3 3 0 1.1 to 2.6 s
    this PR + Reconnect soon after the server drops the cable connection #358 3 0 3 0 1.7 to 3.1 s

    At the end the admin's page and the database list the same messages in the same order, except for the missing ones.

  • Full unit suite, rubocop, herb, brakeman, bench/test_http_client.rb and the system tests pass locally (Docker, headless Chrome).

This touches refresh_room_controller.js, which #358 also changes, and messages_controller.js, which #369 and #357 also change. It merges cleanly with each, and the full system suite passes on a merge of all four (one wait for signing in, in composer_test.rb, timed out once under load and passed on three reruns).

Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

namespaceMarcello and others added 2 commits October 24, 2026 13:31
When a room page reconnects, it asks for the messages created since the
newest one it has. A message that came into the page while it was
disconnected moved that point too: one sent from the page, which its
answer appends, or a live one that arrived before the page counted itself
connected again. Messages others posted before it, while the connection
was down, were then never asked for, and the page went on without them
until a reload.

Only messages that come in while connected now move where the next
refresh starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The refresh after a dropped connection now brings again the messages
that came into the page meanwhile, like the ones sent from it. Turbo
moves those to the end together with the missed ones, so a message that
arrived live as the connection came back stayed above them. Put each of
them back in its place, as was already done for the last one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 24, 2026 12:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Mixed refresh batches and queued stream renders can still leave messages out of chronological order.

2 open issues
What changed in this PR

This PR prevents messages sent during a disconnection from advancing the room’s refresh cursor past missed messages.

Changes:

  • Advance the refresh cursor only while the heartbeat connection is active.
  • Reposition already-shown messages replayed by a refresh.
  • Add system tests for missed messages, reconnect ordering, and avoiding unnecessary refetches.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
test/​system/​catching_up_after_a_dropped_connection_test.rb Adds three reconnect regression tests.
app/​javascript/​controllers/​refresh_room_controller.js Gates refresh-cursor updates on connection state.
app/​javascript/​controllers/​messages_controller.js Repositions duplicate messages after stream rendering.

Review effort: Balanced

Comment on lines 68 to 70
const shownMessageIds = this.#shownMessageIds(streamElement)

const didScroll = await this.#scrollManager.autoscroll(false, async () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right: renders wait their turn in ScrollManager's shared queue, so which messages the page already showed was read too early. Addressed in 0514347, which no longer depends on it: every message the stream brings is put in its place, with the ids taken from the stream inside the queued callback, right before render.

Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

Comment on lines +201 to +202
if (message?.parentElement === this.messagesTarget) {
this.#followingMessage(message)?.before(message)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and reproduced: with a missed message, two sent while disconnected, and a live one put in the list before the refresh renders, the page ended with [live, missed, sent, sent]. Addressed in 0514347: after a stream renders, every message it brought is put back in its place by sort value, which merges the missed and repeated ones into chronological order. The pagination boundary holds because this happens only while the list is at its end; on a merge with #369, doing it regardless fails its test of a refresh that misses more than a page, since paging then starts after the live message. The live-arrival test now includes a missed message, and fails on the previous commit and on main.

Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

Putting back only the messages the page already showed left a missed
message, and the ones after it, below a message that arrived live
meanwhile. And which ones the page showed was read before the render
waited its turn in the ScrollManager queue, so an earlier render still
queued could change it.

Place every message the stream brings, from the ids it carries when its
turn comes. Only while the list is at its end: one that's catching up
pages on from its last message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants