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.

Keep an unresponsive mount from freezing host disk collection

rcourtmanaf6d48251525
diff --git a/internal/hostmetrics/collector.go b/internal/hostmetrics/collector.go
index 8966e2dc1e..c82db8dc5b 100644
--- a/internal/hostmetrics/collector.go
+++ b/internal/hostmetrics/collector.go
@@ -32,6 +32,52 @@ var (
netIOCounters = gonet.IOCountersWithContext
)
+// diskUsageTimeout bounds a single mountpoint usage syscall. statfs on a
+// healthy filesystem answers in microseconds; a hard-mounted network
+// filesystem whose server is unreachable blocks it in an uninterruptible
+// 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.
+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.
+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)
+ }
+
+ type usageResult struct {
+ usage *godisk.UsageStat
+ err error
+ }
+ resultCh := make(chan usageResult, 1)
+ go func() {
+ usage, err := diskUsage(ctx, mountpoint)
+ resultCh <- usageResult{usage: usage, err: err}
+ }()
+
+ timer := time.NewTimer(diskUsageTimeout)
+ defer timer.Stop()
+ select {
+ case result := <-resultCh:
+ return result.usage, result.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")
+ }()
+ return nil, fmt.Errorf("usage call for %s did not answer within %s", mountpoint, diskUsageTimeout)
+ }
+}
+
// Snapshot represents a host resource utilisation sample.
type Snapshot struct {
CPUUsagePercent float64
@@ -262,7 +308,21 @@ func collectDisksWithIncludes(ctx context.Context, diskExclude, diskInclude []st
continue
}
- usage, err := diskUsage(ctx, part.Mountpoint)
+ explicitlyIncluded := fsfilters.MatchesDiskInclude(part.Device, part.Mountpoint, diskInclude)
+ isZFS := strings.EqualFold(part.Fstype, "zfs") || strings.EqualFold(part.Fstype, "fuse.zfs")
+
+ // Type- and mountpoint-based skips need no usage data. Deciding them
+ // before the usage syscall keeps mounts that would be discarded
+ // anyway, hard NFS and CIFS mounts in particular, from stalling the
+ // collector inside statfs. ZFS stays after usage collection because
+ // its datasets are aggregated before this filter applies.
+ if !isZFS && !explicitlyIncluded {
+ if shouldSkip, _ := fsfilters.ShouldSkipFilesystem(part.Fstype, part.Mountpoint, 0, 0); shouldSkip {
+ continue
+ }
+ }
+
+ usage, err := guardedDiskUsage(ctx, part.Mountpoint)
if err != nil {
log.Debug().Err(err).Str("mount", part.Mountpoint).Str("device", part.Device).Str("fstype", part.Fstype).Msg("disk: failed to get usage")
continue
@@ -300,7 +360,6 @@ func collectDisksWithIncludes(ctx context.Context, diskExclude, diskInclude []st
// - Virtual/pseudo filesystems (tmpfs, devtmpfs, cgroup, etc.)
// - Container overlay paths (Docker/Podman layers on ZFS, including TrueNAS .ix-apps)
// See issues #505, #690, #718, #790.
- explicitlyIncluded := fsfilters.MatchesDiskInclude(part.Device, part.Mountpoint, diskInclude)
if shouldSkip, _ := fsfilters.ShouldSkipFilesystem(part.Fstype, part.Mountpoint, usage.Total, usage.Used); shouldSkip && !explicitlyIncluded {
continue
}
diff --git a/internal/hostmetrics/collector_stuck_mount_test.go b/internal/hostmetrics/collector_stuck_mount_test.go
new file mode 100644
index 0000000000..1d0c3cb3c8
--- /dev/null
+++ b/internal/hostmetrics/collector_stuck_mount_test.go
@@ -0,0 +1,144 @@
+package hostmetrics
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ godisk "github.com/shirou/gopsutil/v4/disk"
+)
+
+// A hard-mounted network filesystem whose server is unreachable blocks
+// statfs in an uninterruptible kernel wait. The collector must bound that
+// call, keep reporting the healthy mounts, skip the stuck mount on later
+// cycles without stacking goroutines, and re-include it once the stalled
+// call finally returns. Refs discussion #1747.
+func TestCollectDisks_UnresponsiveMountDoesNotBlockCycle(t *testing.T) {
+ origPartitions := diskPartitions
+ origUsage := diskUsage
+ origTimeout := diskUsageTimeout
+ defer func() {
+ diskPartitions = origPartitions
+ diskUsage = origUsage
+ diskUsageTimeout = origTimeout
+ }()
+
+ diskUsageTimeout = 50 * time.Millisecond
+
+ diskPartitions = func(ctx context.Context, all bool) ([]godisk.PartitionStat, error) {
+ return []godisk.PartitionStat{
+ {Device: "/dev/sda1", Mountpoint: "/", Fstype: "ext4"},
+ {Device: "/dev/mapper/slow", Mountpoint: "/mnt/slow", Fstype: "ext4"},
+ }, nil
+ }
+
+ release := make(chan struct{})
+ inFlight := make(chan struct{}, 16)
+ diskUsage = func(ctx context.Context, path string) (*godisk.UsageStat, error) {
+ if path == "/mnt/slow" {
+ inFlight <- struct{}{}
+ <-release
+ }
+ total := uint64(100 * 1024 * 1024 * 1024)
+ return &godisk.UsageStat{Total: total, Used: total / 2, Free: total / 2, UsedPercent: 50}, nil
+ }
+
+ mounts := func(ctx context.Context) []string {
+ names := []string{}
+ for _, disk := range collectDisksWithIncludes(ctx, nil, nil) {
+ names = append(names, disk.Mountpoint)
+ }
+ return names
+ }
+
+ ctx := context.Background()
+
+ start := time.Now()
+ got := mounts(ctx)
+ if elapsed := time.Since(start); elapsed > 2*time.Second {
+ t.Fatalf("collection took %s, the stuck mount blocked the cycle", elapsed)
+ }
+ if len(got) != 1 || got[0] != "/" {
+ t.Fatalf("first cycle mounts = %v, want just /", got)
+ }
+
+ // Second cycle: the stalled call is still in flight, so the mount is
+ // skipped without issuing another syscall.
+ got = mounts(ctx)
+ if len(got) != 1 || got[0] != "/" {
+ t.Fatalf("second cycle mounts = %v, want just /", got)
+ }
+ if len(inFlight) != 1 {
+ t.Fatalf("expected exactly one in-flight usage call for the stuck mount, got %d", len(inFlight))
+ }
+
+ // The mount recovers: the stalled call returns and the mount is
+ // re-included on a later cycle.
+ close(release)
+ deadline := time.Now().Add(2 * time.Second)
+ for time.Now().Before(deadline) {
+ if _, stuck := stuckDiskMounts.Load("/mnt/slow"); !stuck {
+ break
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+ got = mounts(ctx)
+ if len(got) != 2 {
+ t.Fatalf("post-recovery mounts = %v, want / and /mnt/slow", got)
+ }
+}
+
+// Network filesystems are dropped by the fstype filter, so their usage must
+// be decided before the syscall: a hard NFS mount with an unreachable server
+// blocks statfs indefinitely even though the result would be discarded.
+// Refs discussion #1747.
+func TestCollectDisks_NetworkMountsNeverReachUsageSyscall(t *testing.T) {
+ origPartitions := diskPartitions
+ origUsage := diskUsage
+ defer func() {
+ diskPartitions = origPartitions
+ diskUsage = origUsage
+ }()
+
+ diskPartitions = func(ctx context.Context, all bool) ([]godisk.PartitionStat, error) {
+ return []godisk.PartitionStat{
+ {Device: "/dev/sda1", Mountpoint: "/", Fstype: "ext4"},
+ {Device: "nas1:/export", Mountpoint: "/mnt/pve/nas1", Fstype: "nfs4"},
+ {Device: "//nas2/share", Mountpoint: "/mnt/smb", Fstype: "cifs"},
+ }, nil
+ }
+
+ probed := map[string]bool{}
+ diskUsage = func(ctx context.Context, path string) (*godisk.UsageStat, error) {
+ probed[path] = true
+ total := uint64(10 * 1024 * 1024 * 1024)
+ return &godisk.UsageStat{Total: total, Used: total / 2, Free: total / 2, UsedPercent: 50}, nil
+ }
+
+ disks := collectDisksWithIncludes(context.Background(), nil, nil)
+ if len(disks) != 1 || disks[0].Mountpoint != "/" {
+ t.Fatalf("expected only / to be collected, got %+v", disks)
+ }
+ for _, mount := range []string{"/mnt/pve/nas1", "/mnt/smb"} {
+ if probed[mount] {
+ t.Fatalf("usage syscall was issued for filtered network mount %s", mount)
+ }
+ }
+
+ // An explicit include opts the network mount back in, and only then is
+ // its usage collected.
+ probed = map[string]bool{}
+ disks = collectDisksWithIncludes(context.Background(), nil, []string{"/mnt/pve/nas1"})
+ if !probed["/mnt/pve/nas1"] {
+ t.Fatal("explicitly included network mount was not probed")
+ }
+ found := false
+ for _, disk := range disks {
+ if disk.Mountpoint == "/mnt/pve/nas1" {
+ found = true
+ }
+ }
+ if !found {
+ t.Fatalf("explicitly included network mount missing from collection: %+v", disks)
+ }
+}