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

Commit

Permalink
Fix bug where we kept re-requesting a remote server's key repeatedly. (
Browse files Browse the repository at this point in the history
…#16257)

* Correctly handle multiple rows per server/key

* Newsfile
  • Loading branch information
erikjohnston authored Sep 5, 2023
1 parent 02bc590 commit c9cec2d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.d/16257.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix long-standing bug where we kept re-requesting a remote server's key repeatedly, potentially causing delays in receiving events over federation.
17 changes: 11 additions & 6 deletions synapse/storage/databases/main/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,17 @@ def _get_keys(txn: Cursor, batch: Tuple[Tuple[str, str], ...]) -> None:
"""Processes a batch of keys to fetch, and adds the result to `keys`."""

# batch_iter always returns tuples so it's safe to do len(batch)
sql = """
SELECT server_name, key_id, key_json, ts_valid_until_ms
FROM server_keys_json WHERE 1=0
""" + " OR (server_name=? AND key_id=?)" * len(
batch
)
where_clause = " OR (server_name=? AND key_id=?)" * len(batch)

# `server_keys_json` can have multiple entries per server (one per
# remote server we fetched from, if using perspectives). Order by
# `ts_added_ms` so the most recently fetched one always wins.
sql = f"""
SELECT server_name, key_id, key_json, ts_valid_until_ms
FROM server_keys_json WHERE 1=0
{where_clause}
ORDER BY ts_added_ms
"""

txn.execute(sql, tuple(itertools.chain.from_iterable(batch)))

Expand Down

0 comments on commit c9cec2d

Please sign in to comment.