Fix disk state manager debounced writes - #6807
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
Merging this PR will not alter performance
Comparing Footnotes
|
Greptile SummaryThis PR fixes two related bugs in
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| reflex/istate/manager/disk.py | Two targeted fixes in set_state: optimistic in-memory cache update for non-BaseState tokens in the debounce path, and cache update moved to after a successful disk write in the immediate path. Logic is correct and consistent with the token type semantics (StateToken.get_and_reset_touched_state always returns True, so writes always happen). |
| tests/units/test_state.py | Three new regression tests covering: read-after-write visibility in debounce mode, latest-value flushing with timestamp preservation, and cache integrity after failed immediate writes. All three exercise the code paths changed by the fix. |
| news/6807.bugfix.md | Changelog entry describing the debounce fix. |
Reviews (4): Last reviewed commit: "Merge branch 'main' into fix/disk-state-..." | Re-trigger Greptile
|
hey @masenf @FarhanAliRaza can you go through this? |
Fixes #6806
Summary
Fixes a disk state manager bug where debounced
set_state()calls for non-BaseStatetokens could return stale in-memory values and flush the wrong value to disk.
Problem
When
StateManagerDiskused_write_debounce_seconds > 0,set_state()only queued the first value for a token. Alater
set_state()before the debounce flush did not update the queued item, soclose()or the background flushcould persist stale data.
Also, non-
BaseStatevalues were not updated in the in-memory cache while the write was pending, soget_state()could return the default/old value before the disk flush completed.
Changes
BaseStatetokens.Testing
uv run pytest tests/units/test_state.py::test_state_manager_disk_debounced_set_state_updates_non_base_state_cache 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 -k "deserialize_gc_state_disk or state_manager_disk_close_resets_write_queue_task or state_manager_disk_debounced_set_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.pyuv run pyright reflex/istate/manager/disk.py tests/units/test_state.py