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.

Deduplicate concurrent host mount usage probes

pulse-triage[bot]3461c6cbd856
diff --git a/internal/hostmetrics/collector.go b/internal/hostmetrics/collector.go
index 8267f63b7a..46c11052ec 100644
--- a/internal/hostmetrics/collector.go
+++ b/internal/hostmetrics/collector.go
@@ -80,42 +80,50 @@ func cloneGopsutilEnv(ctx context.Context) gopsutilcommon.EnvMap {
// kernel wait, which would otherwise freeze the whole reporting cycle.
var diskUsageTimeout = 5 * time.Second
-// stuckDiskMounts tracks mountpoints whose usage syscall has not returned
-// yet. The blocked goroutine cannot be cancelled, so the mount is skipped
-// on later cycles until its original call finally comes back.
+// stuckDiskMounts tracks all in-flight usage calls, including calls that have
+// not timed out yet. Host and Docker collectors share the same mount probes.
var stuckDiskMounts sync.Map
-// guardedDiskUsage runs the usage syscall off the collection goroutine and
-// gives up after diskUsageTimeout, leaving at most one in-flight call per
-// mountpoint.
+type diskUsageCall struct {
+ done chan struct{}
+ deadline time.Time
+ usage *godisk.UsageStat
+ err error
+}
+
+// guardedDiskUsage bounds the wait for a mount probe and leaves at most one
+// syscall in flight per mountpoint, even when reporting loops overlap.
func guardedDiskUsage(ctx context.Context, mountpoint string) (*godisk.UsageStat, error) {
- if _, stuck := stuckDiskMounts.Load(mountpoint); stuck {
- return nil, fmt.Errorf("mountpoint %s skipped: previous usage call has not returned", mountpoint)
+ call := &diskUsageCall{
+ done: make(chan struct{}),
+ deadline: time.Now().Add(diskUsageTimeout),
}
-
- type usageResult struct {
- usage *godisk.UsageStat
- err error
+ actual, loaded := stuckDiskMounts.LoadOrStore(mountpoint, call)
+ if loaded {
+ call = actual.(*diskUsageCall)
+ } else {
+ go func() {
+ call.usage, call.err = diskUsage(ctx, mountpoint)
+ // Publish the result before admitting a new probe.
+ close(call.done)
+ stuckDiskMounts.Delete(mountpoint)
+ if time.Now().After(call.deadline) {
+ log.Info().Str("mount", mountpoint).Msg("disk: stalled usage call returned, mount re-included")
+ }
+ }()
}
- resultCh := make(chan usageResult, 1)
- go func() {
- usage, err := diskUsage(ctx, mountpoint)
- resultCh <- usageResult{usage: usage, err: err}
- }()
- timer := time.NewTimer(diskUsageTimeout)
+ timer := time.NewTimer(time.Until(call.deadline))
defer timer.Stop()
select {
- case result := <-resultCh:
- return result.usage, result.err
+ case <-call.done:
+ return call.usage, call.err
+ case <-ctx.Done():
+ return nil, ctx.Err()
case <-timer.C:
- stuckDiskMounts.Store(mountpoint, struct{}{})
- log.Warn().Str("mount", mountpoint).Dur("timeout", diskUsageTimeout).Msg("disk: usage call did not answer, excluding mount until it does (unresponsive network mount?)")
- go func() {
- <-resultCh
- stuckDiskMounts.Delete(mountpoint)
- log.Info().Str("mount", mountpoint).Msg("disk: stalled usage call returned, mount re-included")
- }()
+ if !loaded {
+ log.Warn().Str("mount", mountpoint).Dur("timeout", diskUsageTimeout).Msg("disk: usage call did not answer, excluding mount until it does (unresponsive network mount?)")
+ }
return nil, fmt.Errorf("usage call for %s did not answer within %s", mountpoint, diskUsageTimeout)
}
}
diff --git a/internal/hostmetrics/collector_stuck_mount_test.go b/internal/hostmetrics/collector_stuck_mount_test.go
index 1d0c3cb3c8..34521d5386 100644
--- a/internal/hostmetrics/collector_stuck_mount_test.go
+++ b/internal/hostmetrics/collector_stuck_mount_test.go
@@ -2,6 +2,7 @@ package hostmetrics
import (
"context"
+ "sync/atomic"
"testing"
"time"
@@ -142,3 +143,88 @@ func TestCollectDisks_NetworkMountsNeverReachUsageSyscall(t *testing.T) {
t.Fatalf("explicitly included network mount missing from collection: %+v", disks)
}
}
+
+// Host and Docker report loops can probe the same mount before either timeout
+// expires. They must share the in-flight syscall, including its healthy result.
+func TestGuardedDiskUsage_ConcurrentCollectorsShareCall(t *testing.T) {
+ origUsage, origTimeout := diskUsage, diskUsageTimeout
+ release := make(chan struct{})
+ var calls atomic.Int32
+ diskUsageTimeout = 100 * time.Millisecond
+ diskUsage = func(context.Context, string) (*godisk.UsageStat, error) {
+ calls.Add(1)
+ <-release
+ return &godisk.UsageStat{Total: 123}, nil
+ }
+ defer func() {
+ diskUsage, diskUsageTimeout = origUsage, origTimeout
+ }()
+ const mount = "/test/concurrent-collectors"
+ done := make(chan error, 2)
+ for i := 0; i < 2; i++ {
+ go func() {
+ _, err := guardedDiskUsage(context.Background(), mount)
+ done <- err
+ }()
+ }
+ for i := 0; i < 2; i++ {
+ if err := <-done; err == nil {
+ t.Error("blocked syscall should time out")
+ }
+ }
+ gotCalls := calls.Load()
+ close(release)
+ deadline := time.Now().Add(2 * time.Second)
+ for time.Now().Before(deadline) {
+ if _, ok := stuckDiskMounts.Load(mount); !ok {
+ break
+ }
+ time.Sleep(time.Millisecond)
+ }
+ if gotCalls != 1 {
+ t.Fatalf("concurrent collectors started %d syscalls, want 1", gotCalls)
+ }
+ usage, err := guardedDiskUsage(context.Background(), mount)
+ if err != nil || usage == nil || usage.Total != 123 {
+ t.Fatalf("recovered mount: usage=%v err=%v", usage, err)
+ }
+}
+
+// Waiting callers receive the owner's result rather than dropping a healthy
+// mount solely because another module happened to probe it first.
+func TestGuardedDiskUsage_SharedResult(t *testing.T) {
+ const mount = "/test/shared-result"
+ want := &godisk.UsageStat{Total: 456}
+ call := &diskUsageCall{
+ done: make(chan struct{}),
+ deadline: time.Now().Add(time.Second),
+ }
+ stuckDiskMounts.Store(mount, call)
+ defer stuckDiskMounts.Delete(mount)
+ go func() {
+ call.usage = want
+ close(call.done)
+ }()
+ got, err := guardedDiskUsage(context.Background(), mount)
+ if err != nil || got != want {
+ t.Fatalf("shared result = %v, %v; want %v", got, err, want)
+ }
+}
+
+func TestGuardedDiskUsage_CancelledWaitKeepsProbe(t *testing.T) {
+ const mount = "/test/cancelled-wait"
+ call := &diskUsageCall{
+ done: make(chan struct{}),
+ deadline: time.Now().Add(time.Hour),
+ }
+ stuckDiskMounts.Store(mount, call)
+ defer stuckDiskMounts.Delete(mount)
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ if _, err := guardedDiskUsage(ctx, mount); err != context.Canceled {
+ t.Fatalf("cancelled wait = %v", err)
+ }
+ if actual, ok := stuckDiskMounts.Load(mount); !ok || actual != call {
+ t.Fatal("cancelling a waiter must not admit a duplicate probe")
+ }
+}