Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Ensure a long state res does not starve CPU (matrix-org#15960)
Browse files Browse the repository at this point in the history
We do this by yielding the reactor in hot loops.
  • Loading branch information
erikjohnston authored and Fizzadar committed Aug 31, 2023
1 parent 8901e8c commit e5e430f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/15960.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure a long state res does not starve CPU by occasionally yielding to the reactor.
9 changes: 8 additions & 1 deletion synapse/state/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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],
Expand All @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit e5e430f

Please sign in to comment.