From f223346eb7f9ffa77285811b92a02f5f2db22408 Mon Sep 17 00:00:00 2001 From: Heathley Date: Mon, 27 Apr 2026 21:13:44 -0700 Subject: [PATCH] fix(matrix): add sync timeout, callback diagnostics, and mention-drop logging - Wrap _sync_loop sync() call with asyncio.wait_for(timeout=45s) to guard against TCP-level hangs that the Matrix long-poll timeout cannot catch - Add logger.debug at the top of _on_room_message so LOG_LEVEL=DEBUG confirms whether callbacks fire at all (diagnoses #5819, #7914, #12614) - Add logger.debug when MATRIX_REQUIRE_MENTION silently drops a message, pointing users to the env var to disable the filter Adapted for current mautrix-python adapter (PR was written against the legacy matrix-nio adapter). Closes #5819 --- gateway/platforms/matrix.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/gateway/platforms/matrix.py b/gateway/platforms/matrix.py index 4f3056d26c4..9afe18ee80b 100644 --- a/gateway/platforms/matrix.py +++ b/gateway/platforms/matrix.py @@ -1106,9 +1106,15 @@ class MatrixAdapter(BasePlatformAdapter): next_batch = await client.sync_store.get_next_batch() while not self._closing: try: - sync_data = await client.sync( - since=next_batch, - timeout=30000, + # Wrap in asyncio.wait_for to guard against TCP-level hangs + # that the Matrix long-poll timeout cannot catch. Long-poll + # is 30s, so 45s gives 15s slack for network drain. + sync_data = await asyncio.wait_for( + client.sync( + since=next_batch, + timeout=30000, + ), + timeout=45.0, ) # nio returns SyncError objects (not exceptions) for auth @@ -1231,6 +1237,15 @@ class MatrixAdapter(BasePlatformAdapter): room_id = str(getattr(event, "room_id", "")) sender = str(getattr(event, "sender", "")) + # Diagnostic: confirm the callback is firing at all when DEBUG is on. + # Helps users troubleshoot silent inbound issues like #5819, #7914, #12614. + logger.debug( + "Matrix: callback fired — event %s from %s in %s", + getattr(event, "event_id", "?"), + sender, + room_id, + ) + # Ignore own messages (case-insensitive; also drops when our own # user_id hasn't been resolved yet — see _is_self_sender docstring # and issue #15763). @@ -1342,6 +1357,12 @@ class MatrixAdapter(BasePlatformAdapter): in_bot_thread = bool(thread_id and thread_id in self._threads) if self._require_mention and not is_free_room and not in_bot_thread: if not is_mentioned: + logger.debug( + "Matrix: ignoring message %s in %s — no @mention " + "(set MATRIX_REQUIRE_MENTION=false to disable)", + event_id, + room_id, + ) return None # DM mention-thread.