Skip to content

fix(state): persist explicitly supplied states in StateManagerDisk - #7159

Merged
masenf merged 4 commits into
reflex-dev:mainfrom
FarhanAliRaza:farhan/disk-set-state-touched
Sep 17, 2026
Merged

masenf merged 4 commits into
reflex-dev:mainfrom
FarhanAliRaza:farhan/disk-set-state-touched

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

StateManagerDisk.set_state silently lost data when called with a state instance that was not the one returned by get_state:

  • The debounced write queue kept the first queued object and ignored later calls, so the new instance never reached disk.
  • The in-memory cache (self.states) was never updated, so subsequent get_state calls returned the stale instance.
  • A freshly constructed instance has no touched flags set, so even the immediate write path skipped it entirely.

Approach

Alternative implementation of #6807 (thanks @harsh21234i for the analysis and regression tests). Instead of threading a force_write parameter through QueueItem, set_state_for_substate, and both flush paths, this encodes the persistence decision where it already lives: the state's own _was_touched flag.

set_state does one O(1) identity check against the cache. If the supplied instance is not the cached one, it marks the whole tree touched — the existing touched-driven machinery then persists it with zero signature changes to set_state_for_substate or the flush paths. The flag is sticky until write time, which also covers accumulation across debounced updates for free. Setting _was_touched externally is an established pattern (_screenshot.py, vars/base.py), the flag is stripped on pickling, and its docstring states it exists exactly for this purpose ("determines whether this state instance should be persisted").

Also:

  • Queued QueueItems are updated in place (queued_item.state = state) instead of ignored, per masenf's preference in Fix disk state manager debounced writes #6807.
  • The cache is updated on both paths (after the write on the immediate path, so a failed write is not cached).
  • set_state records _token_last_touched, so a token that is only ever written still expires instead of leaking cache entries.

Non-BaseState tokens need no marking: their get_and_reset_touched_state always returns True.

Testing

Regression tests (ported from #6807, written to fail before the fix): latest-queued-value flush, cache replacement for arbitrary instances (both debounce modes), and persistence of an untouched BaseState (both debounce modes).

Fixes #6806

Review in cubic

When set_state receives a state instance that was not obtained from
get_state, mark the whole state tree as touched so the existing
touched-driven persistence machinery writes it to disk. Keep the
in-memory cache aligned with the supplied instance, update queued
write items in place so debounced writes flush the latest value, and
record last-touched so set_state-only tokens still expire.

Alternative implementation of reflex-dev#6807 that encodes the persistence
decision in the state's own touched flag instead of threading a
force_write parameter through QueueItem, set_state_for_substate, and
both flush paths.
@FarhanAliRaza
FarhanAliRaza requested a review from a team as a code owner September 15, 2026 20:08
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-15T20:15:52.399561Z 54a8ab6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge. No new issue remains.

What we checked:

  • Latest queued state is lost: No. set_state replaces the queued value, and the test checks that the same queue item holds the latest value.

Summary

StateManagerDisk.set_state now keeps, caches, and writes the exact state instance callers provide. It also marks replacement state trees for persistence and keeps token expiration tracking up to date.

  • Debounced writes now use the latest supplied value.
  • Explicit state instances replace stale cache entries.
  • Untouched BaseState trees are persisted correctly.

Reviews (4) · Last reviewed commit: "Invert identity and isinstance check for..."

Comment thread tests/units/test_state.py Resolved
@codspeed

codspeed Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 53 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing FarhanAliRaza:farhan/disk-set-state-touched (b0f1160) with main (aaa44a0)

Open in CodSpeed

Footnotes

  1. 9 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. ↩

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread reflex/istate/manager/disk.py Resolved
Unit tests mirror the source tree; the StateManagerDisk regression
tests belong in tests/units/istate/manager/test_disk.py. Give the
untouched-persistence test a local DiskPersistState instead of the
test_state fixture so the module stays self-contained.

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm trying to see how possible it is to add benchmarks for the state managers so we actually can easily answer the question i've asked here.

Comment thread reflex/istate/manager/disk.py Resolved
Comment thread reflex/istate/manager/disk.py Resolved
@masenf
masenf merged commit ef4b481 into reflex-dev:main Sep 17, 2026
125 checks passed
masenf pushed a commit that referenced this pull request Sep 19, 2026
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

2 participants