Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix to-device being dropped in limited sync in SQLite. (#11966)
Browse files Browse the repository at this point in the history
If ther are more than 100 to-device messages pending for a device
`/sync` will only return the first 100, however the next batch token was
incorrectly calculated and so all other pending messages would be
dropped.

This is due to `txn.rowcount` only returning the number of rows that
*changed*, rather than the number *selected* in SQLite.
  • Loading branch information
erikjohnston committed Feb 11, 2022
1 parent 4ef39f3 commit 79fb64e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/11966.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix to-device messages being dropped during limited sync when using SQLite.
5 changes: 4 additions & 1 deletion synapse/storage/databases/main/deviceinbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ def get_device_messages_txn(txn: LoggingTransaction):
# intended for each device.
last_processed_stream_pos = to_stream_id
recipient_device_to_messages: Dict[Tuple[str, str], List[JsonDict]] = {}
rowcount = 0
for row in txn:
rowcount += 1

last_processed_stream_pos = row[0]
recipient_user_id = row[1]
recipient_device_id = row[2]
Expand All @@ -373,7 +376,7 @@ def get_device_messages_txn(txn: LoggingTransaction):
(recipient_user_id, recipient_device_id), []
).append(message_dict)

if limit is not None and txn.rowcount == limit:
if limit is not None and rowcount == limit:
# We ended up bumping up against the message limit. There may be more messages
# to retrieve. Return what we have, as well as the last stream position that
# was processed.
Expand Down
40 changes: 40 additions & 0 deletions tests/rest/client/test_sendtodevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,43 @@ def test_remote_room_key_request(self):
"content": {"idx": 3},
},
)

def test_limited_sync(self):
"""If a limited sync for to-devices happens the next /sync should respond immediately."""

self.register_user("u1", "pass")
user1_tok = self.login("u1", "pass", "d1")

user2 = self.register_user("u2", "pass")
user2_tok = self.login("u2", "pass", "d2")

# Do an initial sync
channel = self.make_request("GET", "/sync", access_token=user2_tok)
self.assertEqual(channel.code, 200, channel.result)
sync_token = channel.json_body["next_batch"]

# Send 150 to-device messages. We limit to 100 in `/sync`
for i in range(150):
test_msg = {"foo": "bar"}
chan = self.make_request(
"PUT",
f"/_matrix/client/r0/sendToDevice/m.test/1234-{i}",
content={"messages": {user2: {"d2": test_msg}}},
access_token=user1_tok,
)
self.assertEqual(chan.code, 200, chan.result)

channel = self.make_request(
"GET", f"/sync?since={sync_token}&timeout=300000", access_token=user2_tok
)
self.assertEqual(channel.code, 200, channel.result)
messages = channel.json_body.get("to_device", {}).get("events", [])
self.assertEqual(len(messages), 100)
sync_token = channel.json_body["next_batch"]

channel = self.make_request(
"GET", f"/sync?since={sync_token}&timeout=300000", access_token=user2_tok
)
self.assertEqual(channel.code, 200, channel.result)
messages = channel.json_body.get("to_device", {}).get("events", [])
self.assertEqual(len(messages), 50)

0 comments on commit 79fb64e

Please sign in to comment.