Redis state lock: a waiter on another instance can be passed over by an instance that modifies the same token back to back
メンテナーはふだん 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 の本文から書いたものです。
説明
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_stateloop 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
StateManagerRedisinstances 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_state200 times on a token, holding the lock about 2 ms each time. - After 5 iterations, a second manager (via
other_state_manager()) callsmodify_stateon the same token. - The test asserts the second manager gets in before half the loop has finished.
- On
maintoday, 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 showedtest_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_idfencing and lease checks, the cancellation cleanup in the release path (shielded re-send), lock expiry, and oplock behaviour, including the<lock>_waitersset. - 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 / 2or 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>_waitersset (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
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
reflex-dev/reflex のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
reflex-dev/reflex#8026 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Kept env.py failure: the "move it aside" hint is dropped and a full traceback is logged when alembic.ini uses a relative script_location対応中かも @MaiAditya が 13 日前に担当しました。 オープンbug release-train
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
reflex-dev/reflex#7856 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
0.9.12 changelog follow-ups: the #7132 "_get_was_touched" entry describes unreachable behaviour, #7115's hasattr/getattr change is undocumented, #7131's wording再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンrelease-train
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
reflex-dev/reflex#7260 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
ClientStateVar: a component that mounts while the value is non-default never re-renders on the push back to the default再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
reflex-dev/reflex#6823 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
documentation upstream
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
reflex-dev/reflex#6436 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
reflex-dev/reflex の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
superlinked/sie#1033 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
apache/ponymail-foal#365 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
NousResearch/hermes-labs-plugins#116 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
gcivil-nyu-org/team-3-fall26#192 ·
メンテナーはふだん 1 日以内に返信
-
[Study plan] Study sessions can be rescheduled to any length (e.g. 72 hours) with no length checkオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
gcivil-nyu-org/team-2-fall26#159 ·
メンテナーはふだん 1 日以内に返信