Skip to content

Commit

Permalink
Add RWLocks to protect node context from data race
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Warehime committed May 3, 2022
1 parent 386a068 commit 512e87c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (status StatusReport) TimeSinceLastRound() time.Duration {

// AlgorandFullNode specifies and implements a full Algorand node.
type AlgorandFullNode struct {
mu deadlock.Mutex
mu deadlock.RWMutex
ctx context.Context
cancelCtx context.CancelFunc
config config.Local
Expand Down Expand Up @@ -786,13 +786,16 @@ func (node *AlgorandFullNode) checkForParticipationKeys() {
defer node.monitoringRoutinesWaitGroup.Done()
ticker := time.NewTicker(node.config.ParticipationKeysRefreshInterval)
for {
node.mu.RLock()
done := node.ctx.Done()
node.mu.RUnlock()
select {
case <-ticker.C:
err := node.loadParticipationKeys()
if err != nil {
node.log.Errorf("Could not refresh participation keys: %v", err)
}
case <-node.ctx.Done():
case <-done:
ticker.Stop()
return
}
Expand Down Expand Up @@ -1037,10 +1040,13 @@ func (node *AlgorandFullNode) txPoolGaugeThread() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for true {
node.mu.RLock()
done := node.ctx.Done()
node.mu.RUnlock()
select {
case <-ticker.C:
txPoolGuage.Set(float64(node.transactionPool.PendingCount()), nil)
case <-node.ctx.Done():
case <-done:
return
}
}
Expand Down Expand Up @@ -1074,8 +1080,11 @@ func (node *AlgorandFullNode) OnNewBlock(block bookkeeping.Block, delta ledgerco
func (node *AlgorandFullNode) oldKeyDeletionThread() {
defer node.monitoringRoutinesWaitGroup.Done()
for {
node.mu.RLock()
done := node.ctx.Done()
node.mu.RUnlock()
select {
case <-node.ctx.Done():
case <-done:
return
case <-node.oldKeyDeletionNotify:
}
Expand Down

0 comments on commit 512e87c

Please sign in to comment.