fix(state-manager): stop redis lock handoffs from stalling events until the lock expires
Under CPU load, our app's events stalled for 5 or 10 seconds (the oplock hold time and the lock expiration), and some updates made under a lease were lost. StateManagerRedis had several ways to lose a lock or a wakeup:
- A modify cancelled while taking the lock left the lock behind until it expired.
supersedesand a newon_load_internalcancel events at any await. This covers aSET NXwhose reply never arrived, the awaits in_try_modify_statebefore a lease or the single update owns the lock, and a lease task cancelled before its first step, which never enters the context that releases the lock. - The release was a
GETDEL, so a holder releasing after its lock expired deleted the next holder's lock. - A lease never extended its lock while events kept using the cached state. When they outlasted the lock expiration, the flush failed with
LockExpiredErrorand dropped every change made under the lease. - A queued waiter only learns about a lease from the holder ahead of it, which skipped the notification when it raised or its lock had expired, and a waiter cancelled after its wakeup took the wakeup with it. The other waiters then sat out the whole lock expiration.
This PR:
- releases the lock with a compare-and-delete script, also when the acquisition is cancelled, and runs a release a cancellation interrupted again
- closes the lock context on every path that does not hand it to a lease, and hands it over with
AsyncExitStack.pop_all()once the state is fetched - flushes and releases a lease that was cancelled before it started
- gives a lease a full expiration when it starts, and while the flush waits for and writes the cached state, renews the lock as long as the lease still owns it, giving each use of the cached state the lock expiration from when it got it, so a use that runs over it still ends in
LockExpiredError - wakes the next waiter in all of those cases, and lets waiters re-check the lock every tenth of its expiration, so a missed notification costs at most that
Each case has a test in tests/units/istate/manager/test_redis.py that fails without the fix. test_oplock_immediate_cancel now checks the outcome, because its lease can be cancelled before it starts.
With a script that cancels a waiter around the handoff (2 s lock expiration, 50 ms redis latency), 19 of 30 waiters waited out the expiration before and none of 60 after. A fuzzer with up to 2 s holders lost up to 23 of 26 increments per round before and none after.
The unit tests pass (12512 without Redis, 12525 against Redis with and without oplock).