From fd8d0ddaf71673f44d47d5ac6df037c0faec729f Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Thu, 18 Dec 2025 11:45:34 +0000 Subject: [PATCH] Use clock as a time source --- synapse/handlers/sync.py | 3 +-- synapse/storage/databases/main/sticky_events.py | 10 +++------- tests/federation/test_federation_catch_up.py | 3 --- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 47558798d7..15d336f819 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -20,7 +20,6 @@ # import itertools import logging -import time from typing import ( TYPE_CHECKING, AbstractSet, @@ -619,7 +618,7 @@ class SyncHandler: events are included, and a dict mapping from room_id to a list of sticky event IDs for that room. """ - now = round(time.time() * 1000) + now = self.clock.time_msec() with Measure( self.clock, name="sticky_events_by_room", server_name=self.server_name ): diff --git a/synapse/storage/databases/main/sticky_events.py b/synapse/storage/databases/main/sticky_events.py index e72e58d00d..da7534f3d8 100644 --- a/synapse/storage/databases/main/sticky_events.py +++ b/synapse/storage/databases/main/sticky_events.py @@ -11,7 +11,6 @@ # See the GNU Affero General Public License for more details: # . import logging -import time from typing import ( TYPE_CHECKING, Any, @@ -234,7 +233,7 @@ class StickyEventsWorkerStore(StateGroupWorkerStore, CacheInvalidationWorkerStor def _get_sticky_event_ids_sent_by_self_txn( self, txn: LoggingTransaction, room_id: str, from_stream_pos: int ) -> list[str]: - now_ms = self._now() + now_ms = self.clock.time_msec() txn.execute( """ SELECT sticky_events.event_id, sticky_events.sender, events.stream_ordering FROM sticky_events @@ -288,7 +287,7 @@ class StickyEventsWorkerStore(StateGroupWorkerStore, CacheInvalidationWorkerStor txn: LoggingTransaction, events: list[EventBase], ) -> None: - now_ms = self._now() + now_ms = self.clock.time_msec() # event, expires_at, stream_id sticky_events: list[tuple[EventBase, int, int]] = [] for ev in events: @@ -607,7 +606,7 @@ class StickyEventsWorkerStore(StateGroupWorkerStore, CacheInvalidationWorkerStor await self.db_pool.runInteraction( "_delete_expired_sticky_events", self._delete_expired_sticky_events_txn, - self._now(), + self.clock.time_msec(), ) def _delete_expired_sticky_events_txn( @@ -620,9 +619,6 @@ class StickyEventsWorkerStore(StateGroupWorkerStore, CacheInvalidationWorkerStor (now,), ) - def _now(self) -> int: - return round(time.time() * 1000) - def _run_background_cleanup(self) -> Deferred: return self.hs.run_as_background_process( "delete_expired_sticky_events", diff --git a/tests/federation/test_federation_catch_up.py b/tests/federation/test_federation_catch_up.py index 711f199911..9d24749ea0 100644 --- a/tests/federation/test_federation_catch_up.py +++ b/tests/federation/test_federation_catch_up.py @@ -1,4 +1,3 @@ -import time from typing import Callable, Collection from unittest import mock from unittest.mock import AsyncMock, Mock @@ -457,8 +456,6 @@ class FederationCatchUpTestCases(FederatingHomeserverTestCase): @unittest.override_config({"experimental_features": {"msc4354_enabled": True}}) def test_sends_sticky_events(self) -> None: """Test that we send sticky events in addition to the latest event in the room when catching up.""" - # make the clock used when generating origin_server_ts the same as the clock used to check expiry - self.reactor.advance(time.time()) per_dest_queue, sent_pdus = self.make_fake_destination_queue() # Make a room with a local user, and two servers. One will go offline