Skip to content

fix(device-plugin): prevent mutex pass-by-value in device replication - #2652

Closed
Rickydama3 wants to merge 1 commit into
Project-HAMi:masterfrom
Rickydama3:fix-mutex-copy
Closed

fix(device-plugin): prevent mutex pass-by-value in device replication#2652
Rickydama3 wants to merge 1 commit into
Project-HAMi:masterfrom
Rickydama3:fix-mutex-copy

Conversation

@Rickydama3

@Rickydama3 Rickydama3 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?
/kind bug

What this PR does / why we need it:
This fixes the go vet warnings regarding Mutex lock copying in the nvidiadevice RM package. By passing Device by pointer instead of value, we ensure the embedded mutex is not silently cloned, which would otherwise defeat synchronization.

AI Disclosure: I manually authored and reviewed this code, but I used an AI assistant to help me run go vet across the repository and draft the initial fix. The final implementation and understanding of the code is my own work.

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

Special notes for your reviewer:
Note: make verify currently fails on the master branch due to a pre-existing syntax error in .golangci.yaml (Error: can't load config: the format is required), but standard go vet passes cleanly on these changes.

Does this PR introduce a user-facing change?:

None

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **Bug Fixes**
  * Improved handling of replicated GPU devices by preserving device metadata and configuration.
  * Enhanced reliability when identifying devices, retrieving device identifiers, and determining supported allocation modes.
  * Fixed test-case handling to ensure allocation scenarios are evaluated against the correct data.

* **Tests**
  * Updated allocation coverage to better validate device-plugin responses.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Pass Device structs by pointer instead of by value across rm methods
and device_map replication to prevent copying the underlying Mutex.
Copying a Mutex by value silently spawns independent lock states,
creating race conditions that bypass synchronization.

This updates AlignedAllocationSupported, IsMigDevice, and GetUUID
to use pointer receivers, and explicitly initializes replicatedDevice
instead of shallow copying the struct in device_map.go.

Signed-off-by: Ricky Dama <rickydama2006@gmail.com>
@hami-robot hami-robot Bot added the kind/bug Something isn't working label Aug 13, 2026
@hami-robot

hami-robot Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Rickydama3
Once this PR has been reviewed and has the lgtm label, please assign fouof 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

@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 21622d09-54a2-4c9e-b496-6cef18aad0a1

📥 Commits

Reviewing files that changed from the base of the PR and between 1832393 and 627edfa.

📒 Files selected for processing (3)
  • pkg/device-plugin/nvidiadevice/nvinternal/plugin/server_test.go
  • pkg/device-plugin/nvidiadevice/nvinternal/rm/device_map.go
  • pkg/device-plugin/nvidiadevice/nvinternal/rm/devices.go

📝 Walkthrough

Walkthrough

The NVIDIA device manager now uses pointer receivers for Device helpers and explicitly constructs replicated devices without copying embedded lock state. The CDI allocation test binds each subtest to its indexed case.

Changes

Device lock copy prevention

Layer / File(s) Summary
Pointer-safe device operations and replicas
pkg/device-plugin/nvidiadevice/nvinternal/rm/devices.go, pkg/device-plugin/nvidiadevice/nvinternal/rm/device_map.go
Device helper methods now use pointer receivers. Replicated devices explicitly preserve device metadata while assigning replica-specific fields.
CDI allocation test binding
pkg/device-plugin/nvidiadevice/nvinternal/plugin/server_test.go
Each subtest now references the indexed table case through a pointer.

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

Mergeability Score: ⚪ Minimal · up to 627ed

This localized change prevents mutex state from being copied during device replication, with no actionable merge-blocking risk remaining beyond normal checks and review.

Possibly related PRs

Suggested reviewers: mesutoezdil

Poem

I’m a rabbit guarding each device’s state,
No copied locks shall duplicate their fate.
Replicas keep the fields they need,
Test cases follow pointers with speed.
Safe hops through NVIDIA’s code—
One tidy fix along the road.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The server_test.go subtest binding change is unrelated to the mutex-copying objectives in issue #2651. Move the unrelated server_test.go change to a separate pull request or link an issue that requires this test correction.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the mutex pass-by-value fix in device replication.
Linked Issues check ✅ Passed The changes implement the requested pointer receivers and explicit replicated-device construction for issue #2651.
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 requested a review from mesutoezdil August 13, 2026 20:29
@mesutoezdil

Copy link
Copy Markdown
Contributor

The vet warning is from protobuf's zero-size DoNotCopy marker, not a real lock,
so there's no actual bug here
closing.

@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...evice-plugin/nvidiadevice/nvinternal/rm/devices.go 66.66% 1 Missing ⚠️
Flag Coverage Δ
unittests 62.58% <93.33%> (+0.19%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ce-plugin/nvidiadevice/nvinternal/rm/device_map.go 39.77% <100.00%> (+3.24%) ⬆️
...evice-plugin/nvidiadevice/nvinternal/rm/devices.go 50.35% <66.66%> (ø)

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

kind/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Mutex lock state copied by value in device-plugin RM package

2 participants