Skip to content

Commit

Permalink
Store supported namespaces last to avoid data inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Syndace committed Jul 14, 2024
1 parent 8218c95 commit 61669d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed
- Attempt to fix a storage data inconsistency where namespace support is stored for a device but activity information is not

## [1.0.3] - 9th of July, 2024

### Changed
Expand Down
45 changes: 27 additions & 18 deletions omemo/session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,18 +438,18 @@ async def create(
f" {loaded_namespaces} vs. {active_namespaces} (now vs. previous run)"
)

# Store the updated list of loaded namespaces
await storage.store(
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces",
list(loaded_namespaces)
)

# Set the device active for all loaded namespaces
await storage.store(
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active",
{ namespace: True for namespace in loaded_namespaces }
)

# Store the updated list of loaded namespaces
await storage.store(
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces",
list(loaded_namespaces)
)

# Take care of the initialization of newly added backends
for backend in self.__backends:
if backend.namespace not in active_namespaces:
Expand Down Expand Up @@ -539,13 +539,13 @@ async def purge_backend(self, namespace: str) -> None:
device = device._replace(active=frozenset(active.items()))

await self.__storage.store(
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces",
list(device.namespaces)
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active",
dict(device.active)
)

await self.__storage.store(
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active",
dict(device.active)
f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces",
list(device.namespaces)
)

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

# Add new device information entries for new devices
for device_id in new_devices:
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", [ namespace ])
await storage.store(f"/devices/{bare_jid}/{device_id}/active", { namespace: True })
await storage.store(f"/devices/{bare_jid}/{device_id}/label", device_list[device_id])
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", [ namespace ])

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

if device_id in device_list:
# Add the namespace if required
if namespace not in namespaces:
namespaces.add(namespace)
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", list(namespaces))

# Update the status if required
if namespace not in active or active[namespace] is False:
active[namespace] = True
Expand All @@ -1081,6 +1076,11 @@ async def update_device_list(
# doesn't support labels".
if device_list[device_id] is not None and device_list[device_id] != label:
await storage.store(f"/devices/{bare_jid}/{device_id}/label", device_list[device_id])

# Add the namespace if required
if namespace not in namespaces:
namespaces.add(namespace)
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", list(namespaces))
else:
# Update the status if required
if namespace in namespaces:
Expand Down Expand Up @@ -1386,7 +1386,7 @@ async def __get_device_information(
bundle_cache: Set[Bundle] = set()

for device_id in device_list:
namespaces = frozenset((await storage.load_list(
namespaces = set((await storage.load_list(
f"/devices/{bare_jid}/{device_id}/namespaces",
str
)).from_just())
Expand Down Expand Up @@ -1451,8 +1451,17 @@ async def __get_device_information(
str
)).maybe(self.__undecided_trust_level_name)

if any(namespace not in active for namespace in namespaces):
logging.getLogger(SessionManager.LOG_TAG).warning(
f"Inconsistent device information loaded from storage: allegedly supported namespaces are"
f" {namespaces}, but activity information is only available for {set(active.keys())}."
f" Removing the namespaces with missing activity information from storage."
)
namespaces = namespaces & set(active.keys())
await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", list(namespaces))

devices.add(DeviceInformation(
namespaces=namespaces,
namespaces=frozenset(namespaces),
active=frozenset(active.items()),
bare_jid=bare_jid,
device_id=device_id,
Expand Down

0 comments on commit 61669d4

Please sign in to comment.