fix(state): persist cancelled redis handler updates

Merged
KR
1mo ago
+293-6

Summary

Fixes #7248.

When a foreground handler is cancelled by a superseding event, Redis can discard every state change it made before cancellation, including updates already yielded to the client. The replacement handler then reads the old state. Preserve those changes before releasing the Redis lock so the replacement sees the same history as it does with in-memory state.

What changed

  • Share cancellation-aware persistence across the three non-cached Redis locking paths. Save under the existing lock fence and re-raise cancellation afterward.
  • Cover root and substate writes, handler cleanup, cached leases, expired locks and non-cancellation exceptions.
  • Exercise superseding handlers through the event processor and check both stored state and emitted deltas. Document the behavior and add a news fragment.

Verification

  • Reproduced three failing manager cases and the failing Redis event-processor case before the fix using an isolated Redis server.
  • uv run pytest tests/units --cov --no-cov-on-fail --cov-report=: 11,193 passed, 155 skipped; 78.68% coverage.
  • Redis manager tests against real Redis: 27 passed. Event-processor regression with real Redis and the in-memory control: 2 passed.
  • uv run ruff check ., uv run ruff format --check . and uv run pyright reflex tests passed.
  • All pre-commit hooks applicable to the five changed files passed.
  • All-files pre-commit passed formatting, lint, spelling, stub generation and generated-source checks. Its Pyright hook found six existing macOS CPU-affinity errors in unchanged benchmark files; those files pass with --pythonplatform Linux, matching CI. The remaining ty and Biome hooks passed separately.