fix(shared): prevent AB-BA deadlocks when locking linked states
IM
2h ago+1155+1155+0123456789012345678901234567890123456789-64-64-01234567890123456789
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
- Bug fix (non-breaking change which fixes an issue)
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?
Summary
Prevents permanent AB-BA deadlocks and lock-order stalls when multiple clients link or resolve rx.SharedState instances in opposite token orders across StateManagerMemory, StateManagerDisk, and StateManagerRedis:
- Canonical link lock ordering (
reflex/istate/shared.py):_modify_linked_states()now iterates_reflex_internal_linkssorted by(token, state_name)so pre-existing linked states always acquire locks in a consistent order across clients. - Ordered re-acquisition for dynamic/lazy links (
reflex/istate/shared.py): Per-tokenAsyncExitStackand snapshot tracking in_HeldTokenallow_internal_patch_linked_state()to temporarily release clean, idle higher-ordered locks before acquiring a lower-ordered token (e.g. concurrent_link_to()or lazy_resolve_linked_state()), then re-acquire and re-patch them in canonical order. UnderStateManagerRedis(oplock=False),_refresh_reacquired_state()updates the previously heldSharedStateinstance in place so existing local references are preserved. - Rollback on failed
_link_to(reflex/istate/shared.py):_link_to()restores_reflex_internal_linksand defers discarding_linked_fromuntil patching succeeds, and_internal_patch_linked_state()best-effort restores any temporarily released locks before propagating an exception. - In-process wait-for cycle detection (
reflex/istate/manager/__init__.py,memory.py,disk.py): Tracks local lock ownership (_lock_owners) and waiters (_waiting_for) so unreleasable lock cycles (dirty higher-ordered locks, cross-client or self-links) raiseReflexRuntimeErrorimmediately instead of hanging indefinitely. - News fragment: Added
news/+shared-state-deadlock.bugfix.md.
Testing
- Added unit and regression tests in
tests/units/istate/test_shared.pyandtests/units/istate/manager/test_manager_locks.pycovering canonical lock ordering (memory,disk,redis), the standalone dynamic_link_toreproducer (20/20 runs passing), same-handler dual_link_toin opposite orders, lazy_resolve_linked_statepreserving local references under Redis, dirty/cross-client/self-link cycle detection, and rollback of released higher locks on failure. - Verified locally:
uv run --no-sync pytest tests/units/istate tests/units/benchmarks/test_shared_state.py tests/benchmarks/test_shared_state.py(331 passed)uv run --no-sync pytest tests/units/test_app.py tests/units/test_state.py(727 passed, 2 skipped)uv run --no-sync pytest tests/units/reflex_base/event/processor/test_base_state_processor.py(421 passed)uv run --no-sync ruff check .anduv run --no-sync ruff format --check .cleanuv run --no-sync pyright --pythonplatform Linux reflex tests(0 errors)
closes #8064