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

Commit 504940a

Browse files
David RobertsonH-Shay
authored andcommitted
Revert "POC delete stale non-e2e devices for users (#14038)" (#14582)
1 parent 0cf02b5 commit 504940a

File tree

5 files changed

+5
-83
lines changed

5 files changed

+5
-83
lines changed

changelog.d/14582.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a regression in Synapse 1.73.0rc1 where Synapse's main process would stop responding to HTTP requests when a user with a large number of devices logs in.

synapse/handlers/device.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,6 @@ async def check_device_registered(
421421

422422
self._check_device_name_length(initial_device_display_name)
423423

424-
# Prune the user's device list if they already have a lot of devices.
425-
await self._prune_too_many_devices(user_id)
426-
427424
if device_id is not None:
428425
new_device = await self.store.store_device(
429426
user_id=user_id,
@@ -455,14 +452,6 @@ async def check_device_registered(
455452

456453
raise errors.StoreError(500, "Couldn't generate a device ID.")
457454

458-
async def _prune_too_many_devices(self, user_id: str) -> None:
459-
"""Delete any excess old devices this user may have."""
460-
device_ids = await self.store.check_too_many_devices_for_user(user_id)
461-
if not device_ids:
462-
return
463-
464-
await self.delete_devices(user_id, device_ids)
465-
466455
async def _delete_stale_devices(self) -> None:
467456
"""Background task that deletes devices which haven't been accessed for more than
468457
a configured time period.
@@ -492,7 +481,7 @@ async def delete_all_devices_for_user(
492481
device_ids = [d for d in device_ids if d != except_device_id]
493482
await self.delete_devices(user_id, device_ids)
494483

495-
async def delete_devices(self, user_id: str, device_ids: Collection[str]) -> None:
484+
async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
496485
"""Delete several devices
497486
498487
Args:

synapse/storage/databases/main/devices.py

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,71 +1533,6 @@ def _txn(txn: LoggingTransaction) -> int:
15331533

15341534
return rows
15351535

1536-
async def check_too_many_devices_for_user(self, user_id: str) -> Collection[str]:
1537-
"""Check if the user has a lot of devices, and if so return the set of
1538-
devices we can prune.
1539-
1540-
This does *not* return hidden devices or devices with E2E keys.
1541-
"""
1542-
1543-
num_devices = await self.db_pool.simple_select_one_onecol(
1544-
table="devices",
1545-
keyvalues={"user_id": user_id, "hidden": False},
1546-
retcol="COALESCE(COUNT(*), 0)",
1547-
desc="count_devices",
1548-
)
1549-
1550-
# We let users have up to ten devices without pruning.
1551-
if num_devices <= 10:
1552-
return ()
1553-
1554-
# We prune everything older than N days.
1555-
max_last_seen = self._clock.time_msec() - 14 * 24 * 60 * 60 * 1000
1556-
1557-
if num_devices > 50:
1558-
# If the user has more than 50 devices, then we chose a last seen
1559-
# that ensures we keep at most 50 devices.
1560-
sql = """
1561-
SELECT last_seen FROM devices
1562-
LEFT JOIN e2e_device_keys_json USING (user_id, device_id)
1563-
WHERE
1564-
user_id = ?
1565-
AND NOT hidden
1566-
AND last_seen IS NOT NULL
1567-
AND key_json IS NULL
1568-
ORDER BY last_seen DESC
1569-
LIMIT 1
1570-
OFFSET 50
1571-
"""
1572-
1573-
rows = await self.db_pool.execute(
1574-
"check_too_many_devices_for_user_last_seen", None, sql, (user_id,)
1575-
)
1576-
if rows:
1577-
max_last_seen = max(rows[0][0], max_last_seen)
1578-
1579-
# Now fetch the devices to delete.
1580-
sql = """
1581-
SELECT DISTINCT device_id FROM devices
1582-
LEFT JOIN e2e_device_keys_json USING (user_id, device_id)
1583-
WHERE
1584-
user_id = ?
1585-
AND NOT hidden
1586-
AND last_seen < ?
1587-
AND key_json IS NULL
1588-
"""
1589-
1590-
def check_too_many_devices_for_user_txn(
1591-
txn: LoggingTransaction,
1592-
) -> Collection[str]:
1593-
txn.execute(sql, (user_id, max_last_seen))
1594-
return {device_id for device_id, in txn}
1595-
1596-
return await self.db_pool.runInteraction(
1597-
"check_too_many_devices_for_user",
1598-
check_too_many_devices_for_user_txn,
1599-
)
1600-
16011536

16021537
class DeviceStore(DeviceWorkerStore, DeviceBackgroundUpdateStore):
16031538
# Because we have write access, this will be a StreamIdGenerator
@@ -1656,7 +1591,6 @@ async def store_device(
16561591
values={},
16571592
insertion_values={
16581593
"display_name": initial_device_display_name,
1659-
"last_seen": self._clock.time_msec(),
16601594
"hidden": False,
16611595
},
16621596
desc="store_device",
@@ -1702,7 +1636,7 @@ async def store_device(
17021636
)
17031637
raise StoreError(500, "Problem storing device.")
17041638

1705-
async def delete_devices(self, user_id: str, device_ids: Collection[str]) -> None:
1639+
async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
17061640
"""Deletes several devices.
17071641
17081642
Args:

tests/handlers/test_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_get_devices_by_user(self) -> None:
115115
"device_id": "xyz",
116116
"display_name": "display 0",
117117
"last_seen_ip": None,
118-
"last_seen_ts": 1000000,
118+
"last_seen_ts": None,
119119
},
120120
device_map["xyz"],
121121
)

tests/storage/test_client_ips.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ def test_get_last_client_ip_by_device(self, after_persisting: bool):
169169
)
170170
)
171171

172-
last_seen = self.clock.time_msec()
173-
174172
if after_persisting:
175173
# Trigger the storage loop
176174
self.reactor.advance(10)
@@ -191,7 +189,7 @@ def test_get_last_client_ip_by_device(self, after_persisting: bool):
191189
"device_id": device_id,
192190
"ip": None,
193191
"user_agent": None,
194-
"last_seen": last_seen,
192+
"last_seen": None,
195193
},
196194
],
197195
)

0 commit comments

Comments
 (0)