Files
Pulse/internal/alerts/config/clone.go
rcourtman ad1e34277d Stop alert config saves writing into the live config
SaveAlertConfig normalized the config it was handed in place, and its
callers hand it alerts.Manager.GetConfig(), a shallow copy whose maps
and threshold pointers were the running config's own. Alert activation
and the alert identity migration therefore wrote the live delay map and
threshold pointers outside the manager lock while evaluation read them.
The shallow copy also let UpdateConfig, which normalizes the override
map it is handed in place, rewrite the live overrides while
GET /api/alerts/config encoded another snapshot. A concurrent map read
or iteration plus a write is a fatal Go runtime error, so these paths
could kill Pulse.

AlertConfig.Clone is a typed deep copy. GetConfig returns one, so
callers own what they encode, persist and edit, and SaveAlertConfig
clones before normalizing, because a config just applied with
UpdateConfig is the live config. GetConfig has no per-resource caller
(once per poll cycle, backup check, request or Patrol run); a clone
with 1,000 overrides takes about 0.3 ms. A test fills every field and
fails when a new map, slice or pointer is not copied.

Persistence also stored and loaded an explicit 0 type-level alert delay
("no delay") as the 5-second factory delay, which the noisy-gauge rule
stretches to 300 seconds for memory and temperature, while the manager
keeps 0. The shared map write was what made the running manager agree
with the file; without it, memory would say 0 and disk 5. Persistence
now keeps 0 on save and load like NormalizeTimeThresholds, and only a
missing or negative delay takes the default. Pulse saves never wrote a
0, and the first v6 start re-saves alerts.json, so only a newly sent or
hand-edited 0 changes meaning.

On main, a reproducer taking the activation, migration and GET steps
against live evaluation died with "fatal error: concurrent map
iteration and map write" in 5 of 5 plain runs and reported 16 races
under -race. It passes with this change.
2026-10-07 16:57:57 +01:00

165 lines
5.6 KiB
Go

package config
import (
"maps"
"slices"
)
// Clone returns a deep copy of the configuration that shares no maps, slices
// or pointers with c, so the copy can be changed, normalized or encoded
// without touching c. Nil and empty maps and slices keep their distinction.
// Custom rule filter values are copied as the JSON values the API and
// alerts.json decode them to; other Go values stored there stay shared.
//
// The alert manager hands out its live configuration only through Clone;
// callers persist, encode and edit what it returns outside the manager lock.
func (c AlertConfig) Clone() AlertConfig {
clone := c
clone.ActivationTime = clonePointer(c.ActivationTime)
clone.GuestDefaults = c.GuestDefaults.Clone()
clone.NodeDefaults = c.NodeDefaults.Clone()
clone.AgentDefaults = c.AgentDefaults.Clone()
clone.DiskFillByType = maps.Clone(c.DiskFillByType)
clone.DiskTempByType = maps.Clone(c.DiskTempByType)
clone.DockerIgnoredContainerPrefixes = slices.Clone(c.DockerIgnoredContainerPrefixes)
clone.IgnoredGuestPrefixes = slices.Clone(c.IgnoredGuestPrefixes)
clone.GuestTagWhitelist = slices.Clone(c.GuestTagWhitelist)
clone.GuestTagBlacklist = slices.Clone(c.GuestTagBlacklist)
clone.PBSDefaults = c.PBSDefaults.Clone()
clone.KubernetesDefaults = c.KubernetesDefaults.Clone()
clone.TrueNASDefaults = c.TrueNASDefaults.Clone()
clone.TrueNASDiskDefaults = c.TrueNASDiskDefaults.Clone()
clone.VMwareDefaults = c.VMwareDefaults.Clone()
clone.BackupDefaults = c.BackupDefaults.clone()
if c.Overrides != nil {
clone.Overrides = make(map[string]ThresholdConfig, len(c.Overrides))
for id, override := range c.Overrides {
clone.Overrides[id] = override.Clone()
}
}
if c.CustomRules != nil {
clone.CustomRules = make([]CustomAlertRule, len(c.CustomRules))
for i, rule := range c.CustomRules {
clone.CustomRules[i] = rule.clone()
}
}
clone.Schedule.QuietHours.Days = maps.Clone(c.Schedule.QuietHours.Days)
if c.Schedule.Escalation.Levels != nil {
clone.Schedule.Escalation.Levels = make([]EscalationLevel, len(c.Schedule.Escalation.Levels))
for i, level := range c.Schedule.Escalation.Levels {
level.DestinationIDs = slices.Clone(level.DestinationIDs)
clone.Schedule.Escalation.Levels[i] = level
}
}
clone.TimeThresholds = maps.Clone(c.TimeThresholds)
clone.MetricTimeThresholds = cloneNestedIntMap(c.MetricTimeThresholds)
clone.MetricEvaluationWindows = cloneNestedIntMap(c.MetricEvaluationWindows)
return clone
}
// Clone returns a deep copy of the thresholds that shares no pointers with t.
func (t ThresholdConfig) Clone() ThresholdConfig {
clone := t
clone.CPU = clonePointer(t.CPU)
clone.Memory = clonePointer(t.Memory)
clone.Disk = clonePointer(t.Disk)
clone.DiskRead = clonePointer(t.DiskRead)
clone.DiskWrite = clonePointer(t.DiskWrite)
clone.NetworkIn = clonePointer(t.NetworkIn)
clone.NetworkOut = clonePointer(t.NetworkOut)
clone.Usage = clonePointer(t.Usage)
clone.Temperature = clonePointer(t.Temperature)
clone.DiskTemperature = clonePointer(t.DiskTemperature)
clone.SMARTHealthFailure = clonePointer(t.SMARTHealthFailure)
clone.SMARTReallocated = clonePointer(t.SMARTReallocated)
clone.SMARTPending = clonePointer(t.SMARTPending)
clone.SMARTUncorrectable = clonePointer(t.SMARTUncorrectable)
clone.SMARTMediaErrors = clonePointer(t.SMARTMediaErrors)
clone.SMARTCRCErrorDelta = clonePointer(t.SMARTCRCErrorDelta)
clone.SMARTLifeWarning = clonePointer(t.SMARTLifeWarning)
clone.SMARTLifeCritical = clonePointer(t.SMARTLifeCritical)
clone.SMARTSpareWarning = clonePointer(t.SMARTSpareWarning)
clone.SMARTSpareCritical = clonePointer(t.SMARTSpareCritical)
if t.Backup != nil {
backup := t.Backup.clone()
clone.Backup = &backup
}
clone.Snapshot = clonePointer(t.Snapshot)
clone.Note = clonePointer(t.Note)
return clone
}
func (b BackupAlertConfig) clone() BackupAlertConfig {
clone := b
clone.AlertOrphaned = clonePointer(b.AlertOrphaned)
clone.IgnoreVMIDs = slices.Clone(b.IgnoreVMIDs)
return clone
}
func (r CustomAlertRule) clone() CustomAlertRule {
clone := r
if r.FilterConditions.Filters != nil {
clone.FilterConditions.Filters = make([]FilterCondition, len(r.FilterConditions.Filters))
for i, filter := range r.FilterConditions.Filters {
filter.Value = cloneJSONValue(filter.Value)
clone.FilterConditions.Filters[i] = filter
}
}
clone.Thresholds = r.Thresholds.Clone()
if r.Notifications.Email != nil {
email := *r.Notifications.Email
email.Recipients = slices.Clone(email.Recipients)
clone.Notifications.Email = &email
}
clone.Notifications.Webhook = clonePointer(r.Notifications.Webhook)
return clone
}
// clonePointer copies the value p points at. Only use it for types that hold
// no maps, slices or pointers of their own.
func clonePointer[T any](p *T) *T {
if p == nil {
return nil
}
clone := *p
return &clone
}
func cloneNestedIntMap(input map[string]map[string]int) map[string]map[string]int {
if input == nil {
return nil
}
clone := make(map[string]map[string]int, len(input))
for key, inner := range input {
clone[key] = maps.Clone(inner)
}
return clone
}
// cloneJSONValue copies a filter value as encoding/json decodes one: arrays
// and objects are copied, scalars are values already.
func cloneJSONValue(value interface{}) interface{} {
switch typed := value.(type) {
case []interface{}:
if typed == nil {
return typed
}
clone := make([]interface{}, len(typed))
for i, item := range typed {
clone[i] = cloneJSONValue(item)
}
return clone
case map[string]interface{}:
if typed == nil {
return typed
}
clone := make(map[string]interface{}, len(typed))
for key, item := range typed {
clone[key] = cloneJSONValue(item)
}
return clone
default:
return value
}
}