Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Guard the resolved-alert maps on the canonical eval paths
rcourtman8ace17dde1b6
diff --git a/internal/alerts/canonical_lifecycle.go b/internal/alerts/canonical_lifecycle.goindex 301fe6e028..4590c0bb95 100644--- a/internal/alerts/canonical_lifecycle.go+++ b/internal/alerts/canonical_lifecycle.go@@ -410,25 +410,35 @@ func (m *Manager) evaluateCanonicalLifecycleAlert(params canonicalLifecycleAlertreturn result, true}+ // The resolved maps are resolvedMutex-guarded; take it for the+ // lookup/removal only and keep history/dispatch outside the lock.+ var reactivatedAt time.Time+ reactivated := false+ m.resolvedMutex.Lock()if resolved, ok := m.getResolvedAlertNoLock(storageKey); ok && resolved != nil && resolved.Alert != nil {if resolved.ResolvedTime.After(time.Now().Add(-5 * time.Minute)) {if !resolved.Alert.StartTime.IsZero() {alert.StartTime = resolved.Alert.StartTime}m.removeResolvedAlertUnlocked(storageKey)- if params.AddToHistory {- m.historyManager.UpdateAlertLastSeenForAlert(alert, alert.LastSeen)- }- log.Debug().- Str("alertID", storageKey).- Time("resolvedAt", resolved.ResolvedTime).- Msg("Alert re-fired within cooldown, reactivated without new history entry")- if params.RateLimit && !m.checkRateLimit(trackingKey) {- return result, true- }- m.dispatchAlert(alert, params.DispatchAsync)+ reactivated = true+ reactivatedAt = resolved.ResolvedTime+ }+ }+ m.resolvedMutex.Unlock()+ if reactivated {+ if params.AddToHistory {+ m.historyManager.UpdateAlertLastSeenForAlert(alert, alert.LastSeen)+ }+ log.Debug().+ Str("alertID", storageKey).+ Time("resolvedAt", reactivatedAt).+ Msg("Alert re-fired within cooldown, reactivated without new history entry")+ if params.RateLimit && !m.checkRateLimit(trackingKey) {return result, true}+ m.dispatchAlert(alert, params.DispatchAsync)+ return result, true}if params.AddToHistory {@@ -562,25 +572,35 @@ func (m *Manager) evaluateCanonicalStatefulAlert(params canonicalStatefulAlertPa}if existing == nil {+ // The resolved maps are resolvedMutex-guarded; take it for the+ // lookup/removal only and keep history/dispatch outside the lock.+ var reactivatedAt time.Time+ reactivated := false+ m.resolvedMutex.Lock()if resolved, ok := m.getResolvedAlertNoLock(storageKey); ok && resolved != nil && resolved.Alert != nil {if resolved.ResolvedTime.After(time.Now().Add(-5 * time.Minute)) {if !resolved.Alert.StartTime.IsZero() {alert.StartTime = resolved.Alert.StartTime}m.removeResolvedAlertUnlocked(storageKey)- if params.AddToHistory {- m.historyManager.UpdateAlertLastSeenForAlert(alert, alert.LastSeen)- }- log.Debug().- Str("alertID", storageKey).- Time("resolvedAt", resolved.ResolvedTime).- Msg("Stateful alert re-fired within cooldown, reactivated without new history entry")- if params.RateLimit && !m.checkRateLimit(trackingKey) {- return result, true- }- m.dispatchAlert(alert, params.DispatchAsync)+ reactivated = true+ reactivatedAt = resolved.ResolvedTime+ }+ }+ m.resolvedMutex.Unlock()+ if reactivated {+ if params.AddToHistory {+ m.historyManager.UpdateAlertLastSeenForAlert(alert, alert.LastSeen)+ }+ log.Debug().+ Str("alertID", storageKey).+ Time("resolvedAt", reactivatedAt).+ Msg("Stateful alert re-fired within cooldown, reactivated without new history entry")+ if params.RateLimit && !m.checkRateLimit(trackingKey) {return result, true}+ m.dispatchAlert(alert, params.DispatchAsync)+ return result, true}if params.AddToHistory {diff --git a/internal/alerts/manager.go b/internal/alerts/manager.goindex 95f6bc082d..4d62893afa 100644--- a/internal/alerts/manager.go+++ b/internal/alerts/manager.go@@ -15,13 +15,17 @@ import (// Lock Ordering Documentation:// The Manager uses two mutexes to prevent deadlocks:// 1. m.mu (primary lock) - protects most manager state-// 2. m.resolvedMutex - protects only recentlyResolved map+// 2. m.resolvedMutex - protects only the recentlyResolved and resolvedAlias maps//// Lock Ordering Rules:-// - NEVER hold m.mu when acquiring resolvedMutex-// - ALWAYS release m.mu before acquiring resolvedMutex-// - resolvedMutex can be held independently without m.mu-// - When both locks are needed, acquire m.mu first, then release it before acquiring resolvedMutex+// - resolvedMutex is subordinate to m.mu: it MAY be acquired while holding+// m.mu (the cleanup and canonical-eval paths do), but NEVER acquire m.mu+// while holding resolvedMutex+// - keep resolvedMutex critical sections to map access only; never call+// dispatch, history, or notification code while holding it+// - every access to recentlyResolved or resolvedAlias must hold+// resolvedMutex, and writers need the write lock: getResolvedAlertNoLock+// can backfill resolvedAlias, so even lookups are potential writes//// This ordering prevents deadlock scenarios where different goroutines acquire locks in different orders.type Manager struct {diff --git a/internal/alerts/read_model.go b/internal/alerts/read_model.goindex a5e1a09269..9816fc7e19 100644--- a/internal/alerts/read_model.go+++ b/internal/alerts/read_model.go@@ -294,8 +294,11 @@ func (m *Manager) GetRecentlyResolved() []models.ResolvedAlert {// GetResolvedAlert returns a copy of a recently resolved alert by ID.func (m *Manager) GetResolvedAlert(alertID string) *ResolvedAlert {- m.resolvedMutex.RLock()- defer m.resolvedMutex.RUnlock()+ // Write lock: getResolvedAlertNoLock backfills the resolvedAlias map on a+ // canonical-identity miss, so concurrent read-locked callers would race+ // each other on that write.+ m.resolvedMutex.Lock()+ defer m.resolvedMutex.Unlock()resolved, ok := m.getResolvedAlertNoLock(alertID)if !ok || resolved == nil || resolved.Alert == nil {diff --git a/internal/alerts/resolved_lock_discipline_test.go b/internal/alerts/resolved_lock_discipline_test.gonew file mode 100644index 0000000000..9d95a5b0f3--- /dev/null+++ b/internal/alerts/resolved_lock_discipline_test.go@@ -0,0 +1,82 @@+package alerts++import (+ "fmt"+ "sync"+ "testing"+ "time"+)++// Regression for issue #1590. The canonical alert evaluation paths used to+// read and mutate recentlyResolved/resolvedAlias while holding only m.mu,+// while the broadcast and recovery paths guarded them with resolvedMutex, so+// the two lock domains did not exclude each other and Go's runtime aborted+// the process with a concurrent map access fault on flapping resources. Run+// with -race: this exercises the cooldown-reactivation lookup pattern against+// the broadcaster and the recovery writer concurrently.+func TestRecentlyResolvedConcurrentAccessIsRaceFree(t *testing.T) {+ m := NewManagerWithDataDir(t.TempDir())++ mk := func(id string) *ResolvedAlert {+ return &ResolvedAlert{+ Alert: &Alert{ID: id, ResourceID: id, Type: "connectivity"},+ ResolvedTime: time.Now(),+ }+ }++ stop := make(chan struct{})+ var wg sync.WaitGroup+ wg.Add(3)++ // Recovery path: writes resolved alerts under resolvedMutex.+ go func() {+ defer wg.Done()+ for i := 0; ; i++ {+ select {+ case <-stop:+ return+ default:+ }+ m.addRecentlyResolvedUnlocked(mk(fmt.Sprintf("res-%d", i%8)))+ }+ }()++ // Broadcast path: iterates the resolved map every poll cycle.+ go func() {+ defer wg.Done()+ for {+ select {+ case <-stop:+ return+ default:+ }+ m.GetRecentlyResolved()+ m.GetResolvedAlert("res-1")+ }+ }()++ // Canonical eval path: cooldown lookup and removal while holding m.mu,+ // with the resolved maps guarded by the subordinate resolvedMutex.+ go func() {+ defer wg.Done()+ for i := 0; ; i++ {+ select {+ case <-stop:+ return+ default:+ }+ key := fmt.Sprintf("res-%d", i%8)+ m.mu.Lock()+ m.resolvedMutex.Lock()+ if resolved, ok := m.getResolvedAlertNoLock(key); ok && resolved != nil {+ m.removeResolvedAlertUnlocked(key)+ }+ m.resolvedMutex.Unlock()+ m.mu.Unlock()+ }+ }()++ time.Sleep(500 * time.Millisecond)+ close(stop)+ wg.Wait()+}