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.

Resolve History dependencies outside the store publication lock

pulse-triage[bot]27a3e57be85c
diff --git a/internal/api/ai_handlers.go b/internal/api/ai_handlers.go
index 8f64cae99e..61e03a07fc 100644
--- a/internal/api/ai_handlers.go
+++ b/internal/api/ai_handlers.go
@@ -1842,14 +1842,12 @@ func (h *AISettingsHandler) WireOrchestratorAfterChatStart() {
// setupInvestigationOrchestrator creates and wires the investigation orchestrator for an AI service
func (h *AISettingsHandler) setupInvestigationOrchestrator(orgID string, svc *ai.Service) {
- h.investigationMu.Lock()
- defer h.investigationMu.Unlock()
- h.setupInvestigationOrchestratorLocked(orgID, svc)
+ h.configureInvestigationOrchestrator(orgID, svc, false)
}
-// setupInvestigationOrchestratorLocked publishes a store only after its retained
-// history has loaded. The same lock serializes startup and read-side recovery.
-func (h *AISettingsHandler) setupInvestigationOrchestratorLocked(orgID string, svc *ai.Service) {
+// configureInvestigationOrchestrator publishes a store only after its retained
+// history has loaded. readRetry must not replace an already recovered runtime.
+func (h *AISettingsHandler) configureInvestigationOrchestrator(orgID string, svc *ai.Service, readRetry bool) {
// Check factory exists first — if nil, this is OSS (no orchestrator available).
// Clear any stale orchestrator from a prior setup so the patrol service
// doesn't keep using a removed enterprise component.
@@ -1876,6 +1874,29 @@ func (h *AISettingsHandler) setupInvestigationOrchestratorLocked(orgID string, s
return
}
+ // GetService may invoke dependency initializers, which take the AI service
+ // and investigation-map locks. Resolve it before locking load/publication.
+ // Get chat service for this org using org-scoped context
+ ctx := context.WithValue(context.Background(), OrgIDContextKey, orgID)
+ chatSvc := chatHandler.GetService(ctx)
+ if chatSvc == nil {
+ log.Warn().Str("orgID", orgID).Msg("Chat service not available for orchestrator")
+ return
+ }
+
+ // Create chat adapter - need to cast to *chat.Service
+ chatService, ok := chatSvc.(*chat.Service)
+ if !ok {
+ log.Warn().Str("orgID", orgID).Msg("Chat service is not *chat.Service, cannot create adapter")
+ return
+ }
+
+ h.investigationMu.Lock()
+ defer h.investigationMu.Unlock()
+ if readRetry && patrol.GetInvestigationOrchestrator() != nil {
+ return
+ }
+
// A missing tenant directory must not silently become an empty memory-only
// store. It would lose the original run and permit new writes over unread
// history. Never fall back to another organization's persistence.
@@ -1914,21 +1935,6 @@ func (h *AISettingsHandler) setupInvestigationOrchestratorLocked(orgID string, s
h.investigationStores[orgID] = store
}
- // Get chat service for this org using org-scoped context
- ctx := context.WithValue(context.Background(), OrgIDContextKey, orgID)
- chatSvc := chatHandler.GetService(ctx)
- if chatSvc == nil {
- log.Warn().Str("orgID", orgID).Msg("Chat service not available for orchestrator")
- return
- }
-
- // Create chat adapter - need to cast to *chat.Service
- chatService, ok := chatSvc.(*chat.Service)
- if !ok {
- log.Warn().Str("orgID", orgID).Msg("Chat service is not *chat.Service, cannot create adapter")
- return
- }
-
// Mirror default-org router wiring for per-org services so patrol/investigation
// executions always use the chat backend path with mid-run budget enforcement.
svc.SetChatService(&chatServiceAdapter{svc: chatService})
@@ -8346,13 +8352,9 @@ func (h *AISettingsHandler) investigationOrchestratorForRead(ctx context.Context
if !isAIInvestigationEnabled() {
return nil
}
- h.investigationMu.Lock()
- defer h.investigationMu.Unlock()
- // Concurrent retries must not replace a newly recovered live orchestrator.
- if orchestrator := patrol.GetInvestigationOrchestrator(); orchestrator != nil {
- return orchestrator
- }
- h.setupInvestigationOrchestratorLocked(GetOrgID(ctx), svc)
+ // The publication lock rechecks readiness after dependency lookup, so
+ // concurrent retries do not replace a newly recovered live orchestrator.
+ h.configureInvestigationOrchestrator(GetOrgID(ctx), svc, true)
return patrol.GetInvestigationOrchestrator()
}
diff --git a/internal/api/ai_history_readiness_test.go b/internal/api/ai_history_readiness_test.go
index faf678cdb3..37dce43cf4 100644
--- a/internal/api/ai_history_readiness_test.go
+++ b/internal/api/ai_history_readiness_test.go
@@ -391,3 +391,20 @@ func TestInvestigationReadRequiresScopedPersistenceAndEnabledRuntime(t *testing.
t.Fatalf("disabled runtime was created by a read: %d %s", rec.Code, rec.Body.String())
}
}
+
+func TestInvestigationReadInitializersRunOutsideStoreLock(t *testing.T) {
+ f := newHistoryReadFixture(t)
+ var calls atomic.Int32
+ f.handler.chatHandler.SetServiceInitializer(func(context.Context, AIService) {
+ calls.Add(1)
+ if !f.handler.investigationMu.TryRLock() {
+ t.Error("dependency initializer called under investigation lock")
+ return
+ }
+ f.handler.investigationMu.RUnlock()
+ })
+ rec := f.read("tenant-a", "")
+ if rec.Code != http.StatusOK || calls.Load() != 1 {
+ t.Fatalf("read retry did not initialize once: %d %s", rec.Code, rec.Body.String())
+ }
+}