RedisState's compare-and-swap watches through its pipeline, so a write between the compare and EXEC fails the swap; pause_intake waits out a claim already under way, so nothing dispatches after it returns

This commit is contained in:
2026-11-04 21:53:41 +01:00
parent d028bf326a
commit 1a622edb1f
4 changed files with 74 additions and 32 deletions
+24 -28
View File
@@ -834,35 +834,31 @@ class RedisState(StateBackend):
watch_keys = [self._key(k) for k in expected.keys()]
try:
# Start watching the keys for changes
self._client.watch(*watch_keys)
# WATCH has to go through the pipeline: it pins one connection, and
# the client-level call is a no-op in redis-py 7. Leaving the block
# resets the pipeline, which unwatches.
with self._client.pipeline() as pipe:
try:
pipe.watch(*watch_keys) # type: ignore[no-untyped-call]
# A watching pipeline runs commands immediately.
current = cast(list[bytes | None], pipe.mget(watch_keys))
for raw, exp_value in zip(current, expected.values(), strict=True):
if self._deserialize(raw) != exp_value:
return False
# Check current values
current = self.get_multi(list(expected.keys()))
for key, exp_value in expected.items():
if current.get(key) != exp_value:
self._client.unwatch()
return False
# All values match - execute the transaction
pipe = self._client.pipeline(True) # True = use MULTI/EXEC
for key, value in updates.items():
data = self._serialize(value)
if self._ttl:
pipe.setex(self._key(key), self._ttl, data)
else:
pipe.set(self._key(key), data)
self._note(pipe, updates)
pipe.execute()
return True
except redis.WatchError:
# Another client modified one of the watched keys
return False
pipe.multi()
for key, value in updates.items():
data = self._serialize(value)
if self._ttl:
pipe.setex(self._key(key), self._ttl, data)
else:
pipe.set(self._key(key), data)
self._note(pipe, updates)
pipe.execute()
return True
except redis.WatchError:
# Another client modified one of the watched keys
return False
def close(self) -> None:
"""Give the connection pool back."""