Skip to content

Commit 0d44f64

Browse files
committed
Merge remote-tracking branch 'origin/develop' into release-v1.103
2 parents 9d7880c + 1f88790 commit 0d44f64

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

changelog.d/16968.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent locking up when checking auth rules that are independent of room state for batched auth events. Contributed by @ggogel.

synapse/event_auth.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,20 @@
2323
import collections.abc
2424
import logging
2525
import typing
26-
from typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple, Union
26+
from typing import (
27+
Any,
28+
ChainMap,
29+
Dict,
30+
Iterable,
31+
List,
32+
Mapping,
33+
MutableMapping,
34+
Optional,
35+
Set,
36+
Tuple,
37+
Union,
38+
cast,
39+
)
2740

2841
from canonicaljson import encode_canonical_json
2942
from signedjson.key import decode_verify_key_bytes
@@ -175,23 +188,35 @@ async def check_state_independent_auth_rules(
175188
return
176189

177190
# 2. Reject if event has auth_events that: ...
191+
auth_events: ChainMap[str, EventBase] = ChainMap()
178192
if batched_auth_events:
179-
# Copy the batched auth events to avoid mutating them.
180-
auth_events = dict(batched_auth_events)
181-
needed_auth_event_ids = set(event.auth_event_ids()) - batched_auth_events.keys()
193+
# batched_auth_events can become very large. To avoid repeatedly copying it, which
194+
# would significantly impact performance, we use a ChainMap.
195+
# batched_auth_events must be cast to MutableMapping because .new_child() requires
196+
# this type. This casting is safe as the mapping is never mutated.
197+
auth_events = auth_events.new_child(
198+
cast(MutableMapping[str, "EventBase"], batched_auth_events)
199+
)
200+
needed_auth_event_ids = [
201+
event_id
202+
for event_id in event.auth_event_ids()
203+
if event_id not in batched_auth_events
204+
]
182205
if needed_auth_event_ids:
183-
auth_events.update(
206+
auth_events = auth_events.new_child(
184207
await store.get_events(
185208
needed_auth_event_ids,
186209
redact_behaviour=EventRedactBehaviour.as_is,
187210
allow_rejected=True,
188211
)
189212
)
190213
else:
191-
auth_events = await store.get_events(
192-
event.auth_event_ids(),
193-
redact_behaviour=EventRedactBehaviour.as_is,
194-
allow_rejected=True,
214+
auth_events = auth_events.new_child(
215+
await store.get_events(
216+
event.auth_event_ids(),
217+
redact_behaviour=EventRedactBehaviour.as_is,
218+
allow_rejected=True,
219+
)
195220
)
196221

197222
room_id = event.room_id

0 commit comments

Comments
 (0)