From f2e1bdf879db495e2fb6933e5e0ecbe4027cd664 Mon Sep 17 00:00:00 2001 From: Jeff Lenamon Date: Sat, 19 Sep 2026 23:12:26 -0400 Subject: [PATCH] fix: return a repeated visible device once newDevices advanced its index for every id while the lookup map kept one entry per distinct id, so List returned an empty string in the slot a repeated id left behind. In cdi mode the empty entry became a request for nvidia.com/gpu=none and the container failed to start. Signed-off-by: Jeff Lenamon --- internal/config/image/cuda_image_test.go | 16 ++++++++++++++++ internal/config/image/devices.go | 3 +++ 2 files changed, 19 insertions(+) diff --git a/internal/config/image/cuda_image_test.go b/internal/config/image/cuda_image_test.go index 858034a41..ec73f6574 100644 --- a/internal/config/image/cuda_image_test.go +++ b/internal/config/image/cuda_image_test.go @@ -417,6 +417,22 @@ func TestGetDevicesFromEnvvar(t *testing.T) { }, expectedDevices: []string{anotherGPUID}, }, + { + description: "repeated device in NVIDIA_VISIBLE_DEVICES is returned once", + env: map[string]string{ + EnvVarNvidiaVisibleDevices: gpuID + "," + anotherGPUID + "," + gpuID, + }, + expectedDevices: []string{gpuID, anotherGPUID}, + }, + { + description: "device repeated across swarm resource envvars is returned once", + preferredVisibleDeviceEnvVars: []string{"DOCKER_RESOURCE_GPUS", "DOCKER_RESOURCE_GPUS_ADDITIONAL"}, + env: map[string]string{ + "DOCKER_RESOURCE_GPUS": gpuID, + "DOCKER_RESOURCE_GPUS_ADDITIONAL": gpuID, + }, + expectedDevices: []string{gpuID}, + }, } for _, tc := range tests { diff --git a/internal/config/image/devices.go b/internal/config/image/devices.go index 96b11d24a..86f59f243 100644 --- a/internal/config/image/devices.go +++ b/internal/config/image/devices.go @@ -93,6 +93,9 @@ func newDevices(idOrCommaSeparated ...string) devices { i := 0 for _, commaSeparated := range idOrCommaSeparated { for id := range strings.SplitSeq(commaSeparated, ",") { + if _, exists := lookup[id]; exists { + continue + } lookup[id] = i i++ }