0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-11-14 14:01:59 +01:00

Fix failures property in /keys/query (#17499)

Fixes: https://github.com/element-hq/synapse/issues/17498
Fixes: https://github.com/element-hq/element-web/issues/27867
This commit is contained in:
Richard van der Hoff 2024-07-30 09:51:24 +01:00 committed by GitHub
parent 34306be5aa
commit 808dab0699
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 75 additions and 11 deletions

1
changelog.d/17499.bugfix Normal file
View file

@ -0,0 +1 @@
Fix a bug introduced in v1.110.0 which caused `/keys/query` to return incomplete results, leading to high network activity and CPU usage on Matrix clients.

View file

@ -291,14 +291,21 @@ class E2eKeysHandler:
# Only try and fetch keys for destinations that are not marked as # Only try and fetch keys for destinations that are not marked as
# down. # down.
filtered_destinations = await filter_destinations_by_retry_limiter( unfiltered_destinations = remote_queries_not_in_cache.keys()
remote_queries_not_in_cache.keys(), filtered_destinations = set(
await filter_destinations_by_retry_limiter(
unfiltered_destinations,
self.clock, self.clock,
self.store, self.store,
# Let's give an arbitrary grace period for those hosts that are # Let's give an arbitrary grace period for those hosts that are
# only recently down # only recently down
retry_due_within_ms=60 * 1000, retry_due_within_ms=60 * 1000,
) )
)
failures.update(
(dest, _NOT_READY_FOR_RETRY_FAILURE)
for dest in (unfiltered_destinations - filtered_destinations)
)
await concurrently_execute( await concurrently_execute(
_query, _query,
@ -1641,6 +1648,9 @@ def _check_device_signature(
raise SynapseError(400, "Invalid signature", Codes.INVALID_SIGNATURE) raise SynapseError(400, "Invalid signature", Codes.INVALID_SIGNATURE)
_NOT_READY_FOR_RETRY_FAILURE = {"status": 503, "message": "Not ready for retry"}
def _exception_to_failure(e: Exception) -> JsonDict: def _exception_to_failure(e: Exception) -> JsonDict:
if isinstance(e, SynapseError): if isinstance(e, SynapseError):
return {"status": e.code, "errcode": e.errcode, "message": str(e)} return {"status": e.code, "errcode": e.errcode, "message": str(e)}
@ -1649,7 +1659,7 @@ def _exception_to_failure(e: Exception) -> JsonDict:
return {"status": e.code, "message": str(e)} return {"status": e.code, "message": str(e)}
if isinstance(e, NotRetryingDestination): if isinstance(e, NotRetryingDestination):
return {"status": 503, "message": "Not ready for retry"} return _NOT_READY_FOR_RETRY_FAILURE
# include ConnectionRefused and other errors # include ConnectionRefused and other errors
# #

View file

@ -43,9 +43,7 @@ from tests.unittest import override_config
class E2eKeysHandlerTestCase(unittest.HomeserverTestCase): class E2eKeysHandlerTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
self.appservice_api = mock.AsyncMock() self.appservice_api = mock.AsyncMock()
return self.setup_test_homeserver( return self.setup_test_homeserver(application_service_api=self.appservice_api)
federation_client=mock.Mock(), application_service_api=self.appservice_api
)
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None: def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.handler = hs.get_e2e_keys_handler() self.handler = hs.get_e2e_keys_handler()
@ -1224,6 +1222,61 @@ class E2eKeysHandlerTestCase(unittest.HomeserverTestCase):
}, },
) )
def test_query_devices_remote_down(self) -> None:
"""Tests that querying keys for a remote user on an unreachable server returns
results in the "failures" property
"""
remote_user_id = "@test:other"
local_user_id = "@test:test"
# The backoff code treats time zero as special
self.reactor.advance(5)
self.hs.get_federation_http_client().agent.request = mock.AsyncMock( # type: ignore[method-assign]
side_effect=Exception("boop")
)
e2e_handler = self.hs.get_e2e_keys_handler()
query_result = self.get_success(
e2e_handler.query_devices(
{
"device_keys": {remote_user_id: []},
},
timeout=10,
from_user_id=local_user_id,
from_device_id="some_device_id",
)
)
self.assertEqual(
query_result["failures"],
{
"other": {
"message": "Failed to send request: Exception: boop",
"status": 503,
}
},
)
# Do it again: we should hit the backoff
query_result = self.get_success(
e2e_handler.query_devices(
{
"device_keys": {remote_user_id: []},
},
timeout=10,
from_user_id=local_user_id,
from_device_id="some_device_id",
)
)
self.assertEqual(
query_result["failures"],
{"other": {"message": "Not ready for retry", "status": 503}},
)
@parameterized.expand( @parameterized.expand(
[ [
# The remote homeserver's response indicates that this user has 0/1/2 devices. # The remote homeserver's response indicates that this user has 0/1/2 devices.