Skip to content

Commit

Permalink
common/lru: fix race in lru (ethereum#26164)
Browse files Browse the repository at this point in the history
This fixes a problem in the SizeConstrainedLRU. The SCLRU uses an underlying simple lru which is not thread safe.
During the Get operation, the recentness of the accessed item is updated, so it is not a pure read-operation. Therefore, the mutex we need is a full mutex, not RLock.

This PR changes the mutex to be a regular Mutex, instead of RWMutex, so a reviewer can at a glance see that all affected locations are fixed.
  • Loading branch information
holiman authored Nov 11, 2022
1 parent ea65eda commit 8334b5f
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions common/lru/blob_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type SizeConstrainedLRU struct {
size uint64
maxSize uint64
lru *simplelru.LRU
lock sync.RWMutex
lock sync.Mutex
}

// NewSizeConstrainedLRU creates a new SizeConstrainedLRU.
Expand Down Expand Up @@ -78,8 +78,8 @@ func (c *SizeConstrainedLRU) Add(key common.Hash, value []byte) (evicted bool) {

// Get looks up a key's value from the cache.
func (c *SizeConstrainedLRU) Get(key common.Hash) []byte {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()

if v, ok := c.lru.Get(key); ok {
return v.([]byte)
Expand Down

0 comments on commit 8334b5f

Please sign in to comment.