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(monitoring): release reload callers on server cancellation

pulse-triage[bot]4d5222f217db
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index e44e52ace5..232042dbfc 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -3947,3 +3947,19 @@ bridge on either side alone must not supply inferred identity: testing only
symmetric bridges would miss removal of filtering from one inventory.
This proof does not establish safety for arbitrarily renamed Docker bridges
or repair persisted links, and is not reporter or installed-release validation.
+
+### Reload handshake cancellation
+
+`ReloadableMonitor.Reload` rejects calls before `Start` and returns the server
+parent context error if shutdown cancels either request submission or the wait
+for completion. The reply remains buffered so completion of an already accepted
+reload does not block a watcher whose caller has left. Ordinary reload success
+and configuration errors still propagate unchanged. This does not cancel an
+in-flight `doReload`, bound its teardown time, or change monitor replacement and
+router refresh ordering. `Stop` alone is not parent-context cancellation.
+
+`internal/monitoring/reload_test.go` exercises cancellation before submission,
+without a request consumer, and after acceptance, plus late completion and
+success/error propagation using channel-only fixtures without starting monitors
+or storage. This is shutdown handshake proof, not installed notification receipt
+or full server reload acceptance.
diff --git a/internal/monitoring/reload.go b/internal/monitoring/reload.go
index 22426f6415..5e36b97dc7 100644
--- a/internal/monitoring/reload.go
+++ b/internal/monitoring/reload.go
@@ -105,9 +105,31 @@ func (rm *ReloadableMonitor) Start(ctx context.Context) {
// Reload triggers a monitor reload
func (rm *ReloadableMonitor) Reload() error {
+ rm.mu.RLock()
+ ctx := rm.parentCtx
+ rm.mu.RUnlock()
+ if ctx == nil {
+ return fmt.Errorf("reloadable monitor has not been started")
+ }
+ if err := ctx.Err(); err != nil {
+ return err
+ }
+
+ // The watcher exits with the server context. Neither a full request
+ // channel nor an abandoned reply may keep a caller waiting after that.
+ // Keep the reply buffered so an in-flight reload can still finish.
done := make(chan error, 1)
- rm.reloadChan <- done
- return <-done
+ select {
+ case rm.reloadChan <- done:
+ case <-ctx.Done():
+ return ctx.Err()
+ }
+ select {
+ case err := <-done:
+ return err
+ case <-ctx.Done():
+ return ctx.Err()
+ }
}
// watchReload watches for reload signals
diff --git a/internal/monitoring/reload_test.go b/internal/monitoring/reload_test.go
index 6219bb6ca0..adedc82487 100644
--- a/internal/monitoring/reload_test.go
+++ b/internal/monitoring/reload_test.go
@@ -2,6 +2,7 @@ package monitoring
import (
"context"
+ "errors"
"testing"
"time"
@@ -251,3 +252,70 @@ func testTelemetryMonitor(
return &Monitor{state: state}
}
+
+// These fixtures exercise only the reload handshake; no monitor or storage is started.
+func TestReloadCancellation(t *testing.T) {
+ for _, phase := range []string{"before-request", "blocked-request", "awaiting-result"} {
+ t.Run(phase, func(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ rm := &ReloadableMonitor{parentCtx: ctx, reloadChan: make(chan chan error)}
+ if phase == "before-request" {
+ cancel()
+ }
+ result := make(chan error, 1)
+ go func() { result <- rm.Reload() }()
+ var reply chan error
+ if phase == "awaiting-result" {
+ select {
+ case reply = <-rm.reloadChan:
+ case <-time.After(time.Second):
+ t.Fatal("request not submitted")
+ }
+ }
+ cancel()
+ select {
+ case err := <-result:
+ if !errors.Is(err, context.Canceled) {
+ t.Fatalf("got %v, want context.Canceled", err)
+ }
+ case <-time.After(time.Second):
+ // Release the old implementation's blocked caller before failing.
+ if reply == nil {
+ reply = <-rm.reloadChan
+ }
+ reply <- nil
+ <-result
+ t.Fatal("Reload remained blocked after cancellation")
+ }
+ if reply != nil {
+ // Completion after caller cancellation must not strand the watcher.
+ select {
+ case reply <- nil:
+ default:
+ t.Fatal("late completion would block watcher")
+ }
+ }
+ })
+ }
+}
+
+func TestReloadHandshakeResult(t *testing.T) {
+ for _, expected := range []error{nil, errors.New("reload failed")} {
+ rm := &ReloadableMonitor{parentCtx: context.Background(), reloadChan: make(chan chan error)}
+ go func() { reply := <-rm.reloadChan; reply <- expected }()
+ if err := rm.Reload(); err != expected {
+ t.Fatalf("got %v, want %v", err, expected)
+ }
+ }
+}
+
+func TestReloadBeforeStart(t *testing.T) {
+ rm := &ReloadableMonitor{reloadChan: make(chan chan error, 1)}
+ if err := rm.Reload(); err == nil {
+ t.Fatal("Reload before Start must return an error")
+ }
+ if len(rm.reloadChan) != 0 {
+ t.Fatal("unstarted reload was queued")
+ }
+}