Skip to content

fix(cambricon): prevent node lock leaks by reading from apiserver in ReleaseNodeLock - #2329

Merged
hami-robot[bot] merged 1 commit into
Project-HAMi:masterfrom
jianzhangbjz:2251
Aug 13, 2026
Merged

fix(cambricon): prevent node lock leaks by reading from apiserver in ReleaseNodeLock#2329
hami-robot[bot] merged 1 commit into
Project-HAMi:masterfrom
jianzhangbjz:2251

Conversation

@jianzhangbjz

@jianzhangbjz jianzhangbjz commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:
Fix three bugs in ReleaseNodeLock:

  • setNodeLock patches the apiserver without updating the caller's in-memory Node, causing ReleaseNodeLock to read stale state and leak locks
  • the retry loop reused the same DeepCopy without re-fetching resourceVersion, causing 409 conflicts to never resolve
  • delete on the shared informer's annotation map raced with concurrent readers

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

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

Summary by CodeRabbit

  • Bug Fixes

    • Improved node lock acquisition and release using the latest node state.
    • Added safer handling for concurrent updates, already-removed locks, stale state, and temporary retrieval failures.
    • Prevented unrelated node annotations from being affected when releasing a lock.
    • Improved error reporting when node state cannot be retrieved or updated.
  • Tests

    • Expanded coverage for lock creation, removal, missing locks, stale state, concurrent changes, and retry scenarios.

@hami-robot hami-robot Bot added kind/bug Something isn't working dco-signoff: no labels Aug 4, 2026
@hami-robot

hami-robot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

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

@hami-robot hami-robot Bot added the size/L label Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 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
📝 Walkthrough

Walkthrough

Cambricon node lock acquisition now patches the lock annotation without relying on stale local state. Lock release fetches the current node, retries with refreshed resource versions, and handles concurrent lock removal. Fake-client tests cover these behaviors.

Changes

Cambricon node lock lifecycle

Layer / File(s) Summary
Lock annotation acquisition
pkg/device/cambricon/device.go, pkg/device/cambricon/device_test.go
setNodeLock always prepares and patches the lock annotation. Tests verify the persisted annotation through the API server.
Fresh-state lock release
pkg/device/cambricon/device.go, pkg/device/cambricon/device_test.go
ReleaseNodeLock fetches current nodes, uses deep copies for updates, re-fetches after conflicts, reports fetch failures, and handles already-removed locks. Tests cover missing nodes, stale callers, conflicts, concurrent removal, and retry fetch failures.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CambriconDevices
  participant KubernetesAPIServer
  participant ConcurrentActor
  CambriconDevices->>KubernetesAPIServer: Fetch current node
  CambriconDevices->>KubernetesAPIServer: Remove lock annotation
  KubernetesAPIServer-->>CambriconDevices: Return update conflict
  CambriconDevices->>KubernetesAPIServer: Re-fetch node
  ConcurrentActor->>KubernetesAPIServer: Remove lock annotation
  KubernetesAPIServer-->>CambriconDevices: Return node without lock
Loading

Possibly related issues

Possibly related PRs

Suggested reviewers: lengrongfu, chaunceyjiang

Poem

A rabbit fetches the node with care,
Fresh locks wait in API air.
Conflicts trigger retries anew,
Removed locks leave fields clean and true.
Tests check each path in sight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main fix: reading from the apiserver in ReleaseNodeLock to prevent node lock leaks.
✨ 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 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/device/cambricon/device_test.go (1)

590-616: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add a stale-caller regression case.

This test passes a Node whose annotations match the persisted Node. It does not verify the fresh-state behavior in ReleaseNodeLock.

Create a persisted Node with DsmluLockTime. Call ReleaseNodeLock with a separate Node that has only the same name and no lock annotation. Then verify that the persisted lock annotation is removed.

🤖 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/cambricon/device_test.go` around lines 590 - 616, The
ReleaseNodeLock test currently passes the persisted Node instead of a stale
caller object, so it does not cover fresh-state behavior. Add a regression case
that persists a Node with DsmluLockTime, invokes
CambriconDevices.ReleaseNodeLock with a separate Node containing only the same
name and no annotations, and verifies via a fresh API read that the persisted
lock annotation was removed.
🤖 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/cambricon/device_test.go`:
- Around line 590-616: The ReleaseNodeLock test currently passes the persisted
Node instead of a stale caller object, so it does not cover fresh-state
behavior. Add a regression case that persists a Node with DsmluLockTime, invokes
CambriconDevices.ReleaseNodeLock with a separate Node containing only the same
name and no annotations, and verifies via a fresh API read that the persisted
lock annotation was removed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 01310aee-cc66-463a-bd15-ae18708e105e

📥 Commits

Reviewing files that changed from the base of the PR and between b55ab85 and a3df169.

📒 Files selected for processing (2)
  • pkg/device/cambricon/device.go
  • pkg/device/cambricon/device_test.go

@mesutoezdil

Copy link
Copy Markdown
Contributor

dco check fails, missing signed-off-by. pls sign off your commit and push again

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

Comment thread pkg/device/cambricon/device.go Outdated
@archlitchi

Copy link
Copy Markdown
Member

/assign

@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.14286% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/device/cambricon/device.go 82.14% 4 Missing and 1 partial ⚠️
Flag Coverage Δ
unittests 62.43% <82.14%> (-1.67%) ⬇️

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

Files with missing lines Coverage Δ
pkg/device/cambricon/device.go 87.50% <82.14%> (+4.23%) ⬆️

... and 37 files with indirect coverage changes

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

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

archlitchi
archlitchi previously approved these changes Aug 7, 2026

@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

@archlitchi

Copy link
Copy Markdown
Member

/lgtm cancel

@hami-robot hami-robot Bot removed the lgtm label Aug 7, 2026
@archlitchi

Copy link
Copy Markdown
Member

please sync with master to pass the UT

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@jianzhangbjz

Copy link
Copy Markdown
Contributor Author

Hi @archlitchi , I've updated it. Could you help approve it when you get a chance? Thanks!

Comment thread pkg/device/cambricon/device.go Outdated
Comment thread pkg/device/cambricon/device.go Outdated
@mesutoezdil

Copy link
Copy Markdown
Contributor

fixes line has a bad format: "fixes ##2251". github wont auto close the issue like this. can you change it to just "fixes #2251"?

Comment thread pkg/device/cambricon/device.go Outdated
- setNodeLock patches the apiserver without updating the caller's in-memory
  Node, causing ReleaseNodeLock to read stale state and leak locks
- the retry loop reused the same DeepCopy without re-fetching
  resourceVersion, causing 409 conflicts to never resolve
- delete on the shared informer's annotation map raced with concurrent
  readers

Signed-off-by: Jian Zhang <jiazha666@gmail.com>
@jianzhangbjz

Copy link
Copy Markdown
Contributor Author

Hi @mesutoezdil , I've updated it. Could you help have a review? Thanks!

@Shouren Shouren 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 added the lgtm label Aug 13, 2026
@hami-robot

hami-robot Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, jianzhangbjz, Shouren

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

@hami-robot
hami-robot Bot merged commit 2008d2b into Project-HAMi:master Aug 13, 2026
16 checks passed
@jianzhangbjz
jianzhangbjz deleted the 2251 branch August 13, 2026 02:49
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.

Cambricon ReleaseNodeLock can return nil without removing the node lock

4 participants