From 5f6fff3b42d6527a6748b881acd5af4e4f6dd92a Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 3 Sep 2025 15:05:40 +0100 Subject: [PATCH] Persist in stitched order --- synapse/handlers/federation_event.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/synapse/handlers/federation_event.py b/synapse/handlers/federation_event.py index a90abccf5c..10736a8700 100644 --- a/synapse/handlers/federation_event.py +++ b/synapse/handlers/federation_event.py @@ -900,11 +900,20 @@ class FederationEventHandler: room_id = new_events[0].room_id await assign_stitched_orders(room_id, new_events, self._store) - # We want to sort these by depth so we process them and tell clients about - # them in order. It's also more efficient to backfill this way (`depth` - # ascending) because one backfill event is likely to be the `prev_event` of - # the next event we're going to process. - sorted_events = sorted(new_events, key=lambda x: x.depth) + # We want to sort these by stitched ordering, so that events that will + # be sent on to clients over /sync will receive stream_orderings that + # are consistent with stitched orderings (i.e. we will serve them to clients + # in the same order as stitched_order). + # + # It's also more efficient to backfill this way, because one backfill event + # is likely to be the `prev_event` of the next event we're going to process. + # + # Outliers will not yet have received a stitched ordering, but it doesn't + # really matter what order they get persisted in, because they don't get + # sent to clients and we don't do so much state resolution for them. We just + # persist them before any other events. + + sorted_events = sorted(new_events, key=lambda x: (x.stitched_ordering or 0)) for ev in sorted_events: with nested_logging_context(ev.event_id): await self._process_pulled_event(origin, ev, backfilled=backfilled)