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

Commit

Permalink
Limit the number of devices we delete at once (#14649)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Dec 9, 2022
1 parent c2de2ca commit 94bc21e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/14649.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prune user's old devices on login if they have too many.
4 changes: 3 additions & 1 deletion synapse/handlers/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,12 @@ async def check_device_registered(

async def _prune_too_many_devices(self, user_id: str) -> None:
"""Delete any excess old devices this user may have."""
device_ids = await self.store.check_too_many_devices_for_user(user_id)
device_ids = await self.store.check_too_many_devices_for_user(user_id, 100)
if not device_ids:
return

logger.info("Pruning %d old devices for user %s", len(device_ids), user_id)

# We don't want to block and try and delete tonnes of devices at once,
# so we cap the number of devices we delete synchronously.
first_batch, remaining_device_ids = device_ids[:10], device_ids[10:]
Expand Down
11 changes: 8 additions & 3 deletions synapse/storage/databases/main/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,11 +1569,15 @@ def _txn(txn: LoggingTransaction) -> int:

return rows

async def check_too_many_devices_for_user(self, user_id: str) -> List[str]:
async def check_too_many_devices_for_user(
self, user_id: str, limit: int
) -> List[str]:
"""Check if the user has a lot of devices, and if so return the set of
devices we can prune.
This does *not* return hidden devices or devices with E2E keys.
Returns at most `limit` number of devices, ordered by last seen.
"""

num_devices = await self.db_pool.simple_select_one_onecol(
Expand Down Expand Up @@ -1614,20 +1618,21 @@ async def check_too_many_devices_for_user(self, user_id: str) -> List[str]:

# Now fetch the devices to delete.
sql = """
SELECT DISTINCT device_id FROM devices
SELECT device_id FROM devices
LEFT JOIN e2e_device_keys_json USING (user_id, device_id)
WHERE
user_id = ?
AND NOT hidden
AND last_seen < ?
AND key_json IS NULL
ORDER BY last_seen
LIMIT ?
"""

def check_too_many_devices_for_user_txn(
txn: LoggingTransaction,
) -> List[str]:
txn.execute(sql, (user_id, max_last_seen))
txn.execute(sql, (user_id, max_last_seen, limit))
return [device_id for device_id, in txn]

return await self.db_pool.runInteraction(
Expand Down
31 changes: 31 additions & 0 deletions tests/handlers/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from synapse.api.errors import NotFoundError, SynapseError
from synapse.handlers.device import MAX_DEVICE_DISPLAY_NAME_LEN, DeviceHandler
from synapse.rest import admin
from synapse.rest.client import account, login
from synapse.server import HomeServer
from synapse.util import Clock

Expand All @@ -30,6 +32,12 @@


class DeviceTestCase(unittest.HomeserverTestCase):
servlets = [
login.register_servlets,
admin.register_servlets,
account.register_servlets,
]

def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
hs = self.setup_test_homeserver("server", federation_http_client=None)
handler = hs.get_device_handler()
Expand Down Expand Up @@ -229,6 +237,29 @@ def test_update_unknown_device(self) -> None:
NotFoundError,
)

def test_login_delete_old_devices(self) -> None:
"""Delete old devices if the user already has too many."""

user_id = self.register_user("user", "pass")

# Create a bunch of devices
for _ in range(50):
self.login("user", "pass")
self.reactor.advance(1)

# Advance the clock for ages (as we only delete old devices)
self.reactor.advance(60 * 60 * 24 * 300)

# Log in again to start the pruning
self.login("user", "pass")

# Give the background job time to do its thing
self.reactor.pump([1.0] * 100)

# We should now only have the most recent device.
devices = self.get_success(self.handler.get_devices_by_user(user_id))
self.assertEqual(len(devices), 1)

def _record_users(self) -> None:
# check this works for both devices which have a recorded client_ip,
# and those which don't.
Expand Down

0 comments on commit 94bc21e

Please sign in to comment.