fix(util): re-evaluate lock state in optimistic locking retry loop - #2224
fix(util): re-evaluate lock state in optimistic locking retry loop#2224Norway-02 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Norway-02 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 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughNode lock acquisition and release now refresh lock state during retries. Acquisition accepts an existing lock owned by the requester and stops on contention. Release aborts when ownership changes and treats the abort as successful. ChangesNode lock retry handling
Estimated code review effort: 2 (Simple) | ~15 minutes 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 |
4f9b1e3 to
83845fd
Compare
|
@DSFans2014 Hi! I've opened this PR to fix a critical concurrency race condition in the distributed node locking mechanism ( |
There was a problem hiding this comment.
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
The nodelock SetNodeLock and ReleaseNodeLock functions blindly applied patches inside their retry.OnError blocks upon conflict, skipping lock state validation. This caused distributed concurrent updates (e.g. from multiple scheduler instances) to blindly overwrite each other's locks rather than aborting. This commit updates the retry loops to explicitly re-evaluate lock state after fetching the node during a conflict retry, ensuring robust distributed mutual exclusion. Signed-off-by: Norway-02 <anshulkhetade02@gmail.com>
83845fd to
79302a8
Compare
|
@mesutoezdil Thanks for the review! All feedback has been addressed:
PTAL when you have a moment. Thanks! |
|
closing per the comparison above, #2197 is the earlier and more complete base. pls check existing prs before opening a new one. |
fix(util): re-evaluate lock state in optimistic locking retry loop
What happened
The HAMi scheduler and device plugins rely on
pkg/util/nodelockto implement a distributed node lock using Kubernetes node annotations (hami.io/mutex.lock).Currently,
SetNodeLockandReleaseNodeLockcorrectly use an optimistic locking pattern viaretry.OnError, but the condition checking occurs outside the retry function. Inside theretry.OnErrorblock, the code blindly fetches the newresourceVersionand patches the node without evaluating whether the lock is still available (or still owned by the caller).This leads to a race condition where concurrent scheduler instances blindly overwrite each other's locks upon a patch conflict.
What you expected to happen
The
retry.OnErrorloop should explicitly re-evaluate the lock state after fetching the node (inside the loop). If the node is already locked by another process (forSetNodeLock) or the lock is no longer owned by the current pod (forReleaseNodeLock), the retry loop should abort rather than blindly applying the patch.How to reproduce it (as minimally and precisely as possible)
SetNodeLockconcurrently for the same node.retry.OnErrortriggers, fetches the node, and unceremoniously overwrites the first instance's lock because it fails to checknode.Annotations[NodeLockKey]inside the retry block.Environment
--
In a perfect world, AI assistance would produce equal or higher quality
work than any human. That isn't the world we live in today, and in many cases
AI-generated code can contain subtle bugs or not adhere to project-specific
best practices. I say this despite being a fan of and using them successfully
myself (with heavy supervision)!
When using AI assistance, we expect contributors to understand the code
that is produced and be able to answer critical questions about it. It
isn't a maintainer's job to review a PR so broken that it requires
significant rework to be acceptable.
Please be respectful to maintainers and disclose AI assistance.
AI Assistance Disclosure: This PR was identified and written with the assistance of an AI agent. The AI was used to identify the concurrency race condition within the optimistic locking retry logic, write the fix, and draft this PR description. The solution was fully reviewed, vetted, and manually verified.
Summary by CodeRabbit