Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
148da1e
Avoid root dirty delta for shared state events
Aug 4, 2026
eac81b3
Handle linked state patch resolve failures
Aug 4, 2026
e90f8f4
Restore descendant dirty state on linked patch errors
Aug 8, 2026
1ca1067
Restore descendant dirty state after shared patch
Aug 13, 2026
561b2c3
Preserve computed refreshes during shared state patching
Aug 15, 2026
779d62e
Preserve linked computed refresh paths
Aug 15, 2026
8f9548b
Restore computed caches after shared patch failure
Aug 15, 2026
1ada24d
Restore touched state after shared patch failure
Aug 15, 2026
0b92c58
Handle non-scalar computed cache snapshots
Aug 15, 2026
d4f64bc
style: sort shared state imports
harsh21234i Sep 27, 2026
3dd33c4
Merge branch 'main' into fix/shared-state-root-dirty-6392
harsh21234i Sep 29, 2026
5f36616
Fix shared-state computed delta filtering
harsh21234i Oct 2, 2026
b2fe247
Limit linked-state cache comparisons to router deps
harsh21234i Oct 2, 2026
72d4db5
Preserve interval refreshes in linked state deltas
harsh21234i Oct 2, 2026
50f8bd6
Preserve downstream interval computed updates
harsh21234i Oct 2, 2026
078c9fd
Merge upstream main into shared state fix
harsh21234i Oct 2, 2026
e46fa8a
test: make app name validation portable
harsh21234i Oct 3, 2026
5650887
Merge upstream main into shared state fix
harsh21234i Oct 7, 2026
bc8a695
Skip unnecessary linked-state router refresh
harsh21234i Oct 7, 2026
729a18e
Merge branch 'main' into fix/shared-state-root-dirty-6392
harsh21234i Oct 17, 2026
cca0470
test: move shared module import to module scope
harsh21234i Oct 17, 2026
539dcc4
Merge branch 'main' into fix/shared-state-root-dirty-6392
harsh21234i Oct 22, 2026
ae45ca2
test: synchronize redis lock contention queue
harsh21234i Oct 22, 2026
ebcc513
Merge upstream main into PR #6841
harsh21234i Nov 1, 2026
333562e
fix: preserve synchronized storage updates in linked deltas
harsh21234i Nov 1, 2026
65064f6
Merge main into PR #6841 and update shared-state tests
harsh21234i Nov 27, 2026
e925bd4
test: assert complete linked-state deltas and errors
harsh21234i Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/6841.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid leaking temporary router dirtiness into linked/shared state event deltas.
300 changes: 293 additions & 7 deletions reflex/istate/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
import logging
from collections.abc import AsyncIterator
from dataclasses import MISSING
from typing import TYPE_CHECKING, Self, TypeVar
from typing import TYPE_CHECKING, Any, Self, TypeVar

from reflex_base.constants import ROUTER_DATA, ROUTER_VARS
from reflex_base.event import Event, get_hydrate_event
from reflex_base.event.context import EventContext
from reflex_base.registry import RegistrationContext
from reflex_base.session import SessionToken, authorize, is_client_token
from reflex_base.utils.exceptions import ReflexRuntimeError
from reflex_base.vars.base import _owner_state
from reflex_base.vars.base import _delta_value_key, _owner_state

from reflex.istate.delta import _suppress_delta_recording
from reflex.istate.manager.token import BaseStateToken
from reflex.state import BaseState, OnLoadInternalState, State, _override_base_method
from reflex.state import (
BaseState,
OnLoadInternalState,
State,
_override_base_method,
_shared_storage_updates,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -117,14 +123,294 @@ async def _patch_state(
linked_state.dirty_vars.update(linked_state.computed_vars)
linked_state._mark_dirty()
# Apply the updates into the existing state tree for rehydrate.
# For regular linked-state events this router dirtiness is temporary:
# it forces router-dependent computed vars to resolve for the patched
# tree, but should not leak into the event's final delta.
root_state = original_state._get_root_state()
if (
not full_delta
and not any(
root_state._var_dependencies.get(name)
for name in (*ROUTER_VARS, ROUTER_DATA)
)
and all(
name in ROUTER_VARS or name == ROUTER_DATA
for name in root_state.dirty_vars
)
and not root_state._potentially_dirty_states
):
states_to_check = [root_state]
while states_to_check:
state = states_to_check.pop()
if state.computed_vars:
break
states_to_check.extend(
state.substates[name]
for name in state.dirty_substates | state._always_dirty_substates
if name in state.substates
)
else:
# There is no router-dependent or otherwise pending computed
# work, so resolving this discarded delta cannot change state.
yield
return
dirty_state_snapshots: list[
tuple[
BaseState,
set[str],
set[str],
set[str],
bool,
]
] = []
states_by_name: dict[str, BaseState] = {}
computed_var_cache_snapshots: dict[
str, dict[str, tuple[bool, Any, bool, Any]]
] = {}
if not full_delta:
states_to_snapshot = [root_state]
while states_to_snapshot:
state = states_to_snapshot.pop()
# Interval and always-dirty computed vars are valid output from
# this refresh and must not be mistaken for temporary router dirtiness.
computed_vars_to_preserve = state._expired_computed_vars().union(
state._always_dirty_computed_vars
)
states_by_name[state.get_full_name()] = state
dirty_state_snapshots.append((
state,
set(state.dirty_vars),
set(state.dirty_substates),
computed_vars_to_preserve,
state._was_touched,
))
states_to_snapshot.extend(state.substates.values())
computed_vars_to_snapshot = {
state.get_full_name(): (
dirty_vars.intersection(state.computed_vars)
| computed_vars_to_preserve
| state._interval_computed_var_names
)
for (
state,
dirty_vars,
_,
computed_vars_to_preserve,
_,
) in dirty_state_snapshots
}
# Track only caches that dirty propagation can invalidate, before
# marking router vars deletes those cached values.
pending_dependencies = [
(state, name)
for (
state,
dirty_vars,
_,
computed_vars_to_preserve,
_,
) in dirty_state_snapshots
for name in dirty_vars | computed_vars_to_preserve
]
pending_dependencies.extend((root_state, name) for name in ROUTER_VARS)
pending_dependencies.append((root_state, ROUTER_DATA))
seen_dependencies: set[tuple[str, str]] = set()
while pending_dependencies:
state, name = pending_dependencies.pop()
dependency = (state.get_full_name(), name)
if dependency in seen_dependencies:
continue
seen_dependencies.add(dependency)
for target_name, computed_var_name in state._var_dependencies.get(
name, ()
):
target_state = states_by_name.get(target_name)
if target_state is None:
continue
target_vars = computed_vars_to_snapshot[target_name]
target_vars.add(computed_var_name)
pending_dependencies.append((target_state, computed_var_name))
for (
state,
_,
_,
_,
_,
) in dirty_state_snapshots:
computed_var_cache_snapshots[state.get_full_name()] = {
name: (
hasattr(state, state.computed_vars[name]._cache_attr),
getattr(state, state.computed_vars[name]._cache_attr, None),
hasattr(state, state.computed_vars[name]._last_updated_attr),
getattr(
state, state.computed_vars[name]._last_updated_attr, None
),
)
for name in computed_vars_to_snapshot[state.get_full_name()]
}
root_state.dirty_vars.update(ROUTER_VARS)
root_state.dirty_vars.add(ROUTER_DATA)
root_state._mark_dirty()
# The delta is discarded: it is only resolved to refresh computed vars,
# so its values must not count as sent to the client.
with _suppress_delta_recording():
await root_state._get_resolved_delta()
router_dirty_snapshots: list[
tuple[
BaseState,
set[str],
set[str],
set[str],
set[str],
set[str],
dict[str, tuple[bool, Any]],
dict[str, tuple[bool, Any, bool, Any]],
]
] = []
if not full_delta:
for (
state,
dirty_vars,
dirty_substates,
computed_vars_to_preserve,
_,
) in dirty_state_snapshots:
computed_var_snapshots = computed_var_cache_snapshots[
state.get_full_name()
]
router_dirty_vars = (
state.dirty_vars - dirty_vars - computed_vars_to_preserve
)
router_computed_snapshots = {
name: (
computed_var_snapshots[name][0],
_delta_value_key(computed_var_snapshots[name][1])
if computed_var_snapshots[name][0]
else None,
)
for name in router_dirty_vars.intersection(computed_var_snapshots)
}
router_dirty_snapshots.append((
state,
dirty_vars,
dirty_substates,
router_dirty_vars,
state.dirty_substates - dirty_substates,
computed_vars_to_preserve,
router_computed_snapshots,
computed_var_snapshots,
))
storage_updates: set[tuple[str, str]] = set()
storage_updates_token = _shared_storage_updates.set(storage_updates)
try:
try:
# The delta is discarded: it is only resolved to refresh computed vars,
# so its values must not count as sent to the client.
with _suppress_delta_recording():
await root_state._get_resolved_delta()
finally:
_shared_storage_updates.reset(storage_updates_token)
except BaseException:
if not full_delta:
for (
state,
dirty_vars,
dirty_substates,
_,
was_touched,
) in dirty_state_snapshots:
computed_var_snapshots = computed_var_cache_snapshots[
state.get_full_name()
]
state.dirty_vars = dirty_vars
state.dirty_substates = dirty_substates
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
state._was_touched = was_touched
for name, (
had_cache,
cached_value,
had_last_updated,
last_updated,
) in computed_var_snapshots.items():
computed_var = state.computed_vars[name]
if had_cache:
setattr(state, computed_var._cache_attr, cached_value)
else:
with contextlib.suppress(AttributeError):
delattr(state, computed_var._cache_attr)
if had_last_updated:
setattr(
state,
computed_var._last_updated_attr,
last_updated,
)
else:
with contextlib.suppress(AttributeError):
delattr(state, computed_var._last_updated_attr)
raise
else:
if not full_delta:
refreshed_states: list[BaseState] = []
for (
state,
dirty_vars,
dirty_substates,
router_dirty_vars,
_,
computed_vars_to_preserve,
router_computed_snapshots,
computed_var_snapshots,
) in router_dirty_snapshots:
computed_vars_refreshed = set(computed_vars_to_preserve) | (
state.dirty_vars - router_dirty_vars
).intersection(state.computed_vars)
computed_vars_refreshed.update(
name
for name in state._interval_computed_var_names
if (
computed_var_snapshots[name][2]
!= hasattr(
state,
state.computed_vars[name]._last_updated_attr,
)
or computed_var_snapshots[name][3]
!= getattr(
state,
state.computed_vars[name]._last_updated_attr,
None,
)
)
)
computed_vars_refreshed.update(
name
for name, (
had_cache,
cached_value_key,
) in router_computed_snapshots.items()
if hasattr(state, state.computed_vars[name]._cache_attr)
and (
not had_cache
or cached_value_key
!= _delta_value_key(
getattr(state, state.computed_vars[name]._cache_attr)
)
)
)
field_vars_refreshed = {
var_name
for state_name, var_name in storage_updates
if state_name == state.get_full_name()
and var_name not in state.computed_vars
}
if computed_vars_refreshed or field_vars_refreshed:
refreshed_states.append(state)
state.dirty_vars = (
dirty_vars
| computed_vars_to_preserve
| computed_vars_refreshed
| field_vars_refreshed
)
state.dirty_substates = dirty_substates
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
for refreshed_state in refreshed_states:
state = refreshed_state
while state.parent_state is not None:
state.parent_state.dirty_substates.add(state.get_name())
state = state.parent_state
yield
finally:
original_parent_state.substates[state_name] = original_state
Expand Down
7 changes: 7 additions & 0 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re
import sys
from collections.abc import Callable, Iterable, Iterator, Sequence
from contextvars import ContextVar
from datetime import timedelta
from hashlib import md5
from types import FunctionType
Expand Down Expand Up @@ -107,6 +108,10 @@
from reflex.utils import console, format, types
from reflex.utils.exec import is_testing_env

_shared_storage_updates: ContextVar[set[tuple[str, str]] | None] = ContextVar(
"shared_storage_updates", default=None
)


@functools.cache
def _stale_pickle_keys(cls: type[BaseState]) -> frozenset[str]:
Expand Down Expand Up @@ -2537,6 +2542,8 @@ async def _sync_shared_storage(state: BaseState) -> None:
target = await state.get_state(state_cls)
if target.get_value(var_name) != value:
setattr(target, var_name, value)
if (updates := _shared_storage_updates.get()) is not None:
updates.add((target.get_full_name(), var_name))


def _loaded_substate(root: BaseState, state_name: str) -> BaseState | None:
Expand Down
Loading
Loading