Redis state lock: a waiter on another instance can be passed over by an instance that modifies the same token back to back
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 25/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Activo
- Stack tecnológico
- python, redis
- Área
- backend, distributed-systems
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 29k
- Forks
- 1.8k
- Merge medio
- 3 d 5 h
- PR fusionados (30 d)
- 182
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de reflex-dev/reflex
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 78/100
reflex-dev/reflex#8026 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Kept env.py failure: the "move it aside" hint is dropped and a full traceback is logged when alembic.ini uses a relative script_locationPosiblemente ocupada @MaiAditya la tomó hace 13 días. Abiertobug release-train
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
reflex-dev/reflex#7856 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
0.9.12 changelog follow-ups: the #7132 "_get_was_touched" entry describes unreachable behaviour, #7115's hasattr/getattr change is undocumented, #7131's wordingQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abiertorelease-train
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
reflex-dev/reflex#7260 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
ClientStateVar: a component that mounts while the value is non-default never re-renders on the push back to the defaultQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
reflex-dev/reflex#6823 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
documentation upstream
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
reflex-dev/reflex#6436 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de reflex-dev/reflex
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 86/100
superlinked/sie#1033 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
apache/ponymail-foal#365 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 79/100
NousResearch/hermes-labs-plugins#116 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
gcivil-nyu-org/team-3-fall26#192 ·
Los mantenedores suelen responder en 1 día
-
[Study plan] Study sessions can be rescheduled to any length (e.g. 72 hours) with no length checkAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
gcivil-nyu-org/team-2-fall26#159 ·
Los mantenedores suelen responder en 1 día