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.

Publish the monitor's discovery service under its lock

rcourtman19dfd3040ca7
diff --git a/internal/monitoring/monitor.go b/internal/monitoring/monitor.go
index 11e3d7b8ee..36c145ec2b 100644
--- a/internal/monitoring/monitor.go
+++ b/internal/monitoring/monitor.go
@@ -2487,6 +2487,12 @@ func (m *Monitor) startModeRuntime(ctx context.Context, wsHub *websocket.Hub, mo
m.startMockMetricsSampler(ctx)
}
+ // Start runs on its own goroutine while request handlers already read the
+ // service through GetDiscoveryService, so it is published under m.mu, as
+ // StartDiscoveryService and StopDiscoveryService do.
+ m.mu.Lock()
+ defer m.mu.Unlock()
+
// Initialize and start discovery service if enabled
if mockEnabled {
log.Info().Msg("mock mode enabled - skipping discovery service")
@@ -2499,7 +2505,7 @@ func (m *Monitor) startModeRuntime(ctx context.Context, wsHub *websocket.Hub, mo
cfgProvider := func() config.DiscoveryConfig {
return m.discoveryConfigSnapshot()
}
- m.discoveryService = discovery.NewService(wsHub, m.GetOrgID(), 5*time.Minute, discoverySubnet, cfgProvider)
+ m.discoveryService = discovery.NewService(wsHub, m.orgID, 5*time.Minute, discoverySubnet, cfgProvider)
if m.discoveryService != nil {
m.discoveryService.Start(ctx)
log.Info().Msg("discovery service initialized and started")
@@ -5093,6 +5099,8 @@ func (m *Monitor) GetStartTime() time.Time {
// GetDiscoveryService returns the discovery service
func (m *Monitor) GetDiscoveryService() *discovery.Service {
+ m.mu.RLock()
+ defer m.mu.RUnlock()
return m.discoveryService
}
diff --git a/internal/monitoring/monitor_host_agents_test.go b/internal/monitoring/monitor_host_agents_test.go
index e79ad90d42..698a236a62 100644
--- a/internal/monitoring/monitor_host_agents_test.go
+++ b/internal/monitoring/monitor_host_agents_test.go
@@ -8829,6 +8829,32 @@ func TestRetiredQGAMemoryCannotAlterIndependentAgentEvidence(t *testing.T) {
}
}
+// A tenant monitor's Start runs on its own goroutine while request handlers,
+// such as auto-register, already read the discovery service. Under -race an
+// unlocked publish fails this test whether or not the two calls overlap.
+func TestMonitorStartupDiscoveryPublishIsSafeForConcurrentReaders(t *testing.T) {
+ for _, enabled := range []bool{false, true} {
+ t.Run(fmt.Sprintf("discoveryEnabled=%v", enabled), func(t *testing.T) {
+ monitor := &Monitor{config: &config.Config{DiscoveryEnabled: enabled, DiscoverySubnet: "127.0.0.1/32"}}
+ t.Cleanup(monitor.StopDiscoveryService)
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ started := make(chan struct{})
+ go func() {
+ defer close(started)
+ monitor.startModeRuntime(ctx, nil, false)
+ }()
+ monitor.GetDiscoveryService()
+ <-started
+
+ if got := monitor.GetDiscoveryService() != nil; got != enabled {
+ t.Fatalf("discovery service published = %v, want %v", got, enabled)
+ }
+ })
+ }
+}
+
func TestMonitorDiscoveryEventsStayWithOwningOrganization(t *testing.T) {
for _, start := range []string{"startup", "explicit"} {
for _, orgID := range []string{"org-a", "default", ""} {