fix(device): return deep copied pods in GetScheduledPods to prevent data race - #2501
fix(device): return deep copied pods in GetScheduledPods to prevent data race#2501aryansri05 wants to merge 2 commits into
Conversation
…ata race GetScheduledPods previously returned a shallow copy of the pods map. Since the values are *PodInfo pointers, the underlying structs were still shared across threads, causing a data race between metrics scraping and scheduler write updates. We now call v.DeepCopy() on the values in the map to return fully independent structs. Signed-off-by: Aryan Srivastava <your_github_email@example.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: aryansri05 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (1)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthrough
ChangesScheduled pod data isolation
Estimated code review effort: 1 (Trivial) | ~5 minutes Possibly related issues
Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/device/pods.go (1)
241-243: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick winAdd a regression test for copy isolation.
The current
TestGetScheduledPodstest checks only value equality. It would also pass ifGetScheduledPodsreturned shared*PodInfopointers. Mutate the returnedPod,Devicesmap, and nested device slice, then assert that the manager’s storedPodInforemains unchanged.🤖 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/pods.go` around lines 241 - 243, Extend TestGetScheduledPods to verify deep-copy isolation, not just value equality. After retrieving the result from GetScheduledPods, mutate the returned Pod, Devices map, and nested device slice, then assert the manager’s stored PodInfo and all nested data remain unchanged.
🤖 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.
Nitpick comments:
In `@pkg/device/pods.go`:
- Around line 241-243: Extend TestGetScheduledPods to verify deep-copy
isolation, not just value equality. After retrieving the result from
GetScheduledPods, mutate the returned Pod, Devices map, and nested device slice,
then assert the manager’s stored PodInfo and all nested data remain unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 887c5af8-4544-4317-83e2-3468c9fb1c75
📒 Files selected for processing (1)
pkg/device/pods.go
|
Thanks for your pull request. Before we can look at it, you'll need to add a 'DCO signoff' to your commits. 📝 Please follow instructions in the contributing guide to update your commits with the DCO Full details of the Developer Certificate of Origin can be found at developercertificate.org. The list of commits missing DCO signoff:
DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
|
You can view the relevant rule here. |
|
Apologies for not following the contribution guidelines — I should have disclosed AI assistance upfront and checked for existing PRs before opening this. Won't happen again. |
Fixes #2471
Currently,
GetScheduledPods()returns a shallow copy of the pods map viamaps.Copy. While this prevents concurrent map iteration and map write panics on the map itself, the map values are pointers to*PodInfo. As a result, the underlying struct pointers are still shared across threads.When the metrics collector reads the scheduled pods concurrently, it races with scheduling routines (like
AddPodorUpdatePod) mutating these same structs.This PR fixes the data race by iterating and calling
v.DeepCopy()on thePodInfopointers, returning fully independent structs.Before:
After:
Summary by CodeRabbit