Repository navigation
Conversation
`_user_device_resync_returning_failed` returns early when another resync
filled the cache while this one waited in the linearizer. It returned the
raw cache mapping (device ID -> device), but `_query_devices_for_destination`
reads `result["devices"]` and the cross-signing keys. Two concurrent key
queries for a remote user whose devices weren't cached yet made the second
one raise `KeyError('devices')`, which ended up as a 503 failure for the
whole destination. Clients then back off that server and encrypt without
the user's devices.
The early return now builds the same shape as the federation response,
including the cached cross-signing keys.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #304 +/- ##
=======================================
Coverage 81.29% 81.29%
=======================================
Files 511 511
Lines 74391 74396 +5
Branches 11315 11317 +2
=======================================
+ Hits 60473 60479 +6
+ Misses 10595 10594 -1
Partials 3323 3323
... and 1 file with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixed a bug where a remote key query could fail for a whole server when another query had just fetched the same user's devices.
What goes wrong
_user_device_resync_returning_failedreturns early when the user's device cache became valid while it waited in the resync linearizer. That early return passes on the raw cache mapping ({device_id: device}). The caller,E2eKeysHandler._query_devices_for_destination, expects the shape of a federationuser_devicesresponse and readsresult["devices"]:So when two clients (or two devices of one user) query the keys of a remote user whose devices aren't cached yet at the same moment, the second one gets a 503 failure for the whole destination. Clients then back off that server and encrypt without the user's devices, so the user can't read the message.
The bug is also present in element-hq/synapse
develop.Fix
if await self.store.get_users_whose_devices_are_cached([user_id]): cached = await self.store.get_cached_devices_for_user(user_id) - return cached, False + result = {"user_id": user_id, "devices": list(cached.values())} + + master_key / self_signing_key from the cross-signing key store, if present + return result, FalseThe cache stores the device objects exactly as the federation response carried them, so
list(cached.values())is the samedeviceslist. The cross-signing keys are added too; without them the second query would answer without the user's master key.The only caller that reads the result is
_query_devices_for_destination. The other callers (federation_event,devicemessage, the background retry) only check it forNone.Tests
tests/handlers/test_e2e_keys.py::E2eKeysHandlerTestCase::test_query_devices_remote_concurrent_resync: two concurrentquery_devicescalls for the same uncached remote user, with the federation response held back until both are waiting.{'other': {'status': 503, 'message': "'devices'"}} != {}tests.handlers.test_e2e_keys,tests.handlers.test_device,tests.storage.test_devices: 56 tests passed.ruff check,ruff formatandmypyare clean on the changed files.Merge danger
Door: two-way. No schema or stored-data change; a revert restores the old return value.
Blast radius: small. Only the early-return path of the resync changes, and its only reader expected exactly this shape.
🤖 Generated with Claude Code