Skip to content

Commit cdbbf36

Browse files
Don't lock up when joining large rooms (#16903)
Co-authored-by: Andrew Morgan <andrew@amorgan.xyz>
1 parent c51a224 commit cdbbf36

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

changelog.d/16903.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix performance issue when joining very large rooms that can cause the server to lock up. Introduced in v1.100.0.

synapse/handlers/federation_event.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,17 +1757,25 @@ async def prep(event: EventBase) -> None:
17571757

17581758
events_and_contexts_to_persist.append((event, context))
17591759

1760-
for event in sorted_auth_events:
1760+
for i, event in enumerate(sorted_auth_events):
17611761
await prep(event)
17621762

1763-
await self.persist_events_and_notify(
1764-
room_id,
1765-
events_and_contexts_to_persist,
1766-
# Mark these events backfilled as they're historic events that will
1767-
# eventually be backfilled. For example, missing events we fetch
1768-
# during backfill should be marked as backfilled as well.
1769-
backfilled=True,
1770-
)
1763+
# The above function is typically not async, and so won't yield to
1764+
# the reactor. For large rooms let's yield to the reactor
1765+
# occasionally to ensure we don't block other work.
1766+
if (i + 1) % 1000 == 0:
1767+
await self._clock.sleep(0)
1768+
1769+
# Also persist the new event in batches for similar reasons as above.
1770+
for batch in batch_iter(events_and_contexts_to_persist, 1000):
1771+
await self.persist_events_and_notify(
1772+
room_id,
1773+
batch,
1774+
# Mark these events as backfilled as they're historic events that will
1775+
# eventually be backfilled. For example, missing events we fetch
1776+
# during backfill should be marked as backfilled as well.
1777+
backfilled=True,
1778+
)
17711779

17721780
@trace
17731781
async def _check_event_auth(

0 commit comments

Comments
 (0)