From e0a1aaa1b93f5302baf90b09548733aa622090d8 Mon Sep 17 00:00:00 2001 From: Paarth Shah Date: Wed, 31 Jul 2024 00:44:59 -0700 Subject: [PATCH] Fix matrix blocking call by running sync_forever in background_task (#122800) Fix blocking call by running sync_forever in background_task --- homeassistant/components/matrix/__init__.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/matrix/__init__.py b/homeassistant/components/matrix/__init__.py index 4c9af45e63f..77f13293519 100644 --- a/homeassistant/components/matrix/__init__.py +++ b/homeassistant/components/matrix/__init__.py @@ -209,15 +209,22 @@ class MatrixBot: await self._resolve_room_aliases(listening_rooms) self._load_commands(commands) await self._join_rooms() + # Sync once so that we don't respond to past events. + _LOGGER.debug("Starting initial sync for %s", self._mx_id) await self._client.sync(timeout=30_000) + _LOGGER.debug("Finished initial sync for %s", self._mx_id) self._client.add_event_callback(self._handle_room_message, RoomMessageText) - await self._client.sync_forever( - timeout=30_000, - loop_sleep_time=1_000, - ) # milliseconds. + _LOGGER.debug("Starting sync_forever for %s", self._mx_id) + self.hass.async_create_background_task( + self._client.sync_forever( + timeout=30_000, + loop_sleep_time=1_000, + ), # milliseconds. + name=f"{self.__class__.__name__}: sync_forever for '{self._mx_id}'", + ) self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, handle_startup)