Skip to content

refactor: replace NVML util panics with error returns - #2234

Closed
shellyco-code wants to merge 1 commit into
Project-HAMi:masterfrom
shellyco-code:fix-util-nvml-panic
Closed

refactor: replace NVML util panics with error returns#2234
shellyco-code wants to merge 1 commit into
Project-HAMi:masterfrom
shellyco-code:fix-util-nvml-panic

Conversation

@shellyco-code

@shellyco-code shellyco-code commented Jul 31, 2026

Copy link
Copy Markdown

What type of PR is this?

/kind bug

What this PR does / why we need it:
This PR fixes a critical node-level reliability issue where the HAMi device plugin crashes via panic(0) if it encounters any NVML query failures (e.g., driver glitches) in utility functions.

Instead of panicking and crashing the plugin, GetIndexAndTypeFromUUID and GetMigUUIDFromIndex now return an error. The caller (GetContainerDeviceStrArray) logs the error via klog.Errorf and safely skips the faulty device, allowing the plugin to continue operating with the remaining healthy devices on the node.

Which issue(s) this PR fixes:
Fixes #2230

Special notes for your reviewer:

  • Replaced all panic(0) calls with fmt.Errorf() returns and handled them at call sites.
  • I have added DCO sign-off (Signed-off-by) to the commit.

Does this PR introduce a user-facing change?:

Fix: the device plugin will no longer crash entirely if it encounters NVML utility failures while fetching device information.

Signed-off-by: shellyco-code <shellychahar57@gmail.com>
@hami-robot hami-robot Bot added kind/bug Something isn't working dco-signoff: yes labels Jul 31, 2026
@hami-robot

hami-robot Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: shellyco-code
Once this PR has been reviewed and has the lgtm label, please assign shouren for approval. For more information see the Kubernetes Code Review Process.

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 requested review from ouyangluwei163 and wawa0210 July 31, 2026 08:59
@github-actions github-actions Bot added kind/enhancement New feature or request and removed kind/bug Something isn't working labels Jul 31, 2026
@hami-robot hami-robot Bot added the size/M label Jul 31, 2026
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The change replaces panic-based NVML lookup failures with returned errors. Container-device conversion logs these errors and skips affected devices or MIG UUIDs.

Changes

NVML error handling

Layer / File(s) Summary
Return errors from NVML lookups
pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go
GetIndexAndTypeFromUUID and GetMigUUIDFromIndex return errors for NVML and fallback lookup failures.
Skip failed device conversions
pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go
GetContainerDeviceStrArray logs lookup errors and skips affected devices or MIG UUIDs.

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

Possibly related issues

Possibly related PRs

  • Project-HAMi/HAMi#2229 — Replaces panic-based GPU error handling with logging and graceful continuation in related functions.

Suggested reviewers: dsfans2014

Poem

A rabbit checks each GPU with care,
No panic leaps into the air.
Errors hop into the log,
Bad devices leave the fog.
Healthy cards continue bright.
NVML rests easy tonight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes replace NVML-related panics with logged errors and skipped devices, meeting issue #2233.
Out of Scope Changes check ✅ Passed The changes are limited to NVML error handling and device skipping required by issue #2233.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: replacing NVML utility panics with error returns.
✨ Finishing Touches
🧪 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 DSFans2014 July 31, 2026 08:59

@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: 2

🤖 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 `@pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go`:
- Around line 461-465: Update GetContainerDeviceStrArray so conversion failures
from GetIndexAndTypeFromUUID return an error instead of continuing with a
shortened device list; preserve skipping behavior only for device discovery
paths. Propagate this error through Allocate before calling getAllocateResponse,
ensuring failed conversion does not remove the pending allocation annotation or
mark the allocation successful.
- Around line 198-200: Update the fallback path around GetMigUUIDFromSmiOutput
to return a non-nil error when the resolved MIG UUID is empty, instead of
returning empty success. Preserve the existing successful return for non-empty
UUIDs so callers use their current error path to log and skip the device.
🪄 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: b8061e4d-447d-4627-8ddc-fa459597a22a

📥 Commits

Reviewing files that changed from the base of the PR and between 8fdd928 and 7befd57.

📒 Files selected for processing (1)
  • pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go

Comment on lines 198 to +200
outStr := stdout.String()
uuid := GetMigUUIDFromSmiOutput(outStr, originuuid, idx)
return uuid
return uuid, nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Return an error when the fallback does not find a MIG UUID.

GetMigUUIDFromSmiOutput returns "" when the requested MIG device is absent from the output. Lines 198-200 currently report that result as success. Line 505 then appends an empty device identifier.

Return a non-nil error when migUUID == "" so the existing error path logs and skips the device.

Proposed fix
-			uuid := GetMigUUIDFromSmiOutput(outStr, originuuid, idx)
-			return uuid, nil
+			migUUID := GetMigUUIDFromSmiOutput(outStr, originuuid, idx)
+			if migUUID == "" {
+				return "", fmt.Errorf("nvidia-smi -L returned no MIG UUID for GPU %q at index %d", originuuid, idx)
+			}
+			return migUUID, nil
📝 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
outStr := stdout.String()
uuid := GetMigUUIDFromSmiOutput(outStr, originuuid, idx)
return uuid
return uuid, nil
outStr := stdout.String()
migUUID := GetMigUUIDFromSmiOutput(outStr, originuuid, idx)
if migUUID == "" {
return "", fmt.Errorf("nvidia-smi -L returned no MIG UUID for GPU %q at index %d", originuuid, idx)
}
return migUUID, nil
🤖 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 `@pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go` around lines 198 -
200, Update the fallback path around GetMigUUIDFromSmiOutput to return a non-nil
error when the resolved MIG UUID is empty, instead of returning empty success.
Preserve the existing successful return for non-empty UUIDs so callers use their
current error path to log and skip the device.

Comment on lines +461 to +465
devtype, devindex, err := GetIndexAndTypeFromUUID(val.UUID)
if err != nil {
klog.Errorf("failed to get index and type from UUID %s: %v", val.UUID, err)
continue
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Fail allocation when conversion drops a requested device.

These continue statements shorten tmp, but GetContainerDeviceStrArray cannot report that loss. Allocate passes this result to getAllocateResponse, then removes the pending allocation annotation and marks the allocation successful after a nil response error. It does not compare the converted-device count with devreq.

Return an error from GetContainerDeviceStrArray when any requested device cannot convert. Propagate that error through Allocate. Keep device discovery-time skipping separate from allocation-time failure handling.

Also applies to: 500-505

🤖 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 `@pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go` around lines 461 -
465, Update GetContainerDeviceStrArray so conversion failures from
GetIndexAndTypeFromUUID return an error instead of continuing with a shortened
device list; preserve skipping behavior only for device discovery paths.
Propagate this error through Allocate before calling getAllocateResponse,
ensuring failed conversion does not remove the pending allocation annotation or
mark the allocation successful.

@mesutoezdil

Copy link
Copy Markdown
Contributor

the fix direction is ok but skipping a device in allocate silently under-allocates
the pod, which is worse than the panic

also the pr does not touch register.go which is what #2230 actually describes.

no ai assistance disclosure is present. if any ai tool was used, it must be disclosed per CONTRIBUTING.md: https://github.com/Project-HAMi/HAMi/blob/master/CONTRIBUTING.md#ai-assistance-notice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Device plugin crashes via panic(0) on NVML utility failures in util.go

2 participants