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 remaining RC3 backend CI races

rcourtmana3617b923a82
diff --git a/internal/api/router.go b/internal/api/router.go
index abff1a9dfc..ecd0f281d0 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -139,6 +139,8 @@ type Router struct {
relayCancel context.CancelFunc
lifecycleCtx context.Context
lifecycleCancel context.CancelFunc
+ lifecycleWG sync.WaitGroup
+ backgroundWorkersOnce sync.Once
hostedMode bool
stripeWebhookHandlers *StripeWebhookHandlers
patrolLifecycleMu sync.Mutex
@@ -274,12 +276,6 @@ func NewRouter(cfg *config.Config, monitor *monitoring.Monitor, mtMonitor *monit
r.setupRoutes()
log.Debug().Msg("Routes registered successfully")
- // Start forwarding update progress to WebSocket
- go r.forwardUpdateProgress()
-
- // Start background update checker
- go r.backgroundUpdateChecker(r.lifecycleCtx)
-
// Load system settings once at startup and cache them
r.reloadSystemSettings()
@@ -1086,6 +1082,32 @@ func (r *Router) Handler() http.Handler {
return r
}
+// StartBackgroundWorkers starts router-owned background workers for the API
+// server lifecycle. Unit tests that only need the HTTP handler can construct a
+// router without starting update checks or WebSocket forwarding loops.
+func (r *Router) StartBackgroundWorkers() {
+ if r == nil {
+ return
+ }
+ r.backgroundWorkersOnce.Do(func() {
+ r.startLifecycleWorker(r.forwardUpdateProgress)
+ r.startLifecycleWorker(func() {
+ r.backgroundUpdateChecker(r.lifecycleCtx)
+ })
+ })
+}
+
+func (r *Router) startLifecycleWorker(worker func()) {
+ if r == nil || worker == nil {
+ return
+ }
+ r.lifecycleWG.Add(1)
+ go func() {
+ defer r.lifecycleWG.Done()
+ worker()
+ }()
+}
+
// SetMonitor updates the router and associated handlers with a new monitor instance.
func (r *Router) SetMonitor(m *monitoring.Monitor) {
r.monitor = m
@@ -2170,6 +2192,9 @@ func (r *Router) shutdownBackgroundWorkers() {
if r.lifecycleCancel != nil {
r.lifecycleCancel()
}
+ if r.updateManager != nil {
+ r.updateManager.Close()
+ }
if r.sessionStore != nil {
r.sessionStore.Shutdown()
}
@@ -2193,6 +2218,7 @@ func (r *Router) shutdownBackgroundWorkers() {
log.Error().Err(err).Msg("Failed to close deploy store")
}
}
+ r.lifecycleWG.Wait()
}
// StartAIChat starts the AI chat service
diff --git a/internal/hostagent/agent.go b/internal/hostagent/agent.go
index d1c1f8ee6c..6765c0d325 100644
--- a/internal/hostagent/agent.go
+++ b/internal/hostagent/agent.go
@@ -482,20 +482,21 @@ func (a *Agent) signalRemoteConfigChanged() {
}
}
-func (a *Agent) startCommandClient(client *CommandClient) {
+func (a *Agent) startCommandClient(client *CommandClient) bool {
if client == nil {
- return
+ return false
}
a.commandClientMu.Lock()
if a.commandClientRunCancel != nil {
a.commandClientMu.Unlock()
- return
+ return true
}
parentCtx := a.commandClientParentCtx
if parentCtx == nil {
- parentCtx = context.Background()
+ a.commandClientMu.Unlock()
+ return false
}
runCtx, cancel := context.WithCancel(parentCtx)
a.commandClientRunCancel = cancel
@@ -506,6 +507,7 @@ func (a *Agent) startCommandClient(client *CommandClient) {
a.logger.Error().Err(err).Msg("Command client stopped with error")
}
}()
+ return true
}
func (a *Agent) stopCommandClient(clearClient bool) {
@@ -921,14 +923,19 @@ func (a *Agent) applyRemoteConfig(commandsEnabled bool) {
case commandsEnabled && !currentlyEnabled:
clientToStart = a.newCommandClient(commandCfg, a.agentID, a.hostname, a.platform, a.agentVersion)
a.commandClient = clientToStart
+ case commandsEnabled && currentlyEnabled:
+ clientToStart = a.commandClient
case !commandsEnabled && currentlyEnabled:
shouldStop = true
}
a.commandClientMu.Unlock()
if clientToStart != nil {
- a.logger.Info().Msg("Server enabled command execution - starting command client")
- a.startCommandClient(clientToStart)
+ if a.startCommandClient(clientToStart) {
+ a.logger.Info().Msg("Server enabled command execution - starting command client")
+ } else {
+ a.logger.Info().Msg("Server enabled command execution - command client configured pending run context")
+ }
} else if shouldStop {
a.logger.Info().Msg("Server disabled command execution - stopping command client")
a.stopCommandClient(true)
diff --git a/internal/hostagent/hostagent_coverage_test.go b/internal/hostagent/hostagent_coverage_test.go
index a998b3219e..59baabfe8b 100644
--- a/internal/hostagent/hostagent_coverage_test.go
+++ b/internal/hostagent/hostagent_coverage_test.go
@@ -141,7 +141,9 @@ func TestAgent_ApplyRemoteConfig_DefersCommandStartWithoutRunContext(t *testing.
}
parentCtx, cancel := context.WithCancel(context.Background())
- _ = parentCtx
+ a.commandClientMu.Lock()
+ a.commandClientParentCtx = parentCtx
+ a.commandClientMu.Unlock()
a.startCommandClient(a.commandClient)
select {
case <-started:
@@ -181,7 +183,9 @@ func TestAgent_CommandClientLifecycle_StopCancelsContext(t *testing.T) {
parentCtx, cancel := context.WithCancel(context.Background())
defer cancel()
- _ = parentCtx
+ a.commandClientMu.Lock()
+ a.commandClientParentCtx = parentCtx
+ a.commandClientMu.Unlock()
a.startCommandClient(a.commandClient)
select {
diff --git a/pkg/server/server.go b/pkg/server/server.go
index 68036f88f6..144e13363f 100644
--- a/pkg/server/server.go
+++ b/pkg/server/server.go
@@ -375,6 +375,7 @@ func Run(ctx context.Context, version string) error {
return nil
}
router = api.NewRouter(cfg, reloadableMonitor.GetMonitor(), reloadableMonitor.GetMultiTenantMonitor(), wsHub, reloadFunc, version)
+ router.StartBackgroundWorkers()
// Inject resource store into monitor for WebSocket broadcasts
router.SetMonitor(reloadableMonitor.GetMonitor())