From a4cc08227bafaeb1d7fc0f9ac39d4b28a60f5819 Mon Sep 17 00:00:00 2001
From: Tulir Asokan <tulir@maunium.net>
Date: Fri, 4 Sep 2020 16:24:20 +0300
Subject: [PATCH] Fix sync warning when stopping bridge

---
 mautrix_facebook/__main__.py | 12 +++++++++---
 mautrix_facebook/user.py     |  3 ++-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/mautrix_facebook/__main__.py b/mautrix_facebook/__main__.py
index 2fe2ffc..cb5756a 100644
--- a/mautrix_facebook/__main__.py
+++ b/mautrix_facebook/__main__.py
@@ -62,14 +62,19 @@ class MessengerBridge(Bridge):
         self.public_website = PublicBridgeWebsite(self.config["appservice.public.shared_secret"])
         self.az.app.add_subapp(self.config["appservice.public.prefix"], self.public_website.app)
 
-    def prepare_shutdown(self) -> None:
+    def prepare_stop(self) -> None:
         self.periodic_reconnect_task.cancel()
         self.log.debug("Stopping puppet syncers")
         for puppet in Puppet.by_custom_mxid.values():
             puppet.stop()
-        self.log.debug("Saving user sessions and stopping listeners")
-        for mxid, user in User.by_mxid.items():
+        self.log.debug("Stopping facebook listeners")
+        User.shutdown = True
+        for user in User.by_fbid.values():
             user.stop_listening()
+
+    def prepare_shutdown(self) -> None:
+        self.log.debug("Saving user sessions")
+        for user in User.by_mxid.values():
             user.save()
 
     async def start(self) -> None:
@@ -144,4 +149,5 @@ class MessengerBridge(Bridge):
     def is_bridge_ghost(self, user_id: UserID) -> bool:
         return bool(Puppet.get_id_from_mxid(user_id))
 
+
 MessengerBridge().run()
diff --git a/mautrix_facebook/user.py b/mautrix_facebook/user.py
index 0eaeca5..6441250 100644
--- a/mautrix_facebook/user.py
+++ b/mautrix_facebook/user.py
@@ -46,6 +46,7 @@ config: Config
 
 class User(BaseUser):
     temp_disconnect_notices: bool = True
+    shutdown: bool = False
 
     by_mxid: Dict[UserID, 'User'] = {}
     by_fbid: Dict[str, 'User'] = {}
@@ -526,7 +527,7 @@ class User(BaseUser):
         async for event in self.listener.listen():
             await self._handle_event(event)
         self.is_connected = False
-        if not self._is_refreshing:
+        if not self._is_refreshing and not self.shutdown:
             await self.send_bridge_notice("Facebook Messenger connection closed without error")
 
     async def _handle_event(self, event: Any) -> None:
-- 
GitLab