Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/fileservice/fifocache/data_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (d *DataCache) deletePath(ctx context.Context, shardIndex int, path string)
for key, item := range shard.values {
if key.Path == path {
delete(shard.values, key)
// key deleted, call postEvict
if d.fifo.postEvict != nil {
d.fifo.postEvict(ctx, item.key, item.value, item.size)
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/fileservice/fifocache/fifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func (c *Cache[K, V]) Delete(ctx context.Context, key K) {
return
}
delete(shard.values, key)
// key deleted, call postEvict
if c.postEvict != nil {
c.postEvict(ctx, item.key, item.value, item.size)
}
Expand Down Expand Up @@ -290,9 +291,12 @@ func (c *Cache[K, V]) deleteItem(ctx context.Context, item *_CacheItem[K, V]) {
shard := &c.shards[c.keyShardFunc(item.key)%numShards]
shard.Lock()
defer shard.Unlock()
delete(shard.values, item.key)
if c.postEvict != nil {
c.postEvict(ctx, item.key, item.value, item.size)
if _, ok := shard.values[item.key]; ok {
delete(shard.values, item.key)
// key deleted, call postEvict
if c.postEvict != nil {
c.postEvict(ctx, item.key, item.value, item.size)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/fileservice/fifocache/fifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,23 @@ func TestCacheEvict3(t *testing.T) {
assert.Equal(t, 2048, nSet)
assert.Equal(t, 2048, nGet)
}

func TestDoubleFree(t *testing.T) {
evicts := make(map[int]int)
cache := New(
fscache.ConstCapacity(1),
ShardInt,
nil, nil,
func(ctx context.Context, key int, value int, size int64) {
evicts[key]++
},
)
// set
cache.Set(t.Context(), 1, 1, 1)
// delete, item still in queue
cache.Delete(t.Context(), 1)
// set to evict 1
cache.Set(t.Context(), 2, 2, 1)
// check
assert.Equal(t, 1, evicts[1])
}
Loading