Skip to content

fix(v1): rediscover device features when the pickled cache is stale - #1100

Open
hCoureau wants to merge 1 commit into
Python-roborock:mainfrom
hCoureau:submit/fix/stale-pickled-device-features
Open

hCoureau wants to merge 1 commit into
Python-roborock:mainfrom
hCoureau:submit/fix/stale-pickled-device-features

Conversation

@hCoureau

@hCoureau hCoureau commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

What

FileCache (the default persistent cache) pickles DeviceFeatures. Unpickling skips __init__ and DeviceFeatures.from_dict, so features cached by an older library version simply lack any field added since. DeviceFeaturesTrait.refresh() merged that object into the trait with getattr(..., None), so every new field became None. As soon as an optional trait gates on a new field, discovery then raises ValueError: Device feature '<name>' on trait '<trait>' is unknown and the device fails to set up after an upgrade.

DeviceFeatures.from_dict already treats missing fields as a stale cache for dict-based caches. This applies the same rule to the cached object in DeviceFeaturesTrait.refresh(): if it is missing any DeviceFeatures field, it's treated as a cache miss, re-read with APP_GET_INIT_STATUS, and written back.

Why now

#1030 (is_obstacle_photo_supported) and #1086 (is_carpet_boost_supported) both gate a trait on a new DeviceFeatures field. Both hit this on upgrade from 7.38.0 with an existing file cache. The automated review of those updates found it. Any future capability field would hit it too, so it's a standalone fix.

Testing

  • tests/test_supported_features.py::test_refresh_rejects_pickled_features_missing_capabilities pickles a DeviceFeatures missing one field, loads it from the device cache, and checks that refresh re-reads init status and caches the complete trait. It fails on main (no RPC is sent and the field merges as None) and passes with the fix.
  • ruff check, ruff format --check, and pytest pass (3295 passed, 45 xfailed). mypy reports only the existing tests/map/test_b01_q10_render.py:502 error, also on main.

🤖 Generated with Claude Code

FileCache pickles DeviceFeatures, and unpickling skips __init__ and
DeviceFeatures.from_dict, so features cached by an older version lack
any field added since. DeviceFeaturesTrait.refresh merged those as None,
which makes discovery raise "Device feature ... is unknown" as soon as a
trait gates on a new field. Treat an incomplete cached object as a cache
miss, matching what from_dict already does for dict-based caches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
roborock/devices/traits/v1/device_features.py 88.70% <100.00%> (+0.18%) ⬆️
tests/test_supported_features.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@infy1995 infy1995 left a comment

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.

Thanks for this, the ValueError is real and the new check fixes it. One thing should be fixed before this goes in, though: the rediscovered features cannot be written back with the stock FileCache, and the failed flush leaves the cache file empty (details inline).

This also makes me unsure how the stale pickle arises. On main (7.38.0), after a fresh V1 discovery FileCache.flush() already fails the same way, so I could not get a stock setup to produce a pickled DeviceFeatures at all. How was the file on your side written, with a custom serialize_fn perhaps?

The first commit of my #1092 (ef100c4d) fixes the same stale-cache case, and it has the same write-back problem. I think this cache fix should land first, on its own, with the write-back fixed. #1092 is still a draft, so I will drop that commit and rebase on top of this one.

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.


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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants