Skip to content

Commit 6c3476e

Browse files
holimanshekhirin
authored andcommitted
common/lru: fix race in lru (ethereum#26164)
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.
1 parent 4ac1a54 commit 6c3476e

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

common/lru/blob_lru.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type SizeConstrainedLRU struct {
3333
size uint64
3434
maxSize uint64
3535
lru *simplelru.LRU
36-
lock sync.RWMutex
36+
lock sync.Mutex
3737
}
3838

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

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

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

0 commit comments

Comments
 (0)