From 60fa027c2e8552eb757230bfef136c9b1c63d912 Mon Sep 17 00:00:00 2001 From: miruka Date: Thu, 28 May 2020 14:16:43 -0400 Subject: [PATCH] Lock models when trying to decrypt events Trying to deep-copy can result in pickling errors (#50), write-lock the models instead to avoid "dictionary changed size during iteration" issues. --- src/backend/matrix_client.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 7710db7a..fb267581 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -1176,22 +1176,25 @@ class MatrixClient(nio.AsyncClient): _, room_id, _ = sync_id - for ev in deepcopy(model).values(): - room = self.all_rooms[room_id] + with model.write_lock: + for ev in model.values(): + room = self.all_rooms[room_id] - if isinstance(ev.source, nio.MegolmEvent): - try: - decrypted = self.decrypt_event(ev.source) + if isinstance(ev.source, nio.MegolmEvent): + try: + decrypted = self.decrypt_event(ev.source) - if not decrypted: - raise nio.EncryptionError() + if not decrypted: + raise nio.EncryptionError() - except nio.EncryptionError: - continue + except nio.EncryptionError: + continue - for cb in self.event_callbacks: - if not cb.filter or isinstance(decrypted, cb.filter): - await asyncio.coroutine(cb.func)(room, decrypted) + for callback in self.event_callbacks: + filter_ = callback.filter + if not filter_ or isinstance(decrypted, filter_): + coro = asyncio.coroutine(callback.func) + await coro(room, decrypted) async def clear_events(self, room_id: str) -> None: