From 206222f7d97f6fbaa6af1dd3f194a779614a822b Mon Sep 17 00:00:00 2001 From: Nakshatra Sharma Date: Wed, 5 Aug 2026 09:45:58 +0530 Subject: [PATCH] fix(monitor): skip dirs without underscore in Update instead of panicking strings.Split(entry.Name(), "_")[1] in Update() assumes every directory under the hook path follows the _ format. A stray directory without an underscore causes an index out of range panic, which crashes vGPUmonitor and silences all GPU metrics on that node. Replace both unguarded Split calls with a single SplitN(..., 2) whose result is validated before use; directories that do not match the expected format are now skipped with a warning. Signed-off-by: Nakshatra Sharma --- pkg/monitor/nvidia/cudevshr.go | 10 +++++++--- pkg/monitor/nvidia/cudevshr_test.go | 30 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pkg/monitor/nvidia/cudevshr.go b/pkg/monitor/nvidia/cudevshr.go index 788401bc44..ec9fc39ed6 100644 --- a/pkg/monitor/nvidia/cudevshr.go +++ b/pkg/monitor/nvidia/cudevshr.go @@ -185,8 +185,13 @@ func (l *ContainerLister) Update() error { if !entry.IsDir() { continue } + parts := strings.SplitN(entry.Name(), "_", 2) + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { + klog.Warningf("Skipping dir with unexpected name format: %s", entry.Name()) + continue + } dirName := filepath.Join(l.containerPath, entry.Name()) - podUID := strings.Split(entry.Name(), "_")[0] + podUID := parts[0] if !podUIDs[podUID] { dirInfo, err := os.Stat(dirName) if err == nil && dirInfo.ModTime().Add(resyncInterval).After(time.Now()) { @@ -209,11 +214,10 @@ func (l *ContainerLister) Update() error { continue } if usage == nil { - // no cuInit in container continue } usage.PodUID = podUID - usage.ContainerName = strings.Split(entry.Name(), "_")[1] + usage.ContainerName = parts[1] l.containers[entry.Name()] = usage klog.Infof("Adding ctr dirname %s in monitorpath", dirName) } diff --git a/pkg/monitor/nvidia/cudevshr_test.go b/pkg/monitor/nvidia/cudevshr_test.go index c28e28fe90..f96f2a0089 100644 --- a/pkg/monitor/nvidia/cudevshr_test.go +++ b/pkg/monitor/nvidia/cudevshr_test.go @@ -432,4 +432,34 @@ func Test_ContainerLister_Update(t *testing.T) { assert.Equal(t, got.ContainerName, "mycontainer") defer func() { _ = syscall.Munmap(got.data) }() }) + + t.Run("dir without underscore in name is skipped", func(t *testing.T) { + dir := t.TempDir() + pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: "default", UID: "nodashes"}} + ctrDir := filepath.Join(dir, "nodashes") + assert.NilError(t, os.Mkdir(ctrDir, 0755)) + writeCacheFile(t, ctrDir, "x.cache", headerBytes(v1CacheFileSize, SharedRegionMagicFlag, 1, 0)) + l := &ContainerLister{ + containerPath: dir, + containers: map[string]*ContainerUsage{}, + podLister: newTestPodLister(pod), + } + assert.NilError(t, l.Update()) + assert.Equal(t, len(l.containers), 0) + }) + + t.Run("dir with trailing underscore is skipped", func(t *testing.T) { + dir := t.TempDir() + pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "p", Namespace: "default", UID: "uid"}} + ctrDir := filepath.Join(dir, "uid_") + assert.NilError(t, os.Mkdir(ctrDir, 0755)) + writeCacheFile(t, ctrDir, "x.cache", headerBytes(v1CacheFileSize, SharedRegionMagicFlag, 1, 0)) + l := &ContainerLister{ + containerPath: dir, + containers: map[string]*ContainerUsage{}, + podLister: newTestPodLister(pod), + } + assert.NilError(t, l.Update()) + assert.Equal(t, len(l.containers), 0) + }) }