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(server): dump goroutines when startup stalls before serving

pulse-triage[bot]03db74878247
diff --git a/pkg/server/server.go b/pkg/server/server.go
index 69d2952e94..1e56e135aa 100644
--- a/pkg/server/server.go
+++ b/pkg/server/server.go
@@ -228,6 +228,15 @@ func Run(ctx context.Context, version string) (runErr error) {
}
defer mainListener.Close()
+ // The listener is bound here but srv.Serve only starts near the end of Run.
+ // Track the last completed phase and arm a watchdog so a stall in the
+ // synchronous initialization window is diagnosable from the field log
+ // (issue #2129). The watchdog is stopped when serving begins or Run exits.
+ startup := &startupPhase{}
+ startup.mark("listener bound")
+ stopStartupWatchdog := startStartupWatchdog(log.Logger, startup, startupWatchdogBudget)
+ defer stopStartupWatchdog()
+
// Optional dedicated agent-control listener. Reports, command admission,
// version checks, and bootstrap downloads share this network boundary;
// the web UI and management API are not exposed on it.
@@ -446,6 +455,7 @@ func Run(ctx context.Context, version string) (runErr error) {
// Start monitoring
reloadableMonitor.Start(ctx)
+ startup.mark("monitoring started")
// Initialize API server with reload function
var router *api.Router
@@ -473,6 +483,7 @@ func Run(ctx context.Context, version string) (runErr error) {
router = api.NewRouter(cfg, reloadableMonitor.GetMonitor(), reloadableMonitor.GetMultiTenantMonitor(), wsHub, reloadFunc, version)
router.SetLicenseRuntimeIdentity(runtimeIdentity)
router.StartBackgroundWorkers()
+ startup.mark("API router initialized")
// Inject resource store into monitor for WebSocket broadcasts
router.SetMonitor(reloadableMonitor.GetMonitor())
@@ -484,6 +495,7 @@ func Run(ctx context.Context, version string) (runErr error) {
// Start AI chat service
router.StartAIChat(ctx)
+ startup.mark("AI services started")
// Start hosted tenant reaper for automatic soft-delete cleanup
if os.Getenv("PULSE_HOSTED_MODE") == "true" {
@@ -504,6 +516,7 @@ func Run(ctx context.Context, version string) (runErr error) {
// Wire alert-triggered AI analysis
router.WireAlertTriggeredAI()
+ startup.mark("relay and alert AI wired")
// Start pseudonymous telemetry (enabled by default; opt out via PULSE_TELEMETRY=false or Settings toggle).
// Persistence is created once here (outside the closure) to avoid NewConfigPersistence's
@@ -687,6 +700,7 @@ func Run(ctx context.Context, version string) (runErr error) {
failureTelemetryCfg = telemetryCfg
telemetry.Start(ctx, telemetryCfg)
defer telemetry.Stop()
+ startup.mark("telemetry started")
// Wire live telemetry toggle so Settings changes take effect immediately.
router.SetTelemetryToggleFunc(func(enabled bool) {
@@ -765,6 +779,7 @@ func Run(ctx context.Context, version string) (runErr error) {
}
defer configWatcher.Stop()
}
+ startup.mark("config watcher started")
// Start HTTP→HTTPS redirect server when HTTPS is active and redirect port is configured.
var redirectSrv *http.Server
@@ -825,6 +840,8 @@ func Run(ctx context.Context, version string) (runErr error) {
serverErr := make(chan error, 2)
go func() {
var err error
+ startup.mark("listener serving")
+ stopStartupWatchdog()
if cfg.HTTPSEnabled && cfg.TLSCertFile != "" && cfg.TLSKeyFile != "" {
log.Info().
Str("host", cfg.BindAddress).
diff --git a/pkg/server/startup_watchdog.go b/pkg/server/startup_watchdog.go
new file mode 100644
index 0000000000..e8070156db
--- /dev/null
+++ b/pkg/server/startup_watchdog.go
@@ -0,0 +1,84 @@
+package server
+
+import (
+ "runtime"
+ "sync"
+ "time"
+
+ "github.com/rs/zerolog"
+)
+
+// startupWatchdogBudget bounds how long Run may spend between binding the
+// UI/API listener and actually serving it.
+//
+// The listener is bound early so a port conflict fails fast, but nothing
+// accepts connections until srv.Serve starts. Every synchronous initialization
+// step in between (monitor start, router construction, optional AI/relay/
+// telemetry/config-watcher startup) therefore runs with a live listening
+// socket whose accept queue is filling and no reader. A normal startup
+// completes in seconds; this budget only trips when one of those steps stalls,
+// which is otherwise invisible because no log line is emitted while it runs.
+const startupWatchdogBudget = 2 * time.Minute
+
+// startupWatchdogStackBytes is the buffer reserved for the goroutine dump. The
+// default 64KiB used elsewhere truncates under a large goroutine set; a stalled
+// startup is exactly when the full stack matters.
+const startupWatchdogStackBytes = 256 * 1024
+
+// startupPhase records the last completed initialization step so a watchdog
+// dump names where Run was when it stalled instead of only showing the stacks.
+type startupPhase struct {
+ mu sync.Mutex
+ phase string
+}
+
+func (s *startupPhase) mark(phase string) {
+ if s == nil {
+ return
+ }
+ s.mu.Lock()
+ s.phase = phase
+ s.mu.Unlock()
+}
+
+func (s *startupPhase) current() string {
+ if s == nil {
+ return "unknown"
+ }
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ if s.phase == "" {
+ return "unknown"
+ }
+ return s.phase
+}
+
+// startStartupWatchdog arms an independent timer that fires only if the server
+// has not started serving within budget. It logs the last completed startup
+// phase and every goroutine stack so a recurrence of the reported
+// bound-but-not-serving startup stall (issue #2129) is diagnosable from the
+// field log alone, without waiting for an operator to send SIGQUIT. The
+// returned stop function must be called once serving begins or Run exits; it is
+// safe to call more than once.
+func startStartupWatchdog(logger zerolog.Logger, phase *startupPhase, budget time.Duration) (stop func()) {
+ done := make(chan struct{})
+ var once sync.Once
+ go func() {
+ timer := time.NewTimer(budget)
+ defer timer.Stop()
+ select {
+ case <-done:
+ case <-timer.C:
+ buf := make([]byte, startupWatchdogStackBytes)
+ n := runtime.Stack(buf, true)
+ logger.Error().
+ Dur("budget", budget).
+ Str("last_phase", phase.current()).
+ Str("goroutines", string(buf[:n])).
+ Msg("Pulse has not started serving its UI/API listener within the startup watchdog budget; startup is stalled")
+ }
+ }()
+ return func() {
+ once.Do(func() { close(done) })
+ }
+}
diff --git a/pkg/server/startup_watchdog_test.go b/pkg/server/startup_watchdog_test.go
new file mode 100644
index 0000000000..cc05141a0d
--- /dev/null
+++ b/pkg/server/startup_watchdog_test.go
@@ -0,0 +1,68 @@
+package server
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/rs/zerolog"
+)
+
+func TestStartupPhaseMarkAndCurrent(t *testing.T) {
+ var nilPhase *startupPhase
+ if got := nilPhase.current(); got != "unknown" {
+ t.Fatalf("nil phase current = %q, want unknown", got)
+ }
+ nilPhase.mark("ignored") // must not panic
+
+ phase := &startupPhase{}
+ if got := phase.current(); got != "unknown" {
+ t.Fatalf("empty phase current = %q, want unknown", got)
+ }
+ phase.mark("API router initialized")
+ if got := phase.current(); got != "API router initialized" {
+ t.Fatalf("phase current = %q, want last marked phase", got)
+ }
+}
+
+func TestStartupWatchdogFiresAndNamesLastPhase(t *testing.T) {
+ var buf bytes.Buffer
+ logger := zerolog.New(&buf)
+
+ phase := &startupPhase{}
+ phase.mark("AI services started")
+
+ stop := startStartupWatchdog(logger, phase, 20*time.Millisecond)
+ defer stop()
+
+ deadline := time.Now().Add(2 * time.Second)
+ for time.Now().Before(deadline) {
+ if strings.Contains(buf.String(), "startup is stalled") {
+ break
+ }
+ time.Sleep(5 * time.Millisecond)
+ }
+
+ out := buf.String()
+ if !strings.Contains(out, "startup is stalled") {
+ t.Fatalf("watchdog did not fire; log=%q", out)
+ }
+ if !strings.Contains(out, "AI services started") {
+ t.Fatalf("watchdog did not name the last startup phase; log=%q", out)
+ }
+}
+
+func TestStartupWatchdogStopIsIdempotentAndPreventsFire(t *testing.T) {
+ var buf bytes.Buffer
+ logger := zerolog.New(&buf)
+
+ stop := startStartupWatchdog(logger, &startupPhase{}, 15*time.Millisecond)
+ stop()
+ stop() // must not panic
+
+ time.Sleep(80 * time.Millisecond)
+ if strings.Contains(buf.String(), "startup is stalled") {
+ t.Fatalf("watchdog fired after stop; log=%q", buf.String())
+ }
+}