Skip to content

fix: resolve go vet sync.Mutex copy-by-value errors - #2169

Closed
Norway-02 wants to merge 3 commits into
Project-HAMi:masterfrom
Norway-02:fix-go-vet-lock-copy
Closed

fix: resolve go vet sync.Mutex copy-by-value errors#2169
Norway-02 wants to merge 3 commits into
Project-HAMi:masterfrom
Norway-02:fix-go-vet-lock-copy

Conversation

@Norway-02

@Norway-02 Norway-02 commented Jul 28, 2026

Copy link
Copy Markdown

What type of PR is this?
/kind bug

What this PR does / why we need it:
This PR resolves several go vet violations where a sync.Mutex was inadvertently copied by value.

In devices.go, passing Device by value in receiver methods triggered a lock copy of the embedded protobuf MessageState mutex. I've updated these to use pointer receivers.

In device_map.go, updateDeviceMapWithReplicas was dereferencing the device struct to create a replica. I replaced this with an explicit struct initialization that safely copies fields without copying the embedded mutex.

In server_test.go, the loop assigned tc := testCases[i]. Because expectedResponse is a ContainerAllocateResponse containing a protobuf mutex, this triggered go vet. I changed this to a pointer reference tc := &testCases[i].

These changes ensure go vet ./... passes cleanly on master.

Which issue(s) this PR fixes:
NONE

Special notes for your reviewer:
Note: This PR was prepared with AI assistance; the solution was reviewed and tested manually.

Does this PR introduce a user-facing change?:

NONE

Summary by CodeRabbit

  • Bug Fixes
    • Improved replicated device handling so replicas consistently preserve health, topology, paths, memory, and compute capability details.
    • Improved device allocation checks for more reliable capability and UUID handling.
  • Tests
    • Expanded replication coverage to validate complete device details.
    • Improved test case handling for more consistent per-case assertions.

@hami-robot hami-robot Bot added kind/bug Something isn't working dco-signoff: no labels Jul 28, 2026
@hami-robot
hami-robot Bot requested review from FouoF and wawa0210 July 28, 2026 10:03
@hami-robot

hami-robot Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Norway-02
Once this PR has been reviewed and has the lgtm label, please assign wawa0210 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 28, 2026

Copy link
Copy Markdown
Contributor

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

@hami-robot hami-robot Bot added the size/S label Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 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

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: b576f429-22d5-4ffa-81df-8da74837c4d3

📥 Commits

Reviewing files that changed from the base of the PR and between a01f782 and b09689c.

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

📝 Walkthrough

Walkthrough

The change updates three Device methods to pointer receivers, preserves protobuf data when constructing replicas, expands replication assertions, and references CDI test cases by pointer.

Changes

Device replica updates

Layer / File(s) Summary
Device method receiver updates
pkg/device-plugin/nvidiadevice/nvinternal/rm/devices.go
AlignedAllocationSupported, IsMigDevice, and GetUUID now use pointer receivers.
Explicit replicated device construction and validation
pkg/device-plugin/nvidiadevice/nvinternal/rm/device_map.go, pkg/device-plugin/nvidiadevice/nvinternal/rm/device_map_test.go, pkg/device-plugin/nvidiadevice/nvinternal/plugin/server_test.go
Replica construction clones protobuf data and copies selected fields. Tests validate complete replicated devices. CDI test cases use pointers.

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

Possibly related PRs

Suggested reviewers: wawa0210

Poem

A rabbit checks each replica’s fields,
While cloned protobuf data yields.
Pointer methods guide the way,
CDI cases test today.
Complete device facts remain.

🚥 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 and concisely describes the primary change: fixing go vet errors caused by copying sync.Mutex values.
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
🧪 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 July 28, 2026 10:04
Signed-off-by: Norway-02 <anshulkhetade02@gmail.com>
@Norway-02

Norway-02 commented Jul 28, 2026

Copy link
Copy Markdown
Author

cc @FouoF @archlitchi Please review this PR when you have a moment. Thank you!

@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.

TestUpdateDeviceMapWithReplicas only sets ID and Index, so it stays green even if one of the hand copied fields gets dropped, worth extending.

replicatedDevice.Replicas = r.Replicas
devices.insert(name, &replicatedDevice)
orig := oDevices[r.Name][id]
replicatedDevice := &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.

hand listing fields means a new field on Device or on kubelets v1beta1.Device silently stops reaching replicas. did u look at proto.Clone for the embedded part?

Signed-off-by: Norway-02 <anshulkhetade02@gmail.com>
@Norway-02

Norway-02 commented Jul 29, 2026

Copy link
Copy Markdown
Author

Thank you for the feedback! I've pushed a follow-up that addresses both points.

I replaced the manual Protobuf field copy with proto.Merge(). It avoids the go vet copylock warning and also takes care of any new fields added to v1beta1.Device in the future.

I also expanded TestUpdateDeviceMapWithReplicas to cover all the fields. I had to switch the assertions for the embedded structs from require.EqualValues to proto.Equal because of the internal state populated by proto.Merge().

I've run go vet and go test for the rm package locally, and both are passing. Thank you!

proto.Merge on a fresh zero-value proto works, but proto.Clone is more
explicit — it returns a complete deep copy of the embedded kubelet
Device in a single call, making the intent clear and removing the
two-step initialize-then-merge pattern.

Signed-off-by: Norway-02 <anshulkhetade02@gmail.com>
@Norway-02

Copy link
Copy Markdown
Author

@mesutoezdil @archlitchi @wawa0210 PTAL when you have a chance — the last round of feedback (use proto.Clone for the embedded Device) has been addressed. Thank you!

@mesutoezdil

Copy link
Copy Markdown
Contributor

Closing: after 3 commits the core go vet error in device_map.go still fails (the proto.Clone dereference still copies the embedded sync.Mutex), and real CI (Compile/Unit test/lint) has never run on this branch.

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