diff --git a/changelog.d/19885.bugfix b/changelog.d/19885.bugfix new file mode 100644 index 00000000000..2b3b779933d --- /dev/null +++ b/changelog.d/19885.bugfix @@ -0,0 +1 @@ +Fix `/sync` being blocked by presence state updates when presence is enabled. diff --git a/synapse/handlers/presence.py b/synapse/handlers/presence.py index 4c3adca46e3..8b5d25089c3 100644 --- a/synapse/handlers/presence.py +++ b/synapse/handlers/presence.py @@ -601,15 +601,6 @@ async def user_syncing( if not affect_presence or not self._track_presence: return _NullContextManager() - # Note that this causes last_active_ts to be incremented which is not - # what the spec wants. - await self.set_state( - UserID.from_string(user_id), - device_id, - state={"presence": presence_state}, - is_sync=True, - ) - curr_sync = self._user_device_to_num_current_syncs.get((user_id, device_id), 0) self._user_device_to_num_current_syncs[(user_id, device_id)] = curr_sync + 1 @@ -617,6 +608,16 @@ async def user_syncing( if self._user_device_to_num_current_syncs[(user_id, device_id)] == 1: self.mark_as_coming_online(user_id, device_id) + # Note that this causes last_active_ts to be incremented which is not + # what the spec wants. Run in background to avoid blocking sync start. + run_in_background( + self.set_state, + UserID.from_string(user_id), + device_id, + {"presence": presence_state}, + is_sync=True, + ) + def _end() -> None: # We check that the user_id is in user_to_num_current_syncs because # user_to_num_current_syncs may have been cleared if we are @@ -1178,11 +1179,12 @@ async def user_syncing( self._user_device_to_num_current_syncs[(user_id, device_id)] = curr_sync + 1 # Note that this causes last_active_ts to be incremented which is not - # what the spec wants. - await self.set_state( + # what the spec wants. Run in background to avoid blocking sync start. + run_in_background( + self.set_state, UserID.from_string(user_id), device_id, - state={"presence": presence_state}, + {"presence": presence_state}, is_sync=True, )