Repository navigation
Show messages missed while disconnected, even after sending one - #405
namespaceMarcello wants to merge 3 commits into
Conversation
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>
There was a problem hiding this comment.
🟡 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 rungh pr ready --undo.
Click "Ready for review" or rungh pr readyto 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
| const shownMessageIds = this.#shownMessageIds(streamElement) | ||
|
|
||
| const didScroll = await this.#scrollManager.autoscroll(false, async () => { |
There was a problem hiding this comment.
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.
| if (message?.parentElement === this.messagesTarget) { | ||
| this.#followingMessage(message)?.before(message) |
There was a problem hiding this comment.
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>

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:
reset_remote_connections); Action Cable reconnects on its own schedule, 9 to 16 seconds later in the runs below.mainand 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.jskeeps#lastLoadedAt, the newestupdated_atamong the messages on the page, and on reconnecting asksRooms::RefreshesController#showfor 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 itsPOST(messages/create.turbo_stream.erb), or a live message on the room's stream before theHeartbeatChannelconfirms. 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:#lastLoadedAtmoves only while theHeartbeatChannelis connected (#connected, set in itsconnectedanddisconnectedcallbacks). 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'sappenddrops 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 theScrollManagerqueue. 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:mainthe other member's message never shows.mainthe other member's message never shows; without themessages_controller.jschange the live one ends up above the three.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
#connectedafter starting the refresh instead of before passes, as it should: the refresh reads#lastLoadedAtwhen 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.
mainmain+ Reconnect soon after the server drops the cable connection #358At 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.rband the system tests pass locally (Docker, headless Chrome).This touches
refresh_room_controller.js, which #358 also changes, andmessages_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, incomposer_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.