Skip to content

Fix/ascend pod device index out of range - #119

Closed
silvasong wants to merge 2 commits into
Project-HAMi:mainfrom
silvasong:fix/ascend-pod-device-index-out-of-range
Closed

Fix/ascend pod device index out of range#119
silvasong wants to merge 2 commits into
Project-HAMi:mainfrom
silvasong:fix/ascend-pod-device-index-out-of-range

Conversation

@silvasong

@silvasong silvasong commented Aug 4, 2026

Copy link
Copy Markdown

Title

fix: prevent index out of range panic in Ascend pod device annotation decoding

Description

Problem

When a pod with Ascend (910B/310P) GPU devices has fewer device annotation entries than the total container count, the DecodePodDevices function produces a PodSingleDevice slice shorter than expected. This causes a runtime error: index out of range [1] with length 1 panic in fetchContainerInfo at pod.go.

Observed a panic: runtime.boundsError (runtime error: index out of range [1] with length 1)
    vgpu/internal/data.(*podRepo).fetchContainerInfo(...)
        /src/internal/data/pod.go:147

Root Cause

The Ascend/310P device branch in DecodePodDevices was missing three safeguards that Nvidia, DCU, and Metax already have:

  1. podContainerCount boundary check — no if i >= podContainerCount(pod) { break }
  2. Empty string placeholder — used continue to skip empty entries instead of appending an empty ContainerDevices{}, which misaligned the index
  3. Indexed loop — used for _, s := range instead of for i, s := range

Fix

Align the Ascend/310P branch with the Nvidia/DCU/Metax pattern:

- for _, s := range strings.Split(str, OnePodMultiContainerSplitSymbol) {
+ for i, s := range strings.Split(str, OnePodMultiContainerSplitSymbol) {
+     if i >= podContainerCount(pod) {
+         break
+     }
+     if s == "" {
+         pd[devType] = append(pd[devType], ContainerDevices{})
+         continue
+     }
      cd, err := DecodeNpuContainerDevices(s)
      ...
-     if len(cd) == 0 {
-         continue
-     }

Verification

  • go vet passes
  • TestDecodePodDevicesWithInitContainers passes

Summary by CodeRabbit

  • Bug Fixes
    • Improved device annotation handling for pods using Ascend/NPU resources.
    • Preserved empty device entries for containers without assigned devices.
    • Ignored annotation data beyond the pod’s actual container count.

宋杰 added 2 commits August 4, 2026 16:38
Add podContainerCount boundary check and empty slice placeholder for Ascend/310P device annotations to match Nvidia/DCU/Metax patterns, preventing 'index out of range [1] with length 1' panic when pod has more containers than annotation entries.
Add podContainerCount boundary check and empty slice placeholder for Ascend/310P device annotations to match Nvidia/DCU/Metax patterns, preventing 'index out of range [1] with length 1' panic when pod has more containers than annotation entries.
@hami-robot

hami-robot Bot commented Aug 4, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: silvasong

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot

hami-robot Bot commented Aug 4, 2026

Copy link
Copy Markdown

Thanks for your pull request. Before we can look at it, you'll need to add a 'DCO signoff' to your commits.

📝 Please follow instructions in the contributing guide to update your commits with the DCO

Full details of the Developer Certificate of Origin can be found at developercertificate.org.

The list of commits missing DCO signoff:

  • 8e7a535 fix: prevent index out of range in Ascend pod device decoding
  • 136e078 fix: prevent index out of range in Ascend pod device decoding
Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@hami-robot

hami-robot Bot commented Aug 4, 2026

Copy link
Copy Markdown

Welcome @silvasong! It looks like this is your first PR to Project-HAMi/HAMi-WebUI 🎉

@hami-robot hami-robot Bot added the size/S label Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

DecodePodDevices now bounds Ascend annotation processing by pod container count. It preserves empty per-container device entries and ignores excess annotation segments.

Changes

Ascend device decoding

Layer / File(s) Summary
Bounded container annotation decoding
server/internal/provider/util/util.go
DecodePodDevices tracks annotation segment indexes, stops at the pod container count, preserves empty entries, and appends decoded device lists.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: fouof

Poem

I’m a rabbit with devices in line,
Empty entries stay where they belong.
Extra segments stop at the gate,
Each container keeps its proper state.
Hop, decode, and carry on!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the Ascend pod device index-out-of-range fix, which matches the main change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai
coderabbitai Bot requested a review from FouoF August 4, 2026 09:19
@coderabbitai coderabbitai Bot added the enhancement New feature or request label Aug 4, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@server/internal/provider/util/util.go`:
- Around line 329-342: Update the Ascend container-device parsing loop around
DecodeNpuContainerDevices to ensure pd[devType] contains exactly
podContainerCount(pod) entries, appending ContainerDevices{} for missing
trailing split segments while retaining existing handling for empty and invalid
segments. Add a regression test covering Ascend/310P annotations with fewer
segments, using the relevant test setup instead of the NVIDIA-only case in
util_test.go.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3787b080-8279-4650-a72d-a9cac45b03ef

📥 Commits

Reviewing files that changed from the base of the PR and between fa9b560 and 136e078.

📒 Files selected for processing (1)
  • server/internal/provider/util/util.go

Comment on lines +329 to 342
for i, s := range strings.Split(str, OnePodMultiContainerSplitSymbol) {
if i >= podContainerCount(pod) {
break
}
if s == "" {
pd[devType] = append(pd[devType], ContainerDevices{})
continue
}
cd, err := DecodeNpuContainerDevices(s)
if err != nil {
return PodDevices{}, nil
}
if len(cd) == 0 {
continue
}
pd[devType] = append(pd[devType], cd)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

Pad missing Ascend container slots.

When the annotation contains fewer split segments than podContainerCount(pod), this loop appends only the segments that exist. The index check limits excess input, but it does not add missing trailing ContainerDevices{} entries. fetchContainerInfo can still index a shorter PodSingleDevice slice and panic.

Iterate through the container count and treat absent segments as empty, or pad the slice after the loop. Add an Ascend/310P regression test. server/internal/provider/util/util_test.go:353-403 selects NVIDIA, so it does not execute this branch.

Proposed fix
-			for i, s := range strings.Split(str, OnePodMultiContainerSplitSymbol) {
-				if i >= podContainerCount(pod) {
-					break
-				}
+			segments := strings.Split(str, OnePodMultiContainerSplitSymbol)
+			containerCount := podContainerCount(pod)
+			for i := 0; i < containerCount; i++ {
+				s := ""
+				if i < len(segments) {
+					s = segments[i]
+				}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for i, s := range strings.Split(str, OnePodMultiContainerSplitSymbol) {
if i >= podContainerCount(pod) {
break
}
if s == "" {
pd[devType] = append(pd[devType], ContainerDevices{})
continue
}
cd, err := DecodeNpuContainerDevices(s)
if err != nil {
return PodDevices{}, nil
}
if len(cd) == 0 {
continue
}
pd[devType] = append(pd[devType], cd)
}
segments := strings.Split(str, OnePodMultiContainerSplitSymbol)
containerCount := podContainerCount(pod)
for i := 0; i < containerCount; i++ {
s := ""
if i < len(segments) {
s = segments[i]
}
if s == "" {
pd[devType] = append(pd[devType], ContainerDevices{})
continue
}
cd, err := DecodeNpuContainerDevices(s)
if err != nil {
return PodDevices{}, nil
}
pd[devType] = append(pd[devType], cd)
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server/internal/provider/util/util.go` around lines 329 - 342, Update the
Ascend container-device parsing loop around DecodeNpuContainerDevices to ensure
pd[devType] contains exactly podContainerCount(pod) entries, appending
ContainerDevices{} for missing trailing split segments while retaining existing
handling for empty and invalid segments. Add a regression test covering
Ascend/310P annotations with fewer segments, using the relevant test setup
instead of the NVIDIA-only case in util_test.go.

@Nimbus318

Copy link
Copy Markdown
Collaborator

Thanks for the fix. This is now covered by merged #95, which preserves Ascend empty container slots, bounds decoded entries to the Pod container count, and adds the aggregation fix and regression coverage. Closing as superseded.

@Nimbus318 Nimbus318 closed this Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants