Skip to content

Commit 52f456a

Browse files
authored
/sync: Fix edge-case in calculating the "device_lists" response (#16949)
Fixes #16948. If the `join` and the `leave` are in the same sync response, we need to count them as a "left" user.
1 parent 6d5bafb commit 52f456a

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

changelog.d/16949.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix various long-standing bugs which could cause incorrect state to be returned from `/sync` in certain situations.

synapse/handlers/sync.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,7 +2837,7 @@ def calculate_user_changes(self) -> Tuple[AbstractSet[str], AbstractSet[str]]:
28372837
if self.since_token:
28382838
for joined_sync in self.joined:
28392839
it = itertools.chain(
2840-
joined_sync.timeline.events, joined_sync.state.values()
2840+
joined_sync.state.values(), joined_sync.timeline.events
28412841
)
28422842
for event in it:
28432843
if event.type == EventTypes.Member:
@@ -2849,13 +2849,20 @@ def calculate_user_changes(self) -> Tuple[AbstractSet[str], AbstractSet[str]]:
28492849
newly_joined_or_invited_or_knocked_users.add(
28502850
event.state_key
28512851
)
2852+
# If the user left and rejoined in the same batch, they
2853+
# count as a newly-joined user, *not* a newly-left user.
2854+
newly_left_users.discard(event.state_key)
28522855
else:
28532856
prev_content = event.unsigned.get("prev_content", {})
28542857
prev_membership = prev_content.get("membership", None)
28552858
if prev_membership == Membership.JOIN:
28562859
newly_left_users.add(event.state_key)
2860+
# If the user joined and left in the same batch, they
2861+
# count as a newly-left user, not a newly-joined user.
2862+
newly_joined_or_invited_or_knocked_users.discard(
2863+
event.state_key
2864+
)
28572865

2858-
newly_left_users -= newly_joined_or_invited_or_knocked_users
28592866
return newly_joined_or_invited_or_knocked_users, newly_left_users
28602867

28612868

0 commit comments

Comments
 (0)