Skip to content

Commit 4081315

Browse files
committed
Add mutex/locks to ctx reads/writes in node
1 parent b0c4e8a commit 4081315

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

node/node.go

+8-14
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (status StatusReport) TimeSinceLastRound() time.Duration {
9393

9494
// AlgorandFullNode specifies and implements a full Algorand node.
9595
type AlgorandFullNode struct {
96-
mu deadlock.RWMutex
96+
mu deadlock.Mutex
9797
ctx context.Context
9898
cancelCtx context.CancelFunc
9999
config config.Local
@@ -407,11 +407,11 @@ func (node *AlgorandFullNode) startMonitoringRoutines() {
407407

408408
// PKI TODO: Remove this with #2596
409409
// Periodically check for new participation keys
410-
go node.checkForParticipationKeys()
410+
go node.checkForParticipationKeys(node.ctx.Done())
411411

412-
go node.txPoolGaugeThread()
412+
go node.txPoolGaugeThread(node.ctx.Done())
413413
// Delete old participation keys
414-
go node.oldKeyDeletionThread()
414+
go node.oldKeyDeletionThread(node.ctx.Done())
415415

416416
// TODO re-enable with configuration flag post V1
417417
//go logging.UsageLogThread(node.ctx, node.log, 100*time.Millisecond, nil)
@@ -782,7 +782,7 @@ func ensureParticipationDB(genesisDir string, log logging.Logger) (account.Parti
782782
}
783783

784784
// Reload participation keys from disk periodically
785-
func (node *AlgorandFullNode) checkForParticipationKeys() {
785+
func (node *AlgorandFullNode) checkForParticipationKeys(done <-chan struct{}) {
786786
defer node.monitoringRoutinesWaitGroup.Done()
787787
ticker := time.NewTicker(node.config.ParticipationKeysRefreshInterval)
788788
for {
@@ -792,7 +792,7 @@ func (node *AlgorandFullNode) checkForParticipationKeys() {
792792
if err != nil {
793793
node.log.Errorf("Could not refresh participation keys: %v", err)
794794
}
795-
case <-node.ctx.Done():
795+
case <-done:
796796
ticker.Stop()
797797
return
798798
}
@@ -1032,14 +1032,11 @@ func insertStateProofToRegistry(part account.PersistedParticipation, node *Algor
10321032

10331033
var txPoolGuage = metrics.MakeGauge(metrics.MetricName{Name: "algod_tx_pool_count", Description: "current number of available transactions in pool"})
10341034

1035-
func (node *AlgorandFullNode) txPoolGaugeThread() {
1035+
func (node *AlgorandFullNode) txPoolGaugeThread(done <-chan struct{}) {
10361036
defer node.monitoringRoutinesWaitGroup.Done()
10371037
ticker := time.NewTicker(10 * time.Second)
10381038
defer ticker.Stop()
10391039
for true {
1040-
node.mu.RLock()
1041-
done := node.ctx.Done()
1042-
node.mu.RUnlock()
10431040
select {
10441041
case <-ticker.C:
10451042
txPoolGuage.Set(float64(node.transactionPool.PendingCount()), nil)
@@ -1074,12 +1071,9 @@ func (node *AlgorandFullNode) OnNewBlock(block bookkeeping.Block, delta ledgerco
10741071
// oldKeyDeletionThread keeps deleting old participation keys.
10751072
// It runs in a separate thread so that, during catchup, we
10761073
// don't have to delete key for each block we received.
1077-
func (node *AlgorandFullNode) oldKeyDeletionThread() {
1074+
func (node *AlgorandFullNode) oldKeyDeletionThread(done <-chan struct{}) {
10781075
defer node.monitoringRoutinesWaitGroup.Done()
10791076
for {
1080-
node.mu.RLock()
1081-
done := node.ctx.Done()
1082-
node.mu.RUnlock()
10831077
select {
10841078
case <-done:
10851079
return

0 commit comments

Comments
 (0)