R74: recv handler panics, cache TTL race fix

This commit is contained in:
2026-08-13 17:35:25 +01:00
parent ea37443ee0
commit 5a54960cdd
4 changed files with 114 additions and 15 deletions
+21 -15
View File
@@ -33,13 +33,14 @@ func newCacheShard(maxSize, maxMemory int64) *cacheShard {
func (s *cacheShard) get(key string) (interface{}, bool, bool) {
s.mu.RLock()
item, exists := s.items[key]
expired := exists && item.isExpired()
s.mu.RUnlock()
if !exists {
return nil, false, false
}
if item.isExpired() {
if expired {
return nil, true, true // exists but expired
}
@@ -47,16 +48,19 @@ func (s *cacheShard) get(key string) (interface{}, bool, bool) {
s.mu.Lock()
// Re-check item exists (could have been deleted)
item, exists = s.items[key]
if exists && !item.isExpired() {
item.accessedAt = time.Now()
item.accessCount++
if elem, ok := item.element.(*list.Element); ok && elem != nil {
s.lruList.MoveToFront(elem)
if exists {
expired = item.isExpired()
if !expired {
item.accessedAt = time.Now()
item.accessCount++
if elem, ok := item.element.(*list.Element); ok && elem != nil {
s.lruList.MoveToFront(elem)
}
}
}
s.mu.Unlock()
if !exists || item.isExpired() {
if !exists || expired {
return nil, false, false
}
@@ -139,30 +143,32 @@ func (s *cacheShard) deleteIfExpired(key string) bool {
func (s *cacheShard) exists(key string) bool {
s.mu.RLock()
item, exists := s.items[key]
expired := exists && item.isExpired()
s.mu.RUnlock()
if !exists {
return false
}
return !item.isExpired()
return exists && !expired
}
// ttl returns the remaining TTL for a key
func (s *cacheShard) ttl(key string) (time.Duration, bool) {
s.mu.RLock()
item, exists := s.items[key]
expired := exists && item.isExpired()
var expiresAt time.Time
if exists {
expiresAt = item.expiresAt
}
s.mu.RUnlock()
if !exists || item.isExpired() {
if !exists || expired {
return 0, false
}
if item.expiresAt.IsZero() {
if expiresAt.IsZero() {
return 0, true // No expiration
}
remaining := time.Until(item.expiresAt)
remaining := time.Until(expiresAt)
if remaining < 0 {
return 0, false
}
+50
View File
@@ -0,0 +1,50 @@
package backends
import (
"context"
"sync"
"testing"
"time"
)
// TestCache_TTLReadUnderConcurrentExpire exercises get/exists/ttl fast
// paths racing with an in-place Expire that mutates expiresAt. Before
// the fix the fast paths read expiresAt outside the lock, so under -race
// this reports a data race on memoryCacheItem.expiresAt.
func TestCache_TTLReadUnderConcurrentExpire(t *testing.T) {
c := NewMemoryCacheBackend(10000, 1024*1024*64, 0)
defer c.Close()
ctx := context.Background()
if err := c.Set(ctx, "k", "v", time.Hour); err != nil {
t.Fatalf("set: %v", err)
}
stop := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-stop:
return
default:
}
_, _ = c.Get(ctx, "k")
_, _ = c.Exists(ctx, "k")
_, _ = c.TTL(ctx, "k")
}
}()
}
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 2000; i++ {
_ = c.Expire(ctx, "k", time.Hour)
}
close(stop)
}()
wg.Wait()
}