Check out states in the EventContext, make StateManager a key-value store

Draft
MA
1mo ago
+1530-803

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 of #7302. This is meant to become the bottom of the stack (#7313–#7315 are not rebased onto it yet).

Description

The EventContext now owns the state instances used while processing an event and the lock on them. StateManager is reduced to a key-value store of single state instances.

EventContext (reflex_base/event/context.py)

  • get_state(token) checks a state out in the context: one instance per state class and ident, keyed by str(token). The states a token requires (substates, potentially dirty states, parents) are loaded together and linked into one tree, so two lookups in the same event can't get different instances or disjoint trees.
  • modify_state(token) holds the lock on the token's ident. Acquiring it refreshes the ident's checked-out instances in place (same objects, stored values copied in). Releasing it stores the touched states. A load started before the lock was taken is redone under it.
  • The tasks of a context share its states, so they share its lock: a task entering modify_state while another holds it joins the lock, and the last one to leave stores and releases. hold_while_locked(ident, cm) keeps a context manager entered until then. Linked shared states use this to stay patched in for the lifetime of the lock.

StateManager (reflex/istate/manager/)

  • A manager implements load_states(tokens, create=...), store_states(states, lease) and lock(token). The memory, disk and Redis managers are rewritten to this. The Redis opportunistic lock caches single instances under its local lease and writes the touched ones when the lease breaks.
  • get_state, set_state, modify_state and modify_state_with_links remain on every manager and go through a context: the current one when it's for the same token and manager, otherwise a new one. They still return the root of the tree.
  • Tree-specific behavior lives on the token: required_tokens, new_instance, link and refresh on StateToken / BaseStateToken.

Call sites: the event processor, background-task StateProxy, App.modify_state, rx.get_state, shared-state linking and loading a missing state from within another state all go through the event context.

Breaking

  • Custom StateManager subclasses must implement load_states, store_states and lock.
  • With the in-memory and disk managers, state_manager.get_state returns a tree with only the states the requested one needs, like Redis already did.

Performance
Event processing is faster than main in most benchmarks (warm 0.63x, cold 0.82x, 100-event same-client burst 0.31x). Calling the state manager directly is slower (in-memory get_state ~33µs → ~1ms in the micro-benchmark), because each call builds a context and links the tree instead of a single dict lookup. Only App.modify_state, reconnects and the screenshot plugin take that path; CodSpeed will flag the test_state_manager benchmarks.

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?

Unit tests pass with the in-memory manager, Redis, and Redis with the opportunistic lock, and on Python 3.10; the Playwright integration suite passes. New tests in tests/units/reflex_base/event/test_context.py use a minimal pickle-backed manager to cover single instances per context, in-place refresh, touched-only stores, the shared lock, hold_while_locked, and the load/lock race.

šŸ¤– Generated with Claude Code

https://claude.ai/code/session_0145YmAVjKSJhdUFDcBP4FB9


Generated by Claude Code