Repository navigation
Description
Describe the bug
Two defects in the ingress persistence path, same area as #4806.
1. storeIngress / storeIngressState return nil after losing the optimistic lock 5 times
Both retry a WATCH/MULTI/EXEC transaction maxRetries (5) times on redis.TxFailedErr, then fall through to return nil:
livekit/pkg/service/redisstore.go
Lines 604 to 617 in 704c328
| // Retry if the key has been changed. | |
| for range maxRetries { | |
| err := s.rc.Watch(s.ctx, txf, IngressKey) | |
| switch err { | |
| case redis.TxFailedErr: | |
| // Optimistic lock lost. Retry. | |
| continue | |
| default: | |
| return err | |
| } | |
| } | |
| return nil | |
| } |
livekit/pkg/service/redisstore.go
Lines 679 to 692 in 704c328
| // Retry if the key has been changed. | |
| for range maxRetries { | |
| err := s.rc.Watch(s.ctx, txf, IngressStatePrefix+ingressId) | |
| switch err { | |
| case redis.TxFailedErr: | |
| // Optimistic lock lost. Retry. | |
| continue | |
| default: | |
| return err | |
| } | |
| } | |
| return nil | |
| } |
A lost update is reported as success:
CreateIngressreturns anIngressInfothat is not in theingresshash and whose stream key is not registered.StoreIngresscontinues tostoreIngressState, leaving an orphan{ingress}_state:<id>key with no TTL (L661) thatDeleteIngresscan never reach.UpdateIngressState/UpdateIngressdrop the write without a log line.
storeIngress watches the whole ingress hash, so any ingress create/update on any node between WATCH and EXEC invalidates the attempt. A burst of short-lived ingresses (the workload in #4806) is enough.
2. UpdateIngress / DeleteIngress panic on an ingress without a state key
LoadIngress returns the info with State == nil when the state key is missing (L741-L743). #4806 guarded IOInfoService.UpdateIngressState; the two Twirp handlers still dereference info.State.Status:
livekit/pkg/service/ingress.go
Line 318 in 704c328
| switch info.State.Status { |
livekit/pkg/service/ingress.go
Line 401 in 704c328
| switch info.State.Status { |
The panic is recovered by negroni.NewRecovery(), so the node stays up, but the request fails with 500 and the ingress can never be updated or deleted through the API. Such an ingress is produced by the #4806 race, or by (1) when the state write is the one that fails after the info landed.
Server
- master
704c3280; the lines date from 2022, so every release including v1.13.10. - Local dev, single node, Redis 7.
To Reproduce
Regression tests from the fix branch, run on master:
TestIngressStoreReportsLostOptimisticLock: a go-redis hook writes to the watched key from a second connection after everyWATCH, so all five attempts fail withTxFailedErrdeterministically.StoreIngressandUpdateIngressStatereturn nil; afterwardsHGET ingress <id>is nil andEXISTS {ingress}_state:<id>is 1 withTTL-1.TestIngressAPIWithNilState: a fake store returns an info with nilState.UpdateIngresspanics atingress.go:318,DeleteIngressat:401.
The hook forces the worst case; in production the trigger is contention on the ingress hash. The defect itself is deterministic once the retries are exhausted.
Expected behavior
- The store returns an error when the write did not land, so
CreateIngressfails instead of returning a phantom ingress, and no orphan state key is written. UpdateIngress/DeleteIngresstreat a missing state as empty, asIOInfoService.UpdateIngressStatedoes since Fix nil pointer dereference in IOInfoService.UpdateIngressState on concurrent DeleteIngress #4806.
Fix is 2 lines plus the #4806 guard in two places; PR with both tests follows.
Activity