Redis state lock: a waiter on another instance can be passed over by an instance that modifies the same token back to back

オープン
#7,805 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python, redis

調査の方向性

Read _RELEASE_LOCK_SCRIPT and _wait_lock in reflex/istate/manager/redis.py, then run the real-Redis reproduction test_remote_waiter_not_passed_over_by_back_to_back_modifies in tests/units/istate/manager/test_redis.py using the command in the issue. Explore a contended-path hand-off protocol that preserves the listed lock guarantees and avoids added uncontended cost. Done when the waiter gets the lock before half the loop completes deterministically, the benchmark does not regress, and mixed-version safety is addressed.

索引モデルが issue の本文から書いたものです。

説明

release-train
Summary

With StateManagerRedis, consider an instance (worker) that runs modify_state on one token in a tight loop, with no other waiter for that token on the same instance. That instance can keep retaking the token's lock before a waiter on another instance hears that the lock was released. There is no correctness problem: mutual exclusion, fencing, leases and final state are all fine. The cost is tail latency for the other instance's request.

This changed in 0.11.0a1. #7534 replaced the release's GETDEL with a compare-and-delete EVAL script (_RELEASE_LOCK_SCRIPT in reflex/istate/manager/redis.py). Redis then writes the releasing client's reply before the keyspace notification to the other instances' subscriber. So the releaser's event loop resumes first, and its next SET NX wins (the "instaque" fast path in _wait_lock). With GETDEL, the notification happened to be written first, and the waiter usually got in.

Measured impact (0.11.0a1 vs 0.10.1)
  • Real prod app (2 granian workers, real Redis 7.0, one worker running a 3000-iteration modify_state loop on a token): the worst latency of other-worker requests for that token rose from 14–24 ms to 77–165 ms. Medians are unchanged, and final counts are always exact.
  • Two StateManagerRedis instances in one event loop, one doing 1000 back-to-back modifies: the remote waiter stalls 3.8–4.4 s, versus 0.01–0.04 s on 0.10.1. Oplock on or off, redis-py 7 or 8. This setup is the deterministic worst case; real multi-process deployments see the milder number above.
Reproduction

A regression test is on the closed-out branch fix/redis-lock-fair-handoff (#7801): test_remote_waiter_not_passed_over_by_back_to_back_modifies in tests/units/istate/manager/test_redis.py. It needs a real Redis and is skipped on the mock.

  • One manager loops modify_state 200 times on a token, holding the lock about 2 ms each time.
  • After 5 iterations, a second manager (via other_state_manager()) calls modify_state on the same token.
  • The test asserts the second manager gets in before half the loop has finished.
  • On main today, the loop finishes all 200/200 iterations first.

Run it with REDIS_URL=redis://localhost:6379 uv run pytest tests/units/istate/manager/test_redis.py -k passed_over.

Why #7801 was not merged

#7801 sent a PING ahead of the release EVAL in one pipeline, which brings back 0.10.1's reply order. It fixed the latency (77–165 ms → 11–25 ms), but it was rejected for two reasons:

  • CPU cost on every event. It adds about 10–20% client-side CPU to every uncontended modify_state. On real redis-py, one release goes from about 92 µs to about 168 µs median, and from 345 to 576 Python calls. CodSpeed showed test_modify_state[redis] 13% slower. Making every event pay for a contended edge case is the wrong trade-off.
  • Fragile. It relies on Redis's undocumented reply write order, which can differ with io-threads, proxies, Valkey, KeyDB or Dragonfly.
What a fix must satisfy
  • No added cost on the uncontended path. tests/benchmarks/test_state_manager.py::test_modify_state[redis] must not regress, and an uncontended lock/release pair must keep the same number of Redis round trips.
  • No reliance on reply or notification write order between different Redis clients.
  • Keep the existing guarantees: mutual exclusion, the _instance_id fencing and lease checks, the cancellation cleanup in the release path (shielded re-send), lock expiry, and oplock behaviour, including the <lock>_waiters set.
  • A waiter on another instance gets the lock within a bounded number of hand-offs while one instance retakes it in a loop. The test above, with the bound looped_before_waiter < loops / 2 or tighter, should pass deterministically on real Redis.
  • Mixed versions during a rolling deploy: instances running the new protocol and instances running 0.11.0 against the same Redis must not deadlock or lose mutual exclusion. If the new protocol cannot coexist safely, document that.

Possible directions, none validated:

  • a per-lock waiter queue or ticket in Redis, consulted only when a waiter has registered;
  • making the releaser yield (skip the fast path) when the <lock>_waiters set (oplock) or a new waiter marker shows a remote waiter, with that check done only on the contended path.
Non-goals
  • Changing the lock's correctness semantics or its key layout for the uncontended case.
  • Fairness between waiters on the same instance; local waiters are already queued in-process.
  • Adding sleeps or fixed delays to the release or acquire path. A 3 ms gap does remove the effect, but it adds latency everywhere.
  • Pipelining extra commands into every release (the #7801 approach).

Found during reflex 0.11.0a1 pre-release testing.

主要言語
Python
スター
29k
フォーク
1.8k
平均マージ
3日 5時間
マージ済み PR(30日)
182

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

reflex-dev/reflex のほかの issue

reflex-dev/reflex の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。