Skip to content

fix: skip faulty devices instead of panicking during registration - #2231

Closed
shellyco-code wants to merge 2 commits into
Project-HAMi:masterfrom
shellyco-code:fix-register-panic
Closed

fix: skip faulty devices instead of panicking during registration#2231
shellyco-code wants to merge 2 commits into
Project-HAMi:masterfrom
shellyco-code:fix-register-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 reliability issue where the HAMi device plugin crashes via panic(0) if a single GPU fails NVML queries during the buildDeviceMap registration loop (e.g. if the driver or hardware is in a bad state).

Instead of panicking and crashing the entire plugin, the loop now logs the error via klog.Errorln and safely uses continue to skip the faulty device, allowing the plugin to successfully register the remaining healthy GPUs on the node.

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

Special notes for your reviewer:

This replaces all panic(0) calls with continue in the registration loop.
I have added DCO sign-off (Signed-off-by) to the commit.
Does this PR introduce a user-facing change?:

Note :This PR was created with the assistance of an AI coding agent.

release-note

Fix: the device plugin will no longer crash entirely if a single GPU is faulty or fails NVML queries during initialization.

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability of NVIDIA device plugin during device discovery; query failures no longer cause crashes and are gracefully handled by skipping affected devices.
    • Enhanced error reporting with warnings emitted when device discovery results in an empty device list.

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 added the size/XS label Jul 31, 2026

@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 add a unit test covering the skip path.

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

Comment thread pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go
Comment thread pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go
@shellyco-code

Copy link
Copy Markdown
Author

@mesutoezdil getAPIDevices() directly calls nvml.Init(), which panics in CI (no GPU) so we can't test the skip path unless we refactor the plugin to use a mockable nvml.Interface. Should I include that refactor in this PR, or skip the test?

Signed-off-by: shellyco-code <shellychahar57@gmail.com>
@hami-robot hami-robot Bot added size/S and removed size/XS labels Jul 31, 2026
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

NVIDIA device registration now logs NVML query failures, skips affected devices, and continues discovery. It also warns when all discovered devices are skipped and the device list is empty.

Changes

NVIDIA device discovery

Layer / File(s) Summary
Skip failed devices during registration
pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go
Device-index, memory, and device-name query failures now log errors and continue discovery. A warning is emitted when no devices remain.

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

Possibly related issues

Possibly related PRs

Suggested reviewers: ouyangluwei163, lengrongfu

Poem

A rabbit hops past GPUs in line,
“Skip the faulty one; the rest are fine.”
NVML errors now get logged,
No plugin panic leaves it bogged.
Empty lists warn before twilight.

🚥 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 describes the primary change: skipping faulty devices instead of panicking during registration.
Linked Issues check ✅ Passed The changes log NVML failures and skip faulty GPUs, which meets issue #2233 by allowing healthy GPUs to register.
Out of Scope Changes check ✅ Passed All reported changes support the linked issue by improving device registration resilience against NVML failures.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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 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

🧹 Nitpick comments (1)
pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go (1)

111-114: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Include the device UUID in skip logs.

The GetIndex() and GetName() error logs include only ret. When multiple GPUs fail, operators cannot identify the skipped device. Include UUID in both messages.

Suggested log context
- klog.Errorln("nvml get index error ret=", ret)
+ klog.Errorln("nvml get index error uuid=", UUID, "ret=", ret)

- klog.Error("nvml get name error ret=", ret)
+ klog.Error("nvml get name error uuid=", UUID, "ret=", ret)

Also applies to: 139-142

🤖 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/register.go` around lines
111 - 114, Update the error logs in the device registration flow around
GetIndex() and GetName() to include the corresponding device UUID alongside ret,
while preserving the existing continue behavior for both failures.
🤖 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/register.go`:
- Around line 109-114: Replace the panic in getAPIDevices() when nvml.Init()
fails with error propagation, then have RegisterInAnnotation() return that error
and update WatchAndRegister() to handle it by retrying while preserving the last
valid annotation. Add coverage for NVML initialization failure and individual
device failure paths.

---

Nitpick comments:
In `@pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go`:
- Around line 111-114: Update the error logs in the device registration flow
around GetIndex() and GetName() to include the corresponding device UUID
alongside ret, while preserving the existing continue behavior for both
failures.
🪄 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: 40ea26ec-aa02-41fc-9fc5-e3d5a320c0a7

📥 Commits

Reviewing files that changed from the base of the PR and between 8fdd928 and 4efc068.

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

Comment on lines +109 to +114
continue
}
idx, ret := ndev.GetIndex()
if ret != nvml.SUCCESS {
klog.Errorln("nvml get index error ret=", ret)
panic(0)
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.

🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -n -C 6 'nvml\.Init\(\)|panic\(0\)|getAPIDevices|RegisterInAnnotation' \
  pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go \
  pkg/device-plugin/nvidiadevice/nvinternal/plugin/register_test.go

Repository: Project-HAMi/HAMi

Length of output: 4931


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== register.go relevant sections ==="
sed -n '89,200p' pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go | cat -n | sed 's/^/register.go /'
sed -n '250,285p' pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go | cat -n | sed 's/^/register.go /'

echo "=== occurrences of nvidia.Init() / nvml.Init() / getAPIDevices / WatchAndRegister / RegisterInAnnotation ==="
rg -n -C 3 'nvml\.Init\(|\.Init\(\)|getAPIDevices|WatchAndRegister|RegisterInAnnotation|initInterface|Nvml' pkg/device-plugin/nvidiadevice/nvinternal/pkg pkg/device-plugin/nvidiadevice/nvinternal/plugin -g '*.go'

Repository: Project-HAMi/HAMi

Length of output: 27427


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== RegisterInAnnotation body ==="
sed -n '197,245p' pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go | cat -n | sed 's/^/register.go /'

echo "=== factory init handling ==="
sed -n '90,130p' pkg/device-plugin/nvidiadevice/nvinternal/plugin/factory.go | cat -n | sed 's/^/factory.go /'

echo "=== Read-only structural check: getAPIDevices returns pointer and RegisterInAnnotation propagates no error from getAPIDevices ==="
python3 - <<'PY'
from pathlib import Path
p = Path('pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go')
s = p.read_text()
print('signature:', 'func (plugin *NvidiaDevicePlugin) getAPIDevices() *[]*device.DeviceInfo {' in s)
print('getAPIDevices returns &res:', '&res' in s[s.index('func (plugin *NvidiaDevicePlugin) getAPIDevices'):s.index('// RegisterInAnnotation')])
check = 'devices := plugin.getAPIDevices()'
idx = s.index(check)
block = s[idx:idx+400]
print('RegisterInAnnotation calls getAPIDevices:', True)
print('RegisterInAnnotation immediately accesses result:', '*devices' in block or 'len(*devices)' in block)
print('RegisterInAnnotation has explicit error from getAPIDevices:', any(tok in block for tok in [':= getAPIDevices', '= getAPIDevices', 'return nil']))
print('current RegisterInAnnotation return:', 'return true, err' in block or 'return false, err' in block)
PY

Repository: Project-HAMi/HAMi

Length of output: 5559


Handle nvml.Init() failures from getAPIDevices() without panicking.

getAPIDevices() still calls panic(0) when nvml.Init() fails, which crashes WatchAndRegister and can skip the node annotation path. Return this error through RegisterInAnnotation() and call it from WatchAndRegister() so WatchAndRegister() retries without replacing the last valid annotation. Add tests for NVML initialization and per-device failure paths.

🤖 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/register.go` around lines
109 - 114, Replace the panic in getAPIDevices() when nvml.Init() fails with
error propagation, then have RegisterInAnnotation() return that error and update
WatchAndRegister() to handle it by retrying while preserving the last valid
annotation. Add coverage for NVML initialization failure and individual device
failure paths.

@Shouren

Shouren commented Aug 4, 2026

Copy link
Copy Markdown
Member

explained in #2233

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.

Bug: Device plugin crashes via panic(0) during registration if a single GPU fails NVML queries

3 participants