-
-
Notifications
You must be signed in to change notification settings - Fork 79
Description
Mautrix uses lazy membership loading, which causes synapse to oftentimes send state events to the client, that the client has already received. Upon reception of a state event, mautrix invalidates all outbound group sessions. This is the correct thing to do, but because synapse frequently sends duplicate state events, mautrix invalidates outbound group sessions all the time.
A fix for this has first been implemented in 72fb0f6. This fix relies on storing the most recent membership event as an attribute of the event and then later comparing this stored event to the received event and ignoring the received event if they are equal:
python/mautrix/client/state_store/abstract.py
Lines 146 to 148 in 3e22e61
evt.unsigned["mautrix_prev_membership"] = await self.get_member( | |
evt.room_id, UserID(evt.state_key) | |
) |
python/mautrix/crypto/machine.py
Lines 193 to 200 in 3e22e61
prev_cache = evt.unsigned.get("mautrix_prev_membership") | |
if isinstance(prev_cache, Member) and prev_cache.membership == cur: | |
self.log.debug( | |
f"Got duplicate membership state event in {evt.room_id} changing {evt.state_key} " | |
f"from {prev} to {cur}, cached state was {prev_cache} (event ID: {evt.event_id}, " | |
f"sync source: {src})" | |
) | |
return |
The problem is, that loading the most recent membership event and comparing the stored membership event happens in different event handlers, which are executed concurrently.
The most recent membership event is loaded in the update_state()
event handler, which is registered here:
python/mautrix/client/client.py
Line 31 in 3e22e61
self.add_event_handler(EventType.ALL, self._update_state) |
Comparing the received membership event against the most recent one happens in handle_member_event()
, which is registered here:
python/mautrix/crypto/machine.py
Line 109 in 3e22e61
self.client.add_event_handler(EventType.ROOM_MEMBER, self.handle_member_event) |
While the
update_state()
handler is executed first, execution is interrupted once the most recent membership event is loaded via await self.get_member()
, as this loads the event from the database, which takes some time. In the meantime, handle_member_event()
becomes active and attempts to compare the received membership event against the most recent one, which hasn't yet been loaded from the database. As a result, the comparison fails and the outbound session is invalidated.
I implemented a hacky fix for this in jkhsjdhjs@e7c1921, as I don't know a good way to fix this properly.