fix(shared): prevent AB-BA deadlocks when locking linked states

IM
2h ago
+1155-64

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_links sorted 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-token AsyncExitStack and snapshot tracking in _HeldToken allow _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. Under StateManagerRedis (oplock=False), _refresh_reacquired_state() updates the previously held SharedState instance in place so existing local references are preserved.
  • Rollback on failed _link_to (reflex/istate/shared.py): _link_to() restores _reflex_internal_links and defers discarding _linked_from until 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) raise ReflexRuntimeError immediately 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.py and tests/units/istate/manager/test_manager_locks.py covering canonical lock ordering (memory, disk, redis), the standalone dynamic _link_to reproducer (20/20 runs passing), same-handler dual _link_to in opposite orders, lazy _resolve_linked_state preserving 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 . and uv run --no-sync ruff format --check . clean
    • uv run --no-sync pyright --pythonplatform Linux reflex tests (0 errors)

closes #8064