Skip to content

fix: add missing lock protection for LRU cache policy #2860

Merged
DongDongJu merged 4 commits intoLMCache:devfrom
SYaoJun:fix_lru_lock
Mar 27, 2026
Merged

fix: add missing lock protection for LRU cache policy #2860
DongDongJu merged 4 commits intoLMCache:devfrom
SYaoJun:fix_lru_lock

Conversation

@SYaoJun
Copy link
Copy Markdown
Contributor

@SYaoJun SYaoJun commented Mar 24, 2026

What this PR does / why we need it:
Move update_on_put() call under disk_lock protection

Fix race conditions in chunk_hash_to_init_timestamp dictionary access by
ensuring proper lock protection for cache policy operations.

Special notes for your reviewers:

If applicable:

  • this PR contains user facing changes - docs added
  • this PR contains unit tests

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical race condition affecting the LRU cache policy's update mechanism. By strategically relocating the update_on_put method call, the change guarantees that cache policy modifications are executed only under successful eviction conditions and within the necessary lock protection, preventing potential data corruption and ensuring thread safety for cache operations.

Highlights

  • Race Condition Fix: Addressed a race condition in the LRU cache policy by ensuring that the update_on_put operation is performed under proper lock protection.
  • Code Relocation: The cache_policy.update_on_put call was moved within the submit_put_task function to execute only when an eviction is successful, thereby ensuring its execution within a protected critical section.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

…kend

Signed-off-by: syaojun <libevent@yeah.net>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request correctly addresses a potential race condition by moving the cache_policy.update_on_put call inside the if evict_success: block. This ensures that the cache policy's internal state is updated only when a cache item is successfully put, preventing inconsistencies if the put operation fails due to eviction issues. This is a good improvement for the correctness and robustness of the cache management logic.

all_evict_keys.extend(evict_keys)
if evict_success:
self.current_cache_size += required_size
self.cache_policy.update_on_put(key)
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.

high

The change correctly places self.cache_policy.update_on_put(key) within the if evict_success: block. Previously, update_on_put would be called unconditionally (line 343 in the original code), even if the put operation failed due to insufficient space and no eviction candidates. This could lead to an inconsistent state where the cache policy's internal tracking (e.g., LRU timestamps) was updated for an item that was never actually stored. The current placement ensures that the cache policy is updated only when the item is successfully added to the cache, maintaining data integrity and correctness.

Copy link
Copy Markdown
Contributor

@sammshen sammshen left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Collaborator

@DongDongJu DongDongJu left a comment

Choose a reason for hiding this comment

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

Thansk for the work!

@DongDongJu DongDongJu enabled auto-merge (squash) March 27, 2026 05:30
@github-actions github-actions Bot added the full Run comprehensive tests on this PR label Mar 27, 2026
@DongDongJu DongDongJu merged commit 8d8c908 into LMCache:dev Mar 27, 2026
34 checks passed
jooho-XCENA pushed a commit to xcena-dev/LMCache that referenced this pull request Apr 2, 2026
fix: add missing lock protection for LRU cache policy in LocalDiskBackend

Signed-off-by: syaojun <libevent@yeah.net>
jooho-XCENA pushed a commit to xcena-dev/LMCache that referenced this pull request Apr 2, 2026
fix: add missing lock protection for LRU cache policy in LocalDiskBackend

Signed-off-by: syaojun <libevent@yeah.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full Run comprehensive tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants