Skip to content

Fix disk state manager debounced writes - #6807

Open
harsh21234i wants to merge 3 commits into
reflex-dev:mainfrom
harsh21234i:fix/disk-state-manager-debounced-writes
Open

Fix disk state manager debounced writes#6807
harsh21234i wants to merge 3 commits into
reflex-dev:mainfrom
harsh21234i:fix/disk-state-manager-debounced-writes

Conversation

@harsh21234i

@harsh21234i harsh21234i commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #6806

Summary

Fixes a disk state manager bug where debounced set_state() calls for non-BaseState tokens could return stale in-
memory values and flush the wrong value to disk.

Problem

When StateManagerDisk used _write_debounce_seconds > 0, set_state() only queued the first value for a token. A
later set_state() before the debounce flush did not update the queued item, so close() or the background flush
could persist stale data.

Also, non-BaseState values were not updated in the in-memory cache while the write was pending, so get_state()
could return the default/old value before the disk flush completed.

Changes

  • Update the in-memory cache immediately for non-BaseState tokens.
  • Replace existing queued write payloads with the latest value.
  • Preserve the original queue timestamp so repeated updates do not indefinitely delay the flush.
  • Add regression tests for pending read-after-write behavior and latest-value flushing.

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 -q
  • uv 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" -q
  • uv run ruff check reflex/istate/manager/disk.py tests/units/test_state.py
  • uv run ruff format --check reflex/istate/manager/disk.py tests/units/test_state.py
  • uv run pyright reflex/istate/manager/disk.py tests/units/test_state.py

@harsh21234i
harsh21234i requested a review from a team as a code owner July 23, 2026 09:53
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@codspeed-hq

codspeed-hq Bot commented Jul 23, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing harsh21234i:fix/disk-state-manager-debounced-writes (5094278) with main (9854a3a)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@greptile-apps

greptile-apps Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes cache coherence and queued writes in the disk state manager. The main changes are:

  • Updates pending non-BaseState values in memory.
  • Replaces queued payloads with the latest value.
  • Preserves the original debounce timestamp.
  • Keeps the previous cache value when an immediate write fails.
  • Adds tests for pending reads, flushing, and write failures.

Confidence Score: 5/5

No additional blocking issue was found in this follow-up.

  • The immediate failure path now updates the cache only after persistence succeeds.
  • The new tests cover the intended successful write behavior and immediate write failure.

Important Files Changed

Filename Overview
reflex/istate/manager/disk.py Updates cache ordering and queued payload replacement for immediate and debounced disk writes.
tests/units/test_state.py Adds tests for pending cache reads, latest-value flushing, and immediate write failures.
news/6807.bugfix.md Documents the disk state manager bug fix.

Reviews (3): Last reviewed commit: "Add changelog fragment for disk state fi..." | Re-trigger Greptile

Comment thread reflex/istate/manager/disk.py
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.

StateManagerDisk drops debounced set_state updates for non-BaseState tokens

1 participant