-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
If a MutableProxy (used for state reactivity) is nested inside a standard Python collection (such as a list or dict) inside the rx.State, the object identity of the MutableProxy is not maintained during the pickling/unpickling operation. As Reflex uses object identities to maintain "deltas" and push updates to the frontend, this results in the UI ceasing to respond to state changes once a session is reloaded from a Redis or disk-backed cache.
To Reproduce Steps to reproduce the behavior:
Code/Link to Repo:
import reflex as rx
import pickle
from reflex.state import MutableProxy
# 1. Define a base object that Reflex would wrap in a proxy
class SubState(rx.Base):
value: int = 0
def reproduce_identity_loss():
# 2. Simulate a proxy being put into a nested structure
original_obj = SubState()
proxy = MutableProxy(original_obj)
state_dict = {"data": proxy}
original_id = id(state_dict["data"])
# 3. Simulate session serialization (Pickling)
serialized = pickle.dumps(state_dict)
reloaded_dict = pickle.loads(serialized)
# 4. Compare IDs
new_id = id(reloaded_dict["data"])
print(f"Original ID: {original_id}")
print(f"Reloaded ID: {new_id}")
if original_id != new_id:
raise AssertionError("Identity Mismatch: The state tracker will lose track of this object!")
if __name__ == "__main__":
reproduce_identity_loss()The id() of the MutableProxy should remain the same or be automatically re-registered with the State tracker when unpickling. This ensures that even after a session refresh or a server restart (where state is retrieved from a cache), the frontend stays responsive to changes in the nested data structures.
Specifics:
Python Version: 3.10 or 3.11
Reflex Version: 0.8.26 (Latest)
OS: (Insert your OS here, e.g., Windows 11 / macOS / Ubuntu)
Browser (Optional): Chrome/Firefox
Additional context :
This seems to be a regression/edge-case issue related to the recent fixes in #6097 and #6105. Although top-level state variables are properly addressed, the recursive tracking for nested proxies in collections appears to drop the memory reference in setstate. This is especially important for Enterprise users running Reflex in production with persistent session storage.