mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-29 17:21:13 +01:00
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.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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", ""} {
|
||||
|
||||
Reference in New Issue
Block a user