Skip to content

Guard against homeservers stuck in a /messages pagination loop - #903

Open
gamesguru wants to merge 2 commits into
matrix-org:mainfrom
gamesguru:guru/fix/pagination-loop-guard
Open

gamesguru wants to merge 2 commits into
matrix-org:mainfrom
gamesguru:guru/fix/pagination-loop-guard

Conversation

@gamesguru

Copy link
Copy Markdown
Contributor

Fixes a couple 1 hour deadlock scenarios observed in Complement during a federation/timeline refactor of homeserver code.

Enhances checks and emits more helpful failure messages.

Additionally asserts no element is served twice (lessening the chances of future regressions or TOCTOU bugs).


Pull Request Checklist

Signed-off-by: Shane Jaroch <chown_tee@proton.me>
@gamesguru
gamesguru requested review from a team as code owners July 17, 2026 11:45
fromToken = endTokenRes.Str

if seenTokens[fromToken] {
t.Fatalf("homeserver returned a pagination token (%s) we've already seen -- it appears to be stuck in loop or repeating elements", fromToken)

@MadLittleMods MadLittleMods Jul 17, 2026 •

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.

I think it's valid for a homeserver to return the same pagination token if it doesn't have anything to return yet (still backfilling).

(we should get rid of this check)

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.

I agree it's allowed in the spec. However, having run this test against Synapse over 200 times and never seen a failure in this test, I would question what potential value allowing duplicate tokens has? Can you defend that?

Given the overwhelming empirical evidence that no homeserver duplicates tokens under this test, it seems a reasonable enforcement. (That could be totally invalid in other tests, but it strongly appears to hold here.)

My complement run failed for an unrelated flake:

❌ TestRoomSummaryAllowedRoomIDs/restricted_room_includes_allowed_room_ids (0s)
      room_summary_test.go:60: CSAPI.MustDo GET http://127.0.0.1:32784/_matrix/client/v1/room_summary/%21HrEM4o3rDaGUCMUP:hs1 returned non-2xx code: 404 Not Found - body: {"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}

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.

@MadLittleMods Since you haven't replied in over two months, I'm not sure your response to the fact that "Synapse never does it"

I'm not particularly concerned how the check is maintained upstream. Happy to let you guys do it your way 👍

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.

@gamesguru Please re-request review if this is ready to look at again

Comment thread tests/csapi/room_messages_test.go Outdated
Comment on lines +436 to +440
maxIterations := testCase.numberOfMessagesToSend + 1
for iteration := 0; ; iteration++ {
if iteration >= maxIterations {
t.Fatalf("paginated %d times without reaching the start of the room (no `end` token) -- homeserver may be stuck in a pagination loop", iteration)
}

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.

Having maxIterations can make sense 👍

Comment thread tests/csapi/room_messages_test.go Resolved
Comment thread tests/csapi/room_messages_test.go Resolved
Comment thread tests/csapi/room_messages_test.go Resolved
Comment thread tests/csapi/room_messages_test.go Resolved
Co-authored-by: Eric Eastwood <madlittlemods@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 29, 2026 11:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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.

3 participants