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

Fix device list update stream ids going backward #7158

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/7158.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix device list update stream ids going backward.
10 changes: 8 additions & 2 deletions synapse/storage/data_stores/main/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def get_device_updates_by_remote(self, destination, from_stream_id, limit):
# the max stream_id across each set of duplicate entries
#
# maps (user_id, device_id) -> (stream_id, opentracing_context)
# as long as their stream_id does not match that of the last row
#
# opentracing_context contains the opentracing metadata for the request
# that created the poke
Expand Down Expand Up @@ -269,7 +268,14 @@ def _get_device_update_edus_by_remote(self, destination, from_stream_id, query_m
prev_id = yield self._get_last_device_update_for_remote_user(
destination, user_id, from_stream_id
)
for device_id, device in iteritems(user_devices):

# make sure we go through the devices in stream order
device_ids = sorted(
user_devices.keys(), key=lambda i: query_map[(user_id, i)][0],
)

for device_id in device_ids:
device = user_devices[device_id]
stream_id, opentracing_context = query_map[(user_id, device_id)]
result = {
"user_id": user_id,
Expand Down
6 changes: 6 additions & 0 deletions tests/federation/test_federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ def test_upload_signatures(self):
c = edu["content"]
if stream_id is not None:
self.assertEqual(c["prev_id"], [stream_id])
self.assertGreaterEqual(c["stream_id"], stream_id)
stream_id = c["stream_id"]
devices = {edu["content"]["device_id"] for edu in self.edus}
self.assertEqual({"D1", "D2"}, devices)
Expand Down Expand Up @@ -330,6 +331,7 @@ def test_delete_devices(self):
c.items(),
{"user_id": u1, "prev_id": [stream_id], "deleted": True}.items(),
)
self.assertGreaterEqual(c["stream_id"], stream_id)
stream_id = c["stream_id"]
devices = {edu["content"]["device_id"] for edu in self.edus}
self.assertEqual({"D1", "D2", "D3"}, devices)
Expand Down Expand Up @@ -366,6 +368,8 @@ def test_unreachable_server(self):
self.assertEqual(edu["edu_type"], "m.device_list_update")
c = edu["content"]
self.assertEqual(c["prev_id"], [stream_id] if stream_id is not None else [])
if stream_id is not None:
self.assertGreaterEqual(c["stream_id"], stream_id)
stream_id = c["stream_id"]
devices = {edu["content"]["device_id"] for edu in self.edus}
self.assertEqual({"D1", "D2", "D3"}, devices)
Expand All @@ -390,6 +394,8 @@ def check_device_update_edu(
}

self.assertLessEqual(expected.items(), content.items())
if prev_stream_id is not None:
self.assertGreaterEqual(content["stream_id"], prev_stream_id)
return content["stream_id"]

def check_signing_key_update_txn(self, txn: JsonDict,) -> None:
Expand Down