converger: Wrap atomic lookup

Avoid this race. Maybe this code should be revisited with a mutex.
This commit is contained in:
James Shubin
2025-09-09 00:56:59 -04:00
parent 6c206b8010
commit f808c1ea0c

View File

@@ -34,6 +34,7 @@ import (
"fmt" "fmt"
"sort" "sort"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/purpleidea/mgmt/util" "github.com/purpleidea/mgmt/util"
@@ -130,6 +131,8 @@ func (obj *Coordinator) Register() *UID {
//id: obj.lastid, //id: obj.lastid,
//name: fmt.Sprintf("%d", obj.lastid), // some default //name: fmt.Sprintf("%d", obj.lastid), // some default
isConverged: &atomic.Bool{},
poke: obj.poke, poke: obj.poke,
// timer // timer
@@ -421,7 +424,7 @@ type UID struct {
// for per-UID timeouts too. // for per-UID timeouts too.
timeout int timeout int
// isConverged stores the convergence state of this particular UID. // isConverged stores the convergence state of this particular UID.
isConverged bool isConverged *atomic.Bool
// poke stores a reference to the main poke function. // poke stores a reference to the main poke function.
poke func() poke func()
@@ -443,14 +446,14 @@ func (obj *UID) Unregister() {
// IsConverged reports whether this UID is converged or not. // IsConverged reports whether this UID is converged or not.
func (obj *UID) IsConverged() bool { func (obj *UID) IsConverged() bool {
return obj.isConverged return obj.isConverged.Load()
} }
// SetConverged sets the convergence state of this UID. This is used by the // SetConverged sets the convergence state of this UID. This is used by the
// running timer if one is started. The timer will overwrite any value set by // running timer if one is started. The timer will overwrite any value set by
// this method. // this method.
func (obj *UID) SetConverged(isConverged bool) { func (obj *UID) SetConverged(isConverged bool) {
obj.isConverged = isConverged obj.isConverged.Store(isConverged)
obj.poke() // notify of change obj.poke() // notify of change
} }