From 19dfd3040ca72bc1fe464dd5cb83c8fa8d4ba0e9 Mon Sep 17 00:00:00 2001 From: rcourtman <8825017+rcourtman@users.noreply.github.com> Date: Tue, 3 Nov 2026 09:32:19 +0000 Subject: [PATCH] Publish the monitor's discovery service under its lock A tenant monitor's Start goroutine assigned discoveryService in startModeRuntime without holding m.mu, while request handlers such as auto-register read it through GetDiscoveryService. The race detector flagged the pair intermittently in TestTenantProxmoxInstallTokenGrantLivesOnInstanceRecord and failed unrelated pull requests. startModeRuntime now chooses and assigns the service under m.mu, and GetDiscoveryService reads it under the read lock, matching StartDiscoveryService and StopDiscoveryService. The org ID is read directly because GetOrgID would take the read lock again. A new race proof calls startModeRuntime and GetDiscoveryService from two goroutines with no ordering between them. Contract-Neutral: Locking fix for an existing monitor field; no API, schema, discovery behavior or subsystem contract change. --- internal/monitoring/monitor.go | 10 ++++++- .../monitoring/monitor_host_agents_test.go | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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", ""} {