summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2023-07-19 18:00:33 +0100
committerGitHub <noreply@github.com>2023-07-19 17:00:33 +0000
commit67f9e5293ea6650b2ec284c0b7503f3f3eade94b (patch)
treed5e1683a0e61dd2de656ae4f8fdd6308123d1b90
parentFix bad merge of #15933 (#15958) (diff)
downloadsynapse-67f9e5293ea6650b2ec284c0b7503f3f3eade94b.tar.xz
Ensure a long state res does not starve CPU (#15960)
We do this by yielding the reactor in hot loops.
-rw-r--r--changelog.d/15960.misc1
-rw-r--r--synapse/state/v2.py9
2 files changed, 9 insertions, 1 deletions
diff --git a/changelog.d/15960.misc b/changelog.d/15960.misc
new file mode 100644
index 0000000000..7cac24a3c5
--- /dev/null
+++ b/changelog.d/15960.misc
@@ -0,0 +1 @@
+Ensure a long state res does not starve CPU by occasionally yielding to the reactor.
diff --git a/synapse/state/v2.py b/synapse/state/v2.py
index 1b9d7d8457..44c49274a9 100644
--- a/synapse/state/v2.py
+++ b/synapse/state/v2.py
@@ -667,7 +667,7 @@ async def _mainline_sort(
     order_map = {}
     for idx, ev_id in enumerate(event_ids, start=1):
         depth = await _get_mainline_depth_for_event(
-            event_map[ev_id], mainline_map, event_map, state_res_store
+            clock, event_map[ev_id], mainline_map, event_map, state_res_store
         )
         order_map[ev_id] = (depth, event_map[ev_id].origin_server_ts, ev_id)
 
@@ -682,6 +682,7 @@ async def _mainline_sort(
 
 
 async def _get_mainline_depth_for_event(
+    clock: Clock,
     event: EventBase,
     mainline_map: Dict[str, int],
     event_map: Dict[str, EventBase],
@@ -704,6 +705,7 @@ async def _get_mainline_depth_for_event(
 
     # We do an iterative search, replacing `event with the power level in its
     # auth events (if any)
+    idx = 0
     while tmp_event:
         depth = mainline_map.get(tmp_event.event_id)
         if depth is not None:
@@ -720,6 +722,11 @@ async def _get_mainline_depth_for_event(
                 tmp_event = aev
                 break
 
+        idx += 1
+
+        if idx % _AWAIT_AFTER_ITERATIONS == 0:
+            await clock.sleep(0)
+
     # Didn't find a power level auth event, so we just return 0
     return 0