From 726f7c9818fa1158f762e6972575c1ce58fcedb7 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 6 Jun 2024 15:13:21 +0100 Subject: [PATCH 1/3] Always return OTK counts --- synapse/handlers/sync.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index e815e0ea7f6..4e82a9f749a 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -285,7 +285,11 @@ def __bool__(self) -> bool: ) @staticmethod - def empty(next_batch: StreamToken) -> "SyncResult": + def empty( + next_batch: StreamToken, + device_one_time_keys_count: JsonMapping, + device_unused_fallback_key_types: List[str], + ) -> "SyncResult": "Return a new empty result" return SyncResult( next_batch=next_batch, @@ -297,8 +301,8 @@ def empty(next_batch: StreamToken) -> "SyncResult": archived=[], to_device=[], device_lists=DeviceListUpdates(), - device_one_time_keys_count={}, - device_unused_fallback_key_types=[], + device_one_time_keys_count=device_one_time_keys_count, + device_unused_fallback_key_types=device_unused_fallback_key_types, ) @@ -523,7 +527,27 @@ async def _wait_for_sync_for_user( logger.warning( "Timed out waiting for worker to catch up. Returning empty response" ) - return SyncResult.empty(since_token) + device_id = sync_config.device_id + one_time_keys_count: JsonMapping = {} + unused_fallback_key_types: List[str] = [] + if device_id: + user_id = sync_config.user.to_string() + # TODO: We should have a way to let clients differentiate between the states of: + # * no change in OTK count since the provided since token + # * the server has zero OTKs left for this device + # Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298 + one_time_keys_count = await self.store.count_e2e_one_time_keys( + user_id, device_id + ) + unused_fallback_key_types = list( + await self.store.get_e2e_unused_fallback_key_types( + user_id, device_id + ) + ) + + return SyncResult.empty( + since_token, one_time_keys_count, unused_fallback_key_types + ) # If we've spent significant time waiting to catch up, take it off # the timeout. From 817edabd1d1366d05e7bff87568f4b7ac12eb2af Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 6 Jun 2024 15:16:19 +0100 Subject: [PATCH 2/3] Newsfile --- changelog.d/17275.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/17275.bugfix diff --git a/changelog.d/17275.bugfix b/changelog.d/17275.bugfix new file mode 100644 index 00000000000..eb522bb997f --- /dev/null +++ b/changelog.d/17275.bugfix @@ -0,0 +1 @@ +Fix bug where OTKs were not always included in `/sync` response when using workers. From 40310facdb454d6c22a9e1e77c4d43062ccc8eb3 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 6 Jun 2024 16:39:09 +0100 Subject: [PATCH 3/3] don't cache empty responses --- synapse/handlers/sync.py | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 4e82a9f749a..9d37e2a86f3 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -545,6 +545,7 @@ async def _wait_for_sync_for_user( ) ) + cache_context.should_cache = False # Don't cache empty responses return SyncResult.empty( since_token, one_time_keys_count, unused_fallback_key_types )