Skip to content

armgit / Pulse

Public Imported from GitHub
0

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.

Fix guest metadata race in release backend

rcourtmancf6585310881
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index e3cb5c39d3..9a7802d8b1 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -1269,6 +1269,11 @@ shared guest metadata cache must keep VM network and identity metadata alive
long enough to survive short Proxmox status failures, while incomplete
guest-agent metadata stays on a short retry cadence instead of freezing
partial VM summary data for minutes.
+The persisted guest metadata store must also remain the synchronization
+boundary for last-known guest identity updates. Store reads and writes must
+copy metadata, including slice fields, so asynchronous monitor persistence
+cannot expose mutable store pointers to caller goroutines or race with
+release-pipeline `-race` backend proofs.
When Proxmox reports saturated VM memory without `meminfo` or `freemem` but
the QEMU guest agent is queryable, the monitoring memory selector must prefer
the guest's own `/proc/meminfo` `MemAvailable` signal before lower-trust
diff --git a/internal/config/guest_metadata.go b/internal/config/guest_metadata.go
index ecc01ae31b..faf195271b 100644
--- a/internal/config/guest_metadata.go
+++ b/internal/config/guest_metadata.go
@@ -30,6 +30,21 @@ type GuestMetadataStore struct {
fs FileSystem
}
+func cloneGuestMetadata(meta *GuestMetadata) *GuestMetadata {
+ if meta == nil {
+ return nil
+ }
+
+ clone := *meta
+ if meta.Tags != nil {
+ clone.Tags = append([]string(nil), meta.Tags...)
+ }
+ if meta.Notes != nil {
+ clone.Notes = append([]string(nil), meta.Notes...)
+ }
+ return &clone
+}
+
// NewGuestMetadataStore creates a new metadata store
func NewGuestMetadataStore(dataPath string, fs FileSystem) *GuestMetadataStore {
store := &GuestMetadataStore{
@@ -50,8 +65,6 @@ func NewGuestMetadataStore(dataPath string, fs FileSystem) *GuestMetadataStore {
return store
}
-// ... Get/GetWithLegacyMigration/GetAll/Set/Delete/ReplaceAll ... (unchanged)
-
// Load reads metadata from disk
func (s *GuestMetadataStore) Load() error {
filePath := filepath.Join(s.dataPath, "guest_metadata.json")
@@ -105,7 +118,7 @@ func (s *GuestMetadataStore) Get(guestID string) *GuestMetadata {
defer s.mu.RUnlock()
if meta, exists := s.metadata[guestID]; exists {
- return meta
+ return cloneGuestMetadata(meta)
}
return nil
}
@@ -124,35 +137,34 @@ func (s *GuestMetadataStore) GetWithLegacyMigration(guestID, instance, node stri
s.mu.RUnlock()
if exists {
- return meta
+ return cloneGuestMetadata(meta)
}
// Helper to migrate a legacy ID to the new format
migrate := func(legacyID string) *GuestMetadata {
- s.mu.RLock()
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
legacyMeta := s.metadata[legacyID]
- s.mu.RUnlock()
-
- if legacyMeta != nil {
- log.Info().
- Str("legacyID", legacyID).
- Str("newID", guestID).
- Msg("Migrating guest metadata from legacy ID format")
-
- s.mu.Lock()
- // Move to new ID
- s.metadata[guestID] = legacyMeta
- legacyMeta.ID = guestID
- delete(s.metadata, legacyID)
- // Persist while holding the store lock. save() assumes locked access.
- if err := s.save(); err != nil {
- log.Error().Err(err).Msg("Failed to save guest metadata after migration")
- }
- s.mu.Unlock()
-
- return legacyMeta
+ if legacyMeta == nil {
+ return cloneGuestMetadata(s.metadata[guestID])
}
- return nil
+
+ log.Info().
+ Str("legacyID", legacyID).
+ Str("newID", guestID).
+ Msg("Migrating guest metadata from legacy ID format")
+
+ migrated := cloneGuestMetadata(legacyMeta)
+ migrated.ID = guestID
+ s.metadata[guestID] = migrated
+ delete(s.metadata, legacyID)
+ // Persist while holding the store lock. save() assumes locked access.
+ if err := s.save(); err != nil {
+ log.Error().Err(err).Msg("Failed to save guest metadata after migration")
+ }
+
+ return cloneGuestMetadata(migrated)
}
// Try legacy format 1: instance-node-vmid (most specific)
@@ -187,7 +199,7 @@ func (s *GuestMetadataStore) GetAll() map[string]*GuestMetadata {
// Return a copy to prevent external modifications
result := make(map[string]*GuestMetadata)
for k, v := range s.metadata {
- result[k] = v
+ result[k] = cloneGuestMetadata(v)
}
return result
}
@@ -201,8 +213,9 @@ func (s *GuestMetadataStore) Set(guestID string, meta *GuestMetadata) error {
return fmt.Errorf("metadata cannot be nil")
}
- meta.ID = guestID
- s.metadata[guestID] = meta
+ clone := cloneGuestMetadata(meta)
+ clone.ID = guestID
+ s.metadata[guestID] = clone
// Save to disk
return s.save()
@@ -231,13 +244,13 @@ func (s *GuestMetadataStore) ReplaceAll(metadata map[string]*GuestMetadata) erro
continue
}
- clone := *meta
+ clone := cloneGuestMetadata(meta)
clone.ID = guestID
// Ensure slice copy is not nil to allow JSON marshalling of empty tags
if clone.Tags == nil {
clone.Tags = []string{}
}
- s.metadata[guestID] = &clone
+ s.metadata[guestID] = clone
}
return s.save()
diff --git a/internal/config/guest_metadata_test.go b/internal/config/guest_metadata_test.go
index 447eaf9385..e9718e33a2 100644
--- a/internal/config/guest_metadata_test.go
+++ b/internal/config/guest_metadata_test.go
@@ -48,6 +48,16 @@ func TestGuestMetadataStore_Get(t *testing.T) {
if result.LastKnownType != "qemu" {
t.Errorf("LastKnownType = %q, want %q", result.LastKnownType, "qemu")
}
+
+ result.CustomURL = "http://mutated.example.com"
+ result.Tags[0] = "mutated"
+ again := store.Get("pve1:node1:100")
+ if again.CustomURL != "http://example.com" {
+ t.Errorf("Get should return a metadata copy; stored CustomURL = %q, want %q", again.CustomURL, "http://example.com")
+ }
+ if again.Tags[0] != "tag1" {
+ t.Errorf("Get should copy slice fields; stored tag = %q, want tag1", again.Tags[0])
+ }
}
func TestGuestMetadataStore_GetAll(t *testing.T) {
@@ -75,6 +85,10 @@ func TestGuestMetadataStore_GetAll(t *testing.T) {
if len(store.metadata) != 2 {
t.Error("GetAll should return a copy, not the original map")
}
+ all["id1"].CustomURL = "mutated"
+ if got := store.Get("id1").CustomURL; got != "url1" {
+ t.Errorf("GetAll should return metadata copies; stored CustomURL = %q, want url1", got)
+ }
}
func TestGuestMetadataStore_Set(t *testing.T) {
@@ -100,9 +114,9 @@ func TestGuestMetadataStore_Set(t *testing.T) {
t.Fatalf("Set failed: %v", err)
}
- // Verify ID is set
- if meta.ID != "id1" {
- t.Errorf("ID = %q, want %q", meta.ID, "id1")
+ // Verify Set does not mutate caller-owned metadata.
+ if meta.ID != "" {
+ t.Errorf("caller metadata ID = %q, want empty", meta.ID)
}
// Verify stored
@@ -116,6 +130,19 @@ func TestGuestMetadataStore_Set(t *testing.T) {
if stored.LastKnownName != "vm1" {
t.Errorf("Stored LastKnownName = %q, want %q", stored.LastKnownName, "vm1")
}
+ if stored.ID != "id1" {
+ t.Errorf("Stored ID = %q, want %q", stored.ID, "id1")
+ }
+
+ meta.CustomURL = "http://mutated.example"
+ meta.Tags[0] = "mutated"
+ stored = store.Get("id1")
+ if stored.CustomURL != "http://test.com" {
+ t.Errorf("Set should store a metadata copy; stored CustomURL = %q, want %q", stored.CustomURL, "http://test.com")
+ }
+ if stored.Tags[0] != "tag1" {
+ t.Errorf("Set should copy slice fields; stored tag = %q, want tag1", stored.Tags[0])
+ }
// Verify persisted to disk
filePath := filepath.Join(tmpDir, "guest_metadata.json")