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

Commit ce72a2d

Browse files
committed
Make _get_state_group_for_events raise if state is unknown
It seems like calling `_get_state_group_for_events` for an event where the state is unknown is an error. Accordingly, let's raise an exception rather than silently returning an empty result.
1 parent be92775 commit ce72a2d

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

synapse/storage/databases/main/state.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ async def _get_state_group_for_event(self, event_id: str) -> Optional[int]:
307307
num_args=1,
308308
)
309309
async def _get_state_group_for_events(self, event_ids: Collection[str]) -> JsonDict:
310-
"""Returns mapping event_id -> state_group"""
310+
"""Returns mapping event_id -> state_group.
311+
312+
Raises:
313+
RuntimeError if the state is unknown at any of the given events
314+
"""
311315
rows = await self.db_pool.simple_select_many_batch(
312316
table="event_to_state_groups",
313317
column="event_id",
@@ -317,7 +321,11 @@ async def _get_state_group_for_events(self, event_ids: Collection[str]) -> JsonD
317321
desc="_get_state_group_for_events",
318322
)
319323

320-
return {row["event_id"]: row["state_group"] for row in rows}
324+
res = {row["event_id"]: row["state_group"] for row in rows}
325+
for e in event_ids:
326+
if e not in res:
327+
raise RuntimeError("No state group for unknown or outlier event %s" % e)
328+
return res
321329

322330
async def get_referenced_state_groups(
323331
self, state_groups: Iterable[int]

synapse/storage/state.py

+20
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ async def get_state_groups_ids(
571571
572572
Returns:
573573
dict of state_group_id -> (dict of (type, state_key) -> event id)
574+
575+
Raises:
576+
RuntimeError if we don't have a state group for one or more of the events
577+
(ie they are outliers or unknown)
574578
"""
575579
if not event_ids:
576580
return {}
@@ -659,6 +663,10 @@ async def get_state_for_events(
659663
660664
Returns:
661665
A dict of (event_id) -> (type, state_key) -> [state_events]
666+
667+
Raises:
668+
RuntimeError if we don't have a state group for one or more of the events
669+
(ie they are outliers or unknown)
662670
"""
663671
event_to_groups = await self.stores.main._get_state_group_for_events(event_ids)
664672

@@ -696,6 +704,10 @@ async def get_state_ids_for_events(
696704
697705
Returns:
698706
A dict from event_id -> (type, state_key) -> event_id
707+
708+
Raises:
709+
RuntimeError if we don't have a state group for one or more of the events
710+
(ie they are outliers or unknown)
699711
"""
700712
event_to_groups = await self.stores.main._get_state_group_for_events(event_ids)
701713

@@ -723,6 +735,10 @@ async def get_state_for_event(
723735
724736
Returns:
725737
A dict from (type, state_key) -> state_event
738+
739+
Raises:
740+
RuntimeError if we don't have a state group for the event (ie it is an
741+
outlier or is unknown)
726742
"""
727743
state_map = await self.get_state_for_events(
728744
[event_id], state_filter or StateFilter.all()
@@ -741,6 +757,10 @@ async def get_state_ids_for_event(
741757
742758
Returns:
743759
A dict from (type, state_key) -> state_event_id
760+
761+
Raises:
762+
RuntimeError if we don't have a state group for the event (ie it is an
763+
outlier or is unknown)
744764
"""
745765
state_map = await self.get_state_ids_for_events(
746766
[event_id], state_filter or StateFilter.all()

0 commit comments

Comments
 (0)