Replace StateProxy with state locks held by the EventContext

MA
1mo ago
+1046-923

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Part 2 of 4 of #7302. Stack: 1 field descriptors (#7312) → 2 state locks → 3 core state in reflex-base → 4 dispatch_value. Based on #7312.

Description

Removes StateProxy, ReadOnlyStateProxy and ImmutableMutableProxy. Whether a state is writable is now decided at write time, in one place:

  • EventContext tracks the state trees whose locks it holds (StateLocks). EventContext.modify_state binds the root to the context (_event_context), holds the lock for the whole event, and is the active context in its task while it holds it.
  • Every Field write and every MutableProxy change checks that the event context running in the current task holds the lock on the state's tree. If it doesn't, the write raises ImmutableStateError. Reads are always allowed.
  • async with state takes the lock again under the context the state was loaded in. It reloads the tree and puts this instance back into it, emits the delta on exit, and then hands the place back to the reloaded instance.
  • Background tasks, rx.get_state and callbacks get the plain state, read-only until entered with async with.
  • link_token_to_sid now takes the lock through an event context. With the in-memory manager, the state it got was still bound to an earlier event, so recording the reconnecting session raised ImmutableStateError. A regression test covers this.

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

šŸ¤– Generated with Claude Code

https://claude.ai/code/session_0145YmAVjKSJhdUFDcBP4FB9


Generated by Claude Code