Repository navigation
fix(v1): rediscover device features when the pickled cache is stale #1100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
cached = (await device_cache.get()).device_features
assert pickle.loads(pickle.dumps(cached)).is_ai_recognition_obstacle_supportedThe same |
||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("dock_type", "is_collectable", "is_washable"), | ||
| [ | ||
|
|
||
There was a problem hiding this comment.
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 realV1Channelthat channel holds a local closure, soFileCache.flush()raisesAttributeError: Can't get local object 'V1Channel.rpc_channel.<locals>.rpc_strategies_cb'.store_valuehas already opened the file with"wb"by then, so it is left at 0 bytes and the next start fails withEOFError.I reproduced this with
create_device_manager,FileCache,FakeRoborockCloudandV1VacuumSimulator. It is pre-existing onmain, but this change sends more caches down that path: a pickle that only lacks a field no trait gates on (for exampleis_curtain_supported) works and flushes fine onmain, and with this change the flush destroys it.Caching a plain copy fixes it:
Separately (not needed here),
store_valuecould serialize before opening the file, so a serialization error cannot truncate a good cache.