Skip to content

fix(plugin): guard mig smi output parsing against malformed lines - #2246

Closed
AdityaShome wants to merge 5 commits into
Project-HAMi:masterfrom
AdityaShome:mig-smi-output-index-panic
Closed

fix(plugin): guard mig smi output parsing against malformed lines#2246
AdityaShome wants to merge 5 commits into
Project-HAMi:masterfrom
AdityaShome:mig-smi-output-index-panic

Conversation

@AdityaShome

@AdityaShome AdityaShome commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

GetMigUUIDFromSmiOutput (nvidia-smi fallback for MIG UUID lookup) indexed into strings.Split results with no bounds checking, so any MIG-block line not matching the exact expected shape caused an index out of range panic instead of being skipped turning a recoverable NVML error into a device plugin crash. Same general pattern as the scheduler-side fix in #2088 (unguarded indexing while parsing a MIG index/UUID string) a different component and code path, not otherwise related.

This PR bounds-checks both splits and skips the malformed line instead of indexing blindly.

Not raised issue as it is a small fix.

Special notes for your reviewer:

  • Reproduced the panic first (crafted nvidia-smi -L input missing the expected "Device" keyword), then fixed it. Regression tests added covering the happy path and both previously-panicking malformed cases.
  • Not validated on physical MIG hardware, verified via unit tests reproducing the malformed-output panic and its fix, plus go build/go vet/go test -race. Diff: 10+/4- (fix) + 40+ (tests).

Does this PR introduce a user-facing change?:

Fix a panic in the NVIDIA device plugin's nvidia-smi-based MIG UUID fallback lookup when the parsed output line doesn't match the expected format.

AI assistance disclosure: AI assistance was used to help trace the fallback path, reproduce the panic and suggest an initial fix. I manually investigated the issue, validated the root cause, refined and implemented the solution, verified the behavior, added the tests and reviewed all changes before opening this PR.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of malformed GPU management output during MIG UUID lookup.
    • Invalid device indices, incomplete lines, and entries without valid MIG UUIDs are now safely skipped.
    • Valid device entries continue to be processed as expected, reducing errors in GPU device detection.
  • Tests

    • Added coverage for successful MIG UUID lookup, malformed device lines, non-numeric indices, and incomplete output.

@hami-robot hami-robot Bot added the kind/bug Something isn't working label 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: AdityaShome
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 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot removed the kind/bug Something isn't working label 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

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

GetMigUUIDFromSmiOutput validates NVIDIA SMI lines before field access. GetContainerDeviceStrArray skips MIG devices with unresolved UUIDs. Tests cover valid and malformed input.

Changes

MIG UUID parsing

Layer / File(s) Summary
Parser validation and regression tests
pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go, pkg/device-plugin/nvidiadevice/nvinternal/plugin/util_test.go
The parser validates device indices and UUID fields before extraction. Tests cover valid lookup, malformed lines, non-numeric indices, and missing UUID fields.
Container device conversion guard
pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go
Container device conversion logs an error and skips a MIG device when UUID resolution fails.

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

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: archlitchi

Poem

A rabbit checks each MIG line,
Bad fields stop before the sign.
Good UUIDs pass the gate,
Empty results wait outside the state.
Hop, hop—safe parsing is great!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the fix for malformed MIG SMI output parsing.
✨ 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 review from archlitchi and mesutoezdil July 31, 2026 18:38

@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 175-179: Update GetMigUUIDFromSmiOutput to validate that the
parsed field is the UUID field and that its trimmed value is non-empty and
starts with “MIG-” before returning it; otherwise skip the malformed line and
continue scanning. Add a regression test covering a line with enough
colon-separated parts but no “(UUID” field, verifying it is ignored.
- Around line 164-167: Update the device-index parsing flow around strings.Split
and strconv.Atoi so non-numeric indexes are logged at a non-fatal level and
skipped instead of reaching klog.Fatal or terminating the process. Preserve
processing of valid device indexes, and add a regression test covering a line
such as “Device x” to verify parsing continues without exiting.
🪄 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: c87df672-830c-4c95-b0ab-66670bc8f0bd

📥 Commits

Reviewing files that changed from the base of the PR and between c7891de and 741203a.

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

Comment thread pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go
Comment thread pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go
@AdityaShome

Copy link
Copy Markdown
Contributor Author
  1. Non numeric device index now logs and skips the line instead of calling klog.Fatal and killing the process.
  2. Returned value now requires a UUID field and MIG prefix, so a line without a real UUID is skipped instead of returned as is.

Signed-off-by: AdityaShome <shomeaditya65@gmail.com>
Signed-off-by: AdityaShome <shomeaditya65@gmail.com>
Signed-off-by: AdityaShome <shomeaditya65@gmail.com>
@AdityaShome

Copy link
Copy Markdown
Contributor Author

Last 3 commits are just adding signoff by rebase.

@mesutoezdil mesutoezdil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pls rename the title to the conventional format, sth like "fix(plugin): guard mig smi output parsing against malformed lines".

Comment thread pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go
Signed-off-by: AdityaShome <shomeaditya65@gmail.com>
@AdityaShome

Copy link
Copy Markdown
Contributor Author

It now skips appending and logs an error when the MIG UUID lookup returns empty.

@AdityaShome AdityaShome changed the title Mig smi output index panic fix(plugin): guard mig smi output parsing against malformed lines Aug 1, 2026
@AdityaShome

Copy link
Copy Markdown
Contributor Author

Made GetMigUUIDFromIndex and GetContainerDeviceStrArray return an error on unresolved MIG UUID instead of skipping, so Allocate() now fails hard instead of under allocating, validated on real GPU hardware (RTX 3050, driver 592.82, MIG unsupported), confirming the error path triggers and fails the request as expected.

@archlitchi

Copy link
Copy Markdown
Member

/assign @mesutoezdil

@AdityaShome

Copy link
Copy Markdown
Contributor Author

A small follow up, so it does not go stale.

@mesutoezdil

Copy link
Copy Markdown
Contributor

ok error propagation looks good now, getmiguuidfromindex returns err, getcontainerdevicestrarray passes it up, allocate fails hard instead of quietly under allocating. nice that u tested on real hw too.

only thing left, pr conflicts w master now. #2378 refactored mig alloc in the same files a few days ago. pls rebase and make sure ur fix still applies clean on top.

klog.Infoln("inspecting", val)
num := strings.Split(val, "Device")[1]
num = strings.Split(num, ":")[0]
deviceParts := strings.Split(val, "Device")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

getmiguuidfromsmioutput and getmiguuidfromindex are gone in master now. #2378 replaced this whole nvidia-smi fallback with nvml miginstancemanager. pls check if the bug still exists in new code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great work on the new MIG design overall, while going through the code I found two smaller issues:

  1. gpuUUIDToIndex and nvmlBusyGPUs (mig_startup.go) call nvml.Init() with no matching nvml.Shutdown() unlike every other NVML entry point in the codebase. gpuUUIDToIndex runs on every Allocate() and every 5s via the reconciler, so this leaks indefinitely on nodes with active MIG allocations.
  2. migmgr.go's MigInstanceManager never calls nvml.Init() itself it silently relies on nvmlBusyGPUs() having already leaked one at startup. Fixing 1 by adding a proper Shutdown() would break this.

Planning two follow-up PRs for these.

@mesutoezdil

Copy link
Copy Markdown
Contributor

This is being closed because it does not comply with the contribution guidelines.

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.

3 participants