Skip to content

Commit 61669d4

Browse files
committed
Store supported namespaces last to avoid data inconsistencies
1 parent 8218c95 commit 61669d4

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## [Unreleased]
88

9+
### Fixed
10+
- Attempt to fix a storage data inconsistency where namespace support is stored for a device but activity information is not
11+
912
## [1.0.3] - 9th of July, 2024
1013

1114
### Changed

omemo/session_manager.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -438,18 +438,18 @@ async def create(
438438
f" {loaded_namespaces} vs. {active_namespaces} (now vs. previous run)"
439439
)
440440

441-
# Store the updated list of loaded namespaces
442-
await storage.store(
443-
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces",
444-
list(loaded_namespaces)
445-
)
446-
447441
# Set the device active for all loaded namespaces
448442
await storage.store(
449443
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active",
450444
{ namespace: True for namespace in loaded_namespaces }
451445
)
452446

447+
# Store the updated list of loaded namespaces
448+
await storage.store(
449+
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces",
450+
list(loaded_namespaces)
451+
)
452+
453453
# Take care of the initialization of newly added backends
454454
for backend in self.__backends:
455455
if backend.namespace not in active_namespaces:
@@ -539,13 +539,13 @@ async def purge_backend(self, namespace: str) -> None:
539539
device = device._replace(active=frozenset(active.items()))
540540

541541
await self.__storage.store(
542-
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces",
543-
list(device.namespaces)
542+
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active",
543+
dict(device.active)
544544
)
545545

546546
await self.__storage.store(
547-
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active",
548-
dict(device.active)
547+
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces",
548+
list(device.namespaces)
549549
)
550550

551551
# If the backend is currently loaded, remove it from the list of loaded backends
@@ -1046,9 +1046,9 @@ async def update_device_list(
10461046

10471047
# Add new device information entries for new devices
10481048
for device_id in new_devices:
1049-
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", [ namespace ])
10501049
await storage.store(f"/devices/{bare_jid}/{device_id}/active", { namespace: True })
10511050
await storage.store(f"/devices/{bare_jid}/{device_id}/label", device_list[device_id])
1051+
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", [ namespace ])
10521052

10531053
# Update namespaces, label and status for previously known devices
10541054
for device_id in old_device_list:
@@ -1060,11 +1060,6 @@ async def update_device_list(
10601060
active = (await storage.load_dict(f"/devices/{bare_jid}/{device_id}/active", bool)).from_just()
10611061

10621062
if device_id in device_list:
1063-
# Add the namespace if required
1064-
if namespace not in namespaces:
1065-
namespaces.add(namespace)
1066-
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", list(namespaces))
1067-
10681063
# Update the status if required
10691064
if namespace not in active or active[namespace] is False:
10701065
active[namespace] = True
@@ -1081,6 +1076,11 @@ async def update_device_list(
10811076
# doesn't support labels".
10821077
if device_list[device_id] is not None and device_list[device_id] != label:
10831078
await storage.store(f"/devices/{bare_jid}/{device_id}/label", device_list[device_id])
1079+
1080+
# Add the namespace if required
1081+
if namespace not in namespaces:
1082+
namespaces.add(namespace)
1083+
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", list(namespaces))
10841084
else:
10851085
# Update the status if required
10861086
if namespace in namespaces:
@@ -1386,7 +1386,7 @@ async def __get_device_information(
13861386
bundle_cache: Set[Bundle] = set()
13871387

13881388
for device_id in device_list:
1389-
namespaces = frozenset((await storage.load_list(
1389+
namespaces = set((await storage.load_list(
13901390
f"/devices/{bare_jid}/{device_id}/namespaces",
13911391
str
13921392
)).from_just())
@@ -1451,8 +1451,17 @@ async def __get_device_information(
14511451
str
14521452
)).maybe(self.__undecided_trust_level_name)
14531453

1454+
if any(namespace not in active for namespace in namespaces):
1455+
logging.getLogger(SessionManager.LOG_TAG).warning(
1456+
f"Inconsistent device information loaded from storage: allegedly supported namespaces are"
1457+
f" {namespaces}, but activity information is only available for {set(active.keys())}."
1458+
f" Removing the namespaces with missing activity information from storage."
1459+
)
1460+
namespaces = namespaces & set(active.keys())
1461+
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", list(namespaces))
1462+
14541463
devices.add(DeviceInformation(
1455-
namespaces=namespaces,
1464+
namespaces=frozenset(namespaces),
14561465
active=frozenset(active.items()),
14571466
bare_jid=bare_jid,
14581467
device_id=device_id,

0 commit comments

Comments
 (0)