This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix race calling /members?at=
#14817
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix race where calling `/members` or `/state` with an `at` parameter could fail for newly created rooms, when using multiple workers. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -801,13 +801,66 @@ async def get_last_event_in_room_before_stream_ordering( | |
before this stream ordering. | ||
""" | ||
|
||
last_row = await self.get_room_event_before_stream_ordering( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
room_id=room_id, | ||
stream_ordering=end_token.stream, | ||
def get_last_event_in_room_before_stream_ordering_txn( | ||
txn: LoggingTransaction, | ||
) -> Optional[str]: | ||
# We need to handle the fact that the stream tokens can be vector | ||
# clocks. We do this by getting all rows between the minimum and | ||
# maximum stream ordering in the token, plus one row less than the | ||
# minimum stream ordering. We then filter the results against the | ||
# token and return the first row that matches. | ||
|
||
sql = """ | ||
SELECT * FROM ( | ||
SELECT instance_name, stream_ordering, topological_ordering, event_id | ||
FROM events | ||
LEFT JOIN rejections USING (event_id) | ||
WHERE room_id = ? | ||
AND ? < stream_ordering AND stream_ordering <= ? | ||
AND NOT outlier | ||
AND rejections.event_id IS NULL | ||
ORDER BY stream_ordering DESC | ||
) AS a | ||
UNION | ||
SELECT * FROM ( | ||
SELECT instance_name, stream_ordering, topological_ordering, event_id | ||
FROM events | ||
LEFT JOIN rejections USING (event_id) | ||
WHERE room_id = ? | ||
AND stream_ordering <= ? | ||
AND NOT outlier | ||
AND rejections.event_id IS NULL | ||
ORDER BY stream_ordering DESC | ||
LIMIT 1 | ||
) AS b | ||
DMRobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
txn.execute( | ||
sql, | ||
( | ||
room_id, | ||
end_token.stream, | ||
end_token.get_max_stream_pos(), | ||
room_id, | ||
end_token.stream, | ||
), | ||
) | ||
|
||
for instance_name, stream_ordering, topological_ordering, event_id in txn: | ||
if _filter_results( | ||
lower_token=None, | ||
upper_token=end_token, | ||
instance_name=instance_name, | ||
topological_ordering=topological_ordering, | ||
stream_ordering=stream_ordering, | ||
): | ||
return event_id | ||
|
||
return None | ||
|
||
return await self.db_pool.runInteraction( | ||
"get_last_event_in_room_before_stream_ordering", | ||
get_last_event_in_room_before_stream_ordering_txn, | ||
) | ||
if last_row: | ||
return last_row[2] | ||
return None | ||
|
||
async def get_current_room_stream_token_for_room_id( | ||
self, room_id: str | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it just newly-created rooms that are vulnerable to this race? E.g. are there other consumers of
get_last_event_in_room_before_stream_ordering
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it can also be a bit weird if it races with any state change, as it may return the state before the state change. And I guess joins would also do something weird here.
Will update.