Fix disk state manager debounced writes
Closed
HA
3mo ago+1786+1786+0123456789012345678901234567890123456789-102-102-012345678901234567890123456789
Fixes #6806
Summary
Fix debounced StateManagerDisk.set_state() writes so repeated updates for the same token persist the latest value instead of the first queued value.
Problem
When disk write debouncing was enabled, only the first write for a token was retained in _write_queue. A later write before the debounce flush was silently discarded, so background processing or close() could persist stale state.
Changes
- Replace an existing queued payload with the latest state while preserving the original queue timestamp.
- Keep the in-memory cache aligned when
set_state()receives an arbitrary instance instead of the one returned byget_state(). - Explicitly persist supplied
BaseStateinstances, including untouched instances, so the cache never points to a state that was not written to disk. - Preserve the existing touched-state optimization for the normal same-instance path.
Testing
uv run pytest tests/units/test_state.py::test_state_manager_disk_debounced_set_state_flushes_latest_non_base_state -quv run pytest tests/units/test_state.py::test_state_manager_disk_set_state_updates_cache_for_arbitrary_instance -quv run pytest tests/units/test_state.py::test_state_manager_disk_set_state_persists_untouched_base_state -quv run ruff check reflex/istate/manager/disk.py tests/units/test_state.pyuv run ruff format --check reflex/istate/manager/disk.py tests/units/test_state.pygit diff --check