From 954c57071a1a049d243a1552de220d761aa282e6 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Feb 2026 21:48:19 +0000 Subject: [PATCH] fix: clamp VRAM Free to prevent uint64 underflow Guard against transient sysfs inconsistency where used > total. Co-Authored-By: Virgil --- vram.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/vram.go b/vram.go index 26f1385..954ca9a 100644 --- a/vram.go +++ b/vram.go @@ -13,6 +13,9 @@ import ( // GetVRAMInfo reads VRAM usage for the discrete GPU from sysfs. // It identifies the dGPU by selecting the card with the largest VRAM total, // which avoids hardcoding card numbers (e.g. card0=iGPU, card1=dGPU on Ryzen). +// +// Note: total and used are read non-atomically from sysfs; transient +// inconsistencies are possible under heavy allocation churn. func GetVRAMInfo() (VRAMInfo, error) { cards, err := filepath.Glob("/sys/class/drm/card[0-9]*/device/mem_info_vram_total") if err != nil { @@ -45,10 +48,15 @@ func GetVRAMInfo() (VRAMInfo, error) { return VRAMInfo{}, fmt.Errorf("rocm: read vram used: %w", err) } + free := uint64(0) + if bestTotal > used { + free = bestTotal - used + } + return VRAMInfo{ Total: bestTotal, Used: used, - Free: bestTotal - used, + Free: free, }, nil }