Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions roborock/devices/traits/v1/device_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ async def refresh(self) -> None:
ever change with a firmware update, so caching is appropriate.
"""
cache_data = await self._device_cache.get()
if cache_data.device_features is not None:
common.merge_trait_values(self, cache_data.device_features)
cached = cache_data.device_features
# Features unpickled from an older version lack newer fields; rediscover rather than merge them as None.
if cached is not None and all(hasattr(cached, field.name) for field in fields(DeviceFeatures)):
common.merge_trait_values(self, cached)
return
# Save cached device features
await super().refresh()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next line, cache_data.device_features = self, stores the trait itself, including _rpc_channel. With a real V1Channel that channel holds a local closure, so FileCache.flush() raises AttributeError: Can't get local object 'V1Channel.rpc_channel.<locals>.rpc_strategies_cb'. store_value has already opened the file with "wb" by then, so it is left at 0 bytes and the next start fails with EOFError.

I reproduced this with create_device_manager, FileCache, FakeRoborockCloud and V1VacuumSimulator. It is pre-existing on main, but this change sends more caches down that path: a pickle that only lacks a field no trait gates on (for example is_curtain_supported) works and flushes fine on main, and with this change the flush destroys it.

Caching a plain copy fixes it:

# Save cached device features
await super().refresh()
# Cache a plain copy: the trait holds the RPC channel, which FileCache cannot pickle.
cache_data.device_features = DeviceFeatures(**{f.name: getattr(self, f.name) for f in fields(DeviceFeatures)})
await self._device_cache.set(cache_data)

Separately (not needed here), store_value could serialize before opening the file, so a serialization error cannot truncate a good cache.

Expand Down
31 changes: 30 additions & 1 deletion tests/test_supported_features.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import pickle
from dataclasses import asdict, fields
from unittest.mock import AsyncMock

Expand All @@ -16,7 +17,7 @@
is_valid_dock,
is_wash_n_fill_dock,
)
from roborock.devices.cache import CacheData, DeviceCache, InMemoryCache
from roborock.devices.cache import CacheData, DeviceCache, DeviceCacheData, InMemoryCache
from roborock.devices.traits.v1.device_features import DeviceFeaturesTrait
from roborock.roborock_typing import RoborockCommand
from tests import mock_data
Expand Down Expand Up @@ -489,6 +490,34 @@ async def test_from_dict_rejects_legacy_missing_capabilities(caplog: pytest.LogC
assert (await device_cache.get()).device_features is trait


async def test_refresh_rejects_pickled_features_missing_capabilities() -> None:
"""Test features unpickled from an older version are rediscovered, not merged with missing values as None."""
device_features = DeviceFeatures.from_feature_flags(
new_feature_info=262144,
new_feature_info_str="",
feature_info=[],
product_nickname=None,
)
# Unpickling skips __init__, so fields added since the cache was written are absent from the instance.
del device_features.is_ai_recognition_obstacle_supported
legacy_features = pickle.loads(pickle.dumps(device_features))

cache = InMemoryCache()
await cache.set(CacheData(device_info={"legacy-device": DeviceCacheData(device_features=legacy_features)}))
device_cache = DeviceCache("legacy-device", cache)
product = HomeDataProduct.from_dict(mock_data.PRODUCTS["home_data_product_a27.json"])
trait = DeviceFeaturesTrait(product, device_cache)
rpc_channel = AsyncMock()
rpc_channel.send_command.return_value = [mock_data.APP_GET_INIT_STATUS]
trait._rpc_channel = rpc_channel

await trait.refresh()

rpc_channel.send_command.assert_awaited_once_with(RoborockCommand.APP_GET_INIT_STATUS)
assert trait.is_ai_recognition_obstacle_supported is True
assert (await device_cache.get()).device_features is trait

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

device_features is trait locks in storing the object that cannot be pickled, which is why the flush problem does not show up here. With the plain copy, something like this would cover it:

cached = (await device_cache.get()).device_features
assert pickle.loads(pickle.dumps(cached)).is_ai_recognition_obstacle_supported

The same is trait assertion in test_from_dict_rejects_legacy_missing_capabilities above would need updating too.



@pytest.mark.parametrize(
("dock_type", "is_collectable", "is_washable"),
[
Expand Down
Loading