Skip to content

fix: return deep copies from ListPodsInfo to prevent data race - #2055

Merged
hami-robot[bot] merged 1 commit into
Project-HAMi:masterfrom
mesutoezdil:fix/list-pods-info-data-race
Jul 13, 2026
Merged

fix: return deep copies from ListPodsInfo to prevent data race#2055
hami-robot[bot] merged 1 commit into
Project-HAMi:masterfrom
mesutoezdil:fix/list-pods-info-data-race

Conversation

@mesutoezdil

@mesutoezdil mesutoezdil commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

ListPodsInfo returned raw *PodInfo pointers from the internal map. The read lock is released before callers in getNodesUsage read p.NodeID, p.Devices, and store the pointer in device state. A concurrent AddPod or UpdatePod can write through the same pointer at the same time, causing a data race.

PodInfo.DeepCopy() already existed. This PR uses it so callers get an independent snapshot. One line change.

Same class of race fixed in #1967.

Which issue(s) this PR fixes:

NONE

Special notes for your reviewer:

This change is scoped to the scheduler. Unit test added: TestListPodsInfoReturnsDeepCopy mutates the returned slice and checks the internal map is unchanged. Passes with -race.

Does this PR introduce a user-facing change?:

No

Summary by CodeRabbit

  • Bug Fixes
    • Improved pod information listing so returned data is isolated from internal records.
    • Changes made to listed pod details no longer unintentionally alter cached pod information.

@hami-robot
hami-robot Bot requested review from chaunceyjiang and lengrongfu July 10, 2026 12:20
@github-actions github-actions Bot added the kind/bug Something isn't working label Jul 10, 2026
@hami-robot hami-robot Bot added the size/S label Jul 10, 2026
@coderabbitai

coderabbitai Bot commented Jul 10, 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: e8b53de2-3916-4bd4-bc84-fd602bc35986

📥 Commits

Reviewing files that changed from the base of the PR and between c6449b6 and b307d79.

📒 Files selected for processing (2)
  • pkg/device/pod_test.go
  • pkg/device/pods.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • pkg/device/pods.go
  • pkg/device/pod_test.go

📝 Walkthrough

Walkthrough

PodManager.ListPodsInfo now returns deep copies of cached pod information, and a unit test verifies that mutations to returned top-level and nested fields do not affect the internal cache.

Changes

Pod listing isolation

Layer / File(s) Summary
Deep-copy pod listing results
pkg/device/pods.go, pkg/device/pod_test.go
ListPodsInfo deep-copies cached PodInfo entries, with coverage for mutations to returned NodeID and nested device UUID values.

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

Poem

I’m a rabbit guarding pods in a row,
Deep copies keep their secrets below.
Twist a UUID, change a node—
The cached pod stays safely sewn.
Hop, hop, isolation wins!

🚥 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 main change: returning deep copies from ListPodsInfo to prevent data races.
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.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request modifies ListPodsInfo in pkg/device/pods.go to return deep copies of PodInfo objects instead of direct references, preventing external mutation of the internal state. It also adds a corresponding unit test TestListPodsInfoReturnsDeepCopy in pkg/device/pod_test.go to verify this behavior. There are no review comments to address, so no further feedback is provided.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

ListPodsInfo was returning raw *PodInfo pointers from the internal map.
Callers access fields after the read lock is released, which races with
concurrent AddPod or UpdatePod calls. Use the existing DeepCopy method
so callers get an independent snapshot.

Signed-off-by: mesutoezdil <mesudozdil@gmail.com>
@mesutoezdil
mesutoezdil force-pushed the fix/list-pods-info-data-race branch from c6449b6 to b307d79 Compare July 10, 2026 12:22
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 59.81% <100.00%> (+0.31%) ⬆️

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

Files with missing lines Coverage Δ
pkg/device/pods.go 73.23% <100.00%> (+10.56%) ⬆️

... and 5 files with indirect coverage changes

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

@DSFans2014 DSFans2014 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@archlitchi archlitchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@hami-robot

hami-robot Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, mesutoezdil

The full list of commands accepted by this bot can be found here.

The pull request process is described 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

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