Skip to content

Commit 684face

Browse files
authored
light: fix disableCheckFreq locking (#18515)
This change unbreaks the build and removes racy access to disableCheckFreq. Even though the field is set while holding the lock, it was read outside of the protected section.
1 parent 0a45455 commit 684face

1 file changed

Lines changed: 13 additions & 19 deletions

File tree

‎light/lightchain.go‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
"github.com/ethereum/go-ethereum/log"
3636
"github.com/ethereum/go-ethereum/params"
3737
"github.com/ethereum/go-ethereum/rlp"
38-
"github.com/hashicorp/golang-lru"
38+
lru "github.com/hashicorp/golang-lru"
3939
)
4040

4141
var (
@@ -50,28 +50,26 @@ type LightChain struct {
5050
hc *core.HeaderChain
5151
indexerConfig *IndexerConfig
5252
chainDb ethdb.Database
53+
engine consensus.Engine
5354
odr OdrBackend
5455
chainFeed event.Feed
5556
chainSideFeed event.Feed
5657
chainHeadFeed event.Feed
5758
scope event.SubscriptionScope
5859
genesisBlock *types.Block
5960

60-
chainmu sync.RWMutex
61-
6261
bodyCache *lru.Cache // Cache for the most recent block bodies
6362
bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format
6463
blockCache *lru.Cache // Cache for the most recent entire blocks
6564

65+
chainmu sync.RWMutex // protects header inserts
6666
quit chan struct{}
67-
running int32 // running must be called automically
68-
// procInterrupt must be atomically called
69-
procInterrupt int32 // interrupt signaler for block processing
70-
wg sync.WaitGroup
71-
72-
engine consensus.Engine
67+
wg sync.WaitGroup
7368

74-
disableCheckFreq bool
69+
// Atomic boolean switches:
70+
running int32 // whether LightChain is running or stopped
71+
procInterrupt int32 // interrupts chain insert
72+
disableCheckFreq int32 // disables header verification
7573
}
7674

7775
// NewLightChain returns a fully initialised light chain using information
@@ -356,7 +354,7 @@ func (self *LightChain) postChainEvents(events []interface{}) {
356354
// In the case of a light chain, InsertHeaderChain also creates and posts light
357355
// chain events when necessary.
358356
func (self *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
359-
if self.disableCheckFreq {
357+
if atomic.LoadInt32(&self.disableCheckFreq) == 1 {
360358
checkFreq = 0
361359
}
362360
start := time.Now()
@@ -532,16 +530,12 @@ func (self *LightChain) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEven
532530
return self.scope.Track(new(event.Feed).Subscribe(ch))
533531
}
534532

535-
//DisableCheckFreq disables header validation. It needs for ULC
533+
// DisableCheckFreq disables header validation. This is used for ultralight mode.
536534
func (self *LightChain) DisableCheckFreq() {
537-
self.mu.Lock()
538-
defer self.mu.Unlock()
539-
self.disableCheckFreq = true
535+
atomic.StoreInt32(&self.disableCheckFreq, 1)
540536
}
541537

542-
//EnableCheckFreq enables header validation
538+
// EnableCheckFreq enables header validation.
543539
func (self *LightChain) EnableCheckFreq() {
544-
self.mu.Lock()
545-
defer self.mu.Unlock()
546-
self.disableCheckFreq = false
540+
atomic.StoreInt32(&self.disableCheckFreq, 0)
547541
}

0 commit comments

Comments
 (0)