Skip to content

Avoid root dirty delta for shared state events - #6841

Merged
FarhanAliRaza merged 34 commits into
reflex-dev:mainfrom
harsh21234i:fix/shared-state-root-dirty-6392
Dec 1, 2026
Merged

FarhanAliRaza merged 34 commits into
reflex-dev:mainfrom
harsh21234i:fix/shared-state-root-dirty-6392

Conversation

@harsh21234i

@harsh21234i harsh21234i commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Tests

  • uv run --frozen pytest tests/units/test_state.py::test_linked_state_event_does_not_dirty_root_state -q
  • uv run --frozen pytest tests/units/test_state.py::test_linked_state_event_does_not_dirty_root_state tests/units/test_state.py::test_router_var_dep tests/units/test_state.py::test_computed_var_depends_on_parent_non_cached tests/units/test_state.py::test_async_computed_var_get_state -q
  • uv run --frozen pytest tests/units/test_state.py -q
  • uv run --frozen pre-commit run --files reflex/istate/shared.py tests/units/test_state.py news/6841.bugfix.md

Closes #6392

Review in cubic

@harsh21234i
harsh21234i requested a review from a team as a code owner August 4, 2026 11:06
@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

codspeed Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 4.79%

2 warnings

⚠️ 22 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 2 improved benchmarks
✅ 144 untouched benchmarks
⏩ 8 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_proxy_list_mutation 9.7 ms 9.2 ms +4.9%
⚡ test_modify_state[linked] 11.9 ms 11.4 ms +4.68%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing harsh21234i:fix/shared-state-root-dirty-6392 (e9f3235) with main (ee44782)

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 Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] Changes how shared state computes dirty variables after updates.

The PR is not yet safe to merge because discarded resolution failures can leave dirty state and refreshed caches behind, and unchanged router-dependent computed values are still emitted.

Summary

The PR changes linked/shared-state patching to invalidate only computed variables that depend on router data instead of dirtying the router fields themselves.

  • Replaces explicit root router dirtiness with dependency-based computed invalidation.
  • Adds linked-state delta tests covering unchanged router fields, real router changes, and synchronized storage.
  • Adds a user-facing bugfix news fragment.

Reviews (35) · Last reviewed commit: "Make the link case of the delta test rel..." · Reviewed by Greptile

Comment thread reflex/istate/shared.py Resolved

@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.

Fix all with cubic | Re-trigger cubic

Comment thread reflex/istate/shared.py Resolved
Comment thread reflex/istate/shared.py Resolved
Comment thread reflex/istate/shared.py Resolved
@harsh21234i

Copy link
Copy Markdown
Contributor Author

hey @masenf The state-dirty cleanup changes and regression tests are now implemented, including descendant cleanup when delta resolution fails. Most CI checks are passing, but reflex-install-and-init is failing. Could you confirm whether that failure is infrastructure-related or requires a code change? Once it passes, could you please review the latest commit?

Comment thread reflex/istate/shared.py Resolved
Comment thread reflex/istate/shared.py Resolved

@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.

patch breaks delta calculation for computed vars in shared state that depend on root state.

see https://github.com/reflex-dev/reflex/blob/claude/substate-dirty-tracking-demo-rple7z/shared_state_router_delta_repro.py

@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 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread reflex/istate/shared.py Resolved
Comment thread reflex/istate/shared.py Resolved
Comment thread reflex/istate/shared.py Resolved
Comment thread reflex/istate/shared.py Resolved

@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 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread reflex/istate/shared.py Resolved

@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 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread reflex/istate/shared.py Resolved
Comment thread reflex/istate/shared.py Resolved

@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 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

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

Copy link
Copy Markdown
Contributor Author

hey @MANsef could you please review the latest commit?

@masenf masenf added the bug Something isn't working label Oct 6, 2026
Comment thread tests/units/istate/test_shared.py Resolved
@harsh21234i

Copy link
Copy Markdown
Contributor Author

hey @masenf please take a look!!

Comment thread reflex/istate/shared.py Resolved
Comment thread tests/units/istate/test_shared.py Resolved
Comment thread tests/units/istate/test_shared.py Resolved
@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 30, 2026
@FarhanAliRaza FarhanAliRaza added the priority: P2 Medium: valid edge-case bug, reasonable feature, or docs fix label Nov 30, 2026 — with Claude
Comment thread reflex/istate/shared.py Resolved
Replace the tests of the snapshot and rollback machinery with tests of what
a client receives: the shared change and the router readers computed for its
own tree, without the unchanged router, whether the event links the state or
not; a router change made by the event itself; a synced storage value.

The link case fails here: linking still sends the unchanged router.
_patch_state marked the root's router fields dirty to recompute the computed
vars that read them. The fields themselves had not changed, but they stayed
dirty and were sent with every event of a linked client. Mark their readers
dirty instead, and leave the root's dirty vars alone.

This replaces the snapshot and rollback of the whole tree, and the storage
sync bookkeeping it needed, with one call.
The redis lock queue and app name validation test changes do not concern
linked-state deltas. Restore them to main.

@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 6 files (changes from recent commits).

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

View guided diff | Re-trigger cubic

Comment thread tests/units/istate/test_shared.py Resolved
@FarhanAliRaza
FarhanAliRaza merged commit d0d28d4 into reflex-dev:main Dec 1, 2026
170 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working on deck PRs lined up to review / merge next perf Performance-improving changes priority: P2 Medium: valid edge-case bug, reasonable feature, or docs fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Events against SharedState should not mark root state dirty all the time

3 participants