Skip to content

fix(server): guard finish_read_prefetched behind retrieve_succeeded flag#2736

Merged
maobaolong merged 2 commits intoLMCache:devfrom
maobaolong:fix/retrieve-succeeded-guard
Mar 13, 2026
Merged

fix(server): guard finish_read_prefetched behind retrieve_succeeded flag#2736
maobaolong merged 2 commits intoLMCache:devfrom
maobaolong:fix/retrieve-succeeded-guard

Conversation

@maobaolong
Copy link
Copy Markdown
Collaborator

@maobaolong maobaolong commented Mar 11, 2026

When _retrieve_loop raises an exception inside the with-block, the read_prefetched_results context manager already releases the read locks in its finally clause. The retrieve() finally block was unconditionally scheduling a GPU callback to call
finish_read_prefetched, causing a double-free of read locks.

Introduce retrieve_succeeded flag that is only set to True when the with-block exits normally. The GPU callback is now conditional on this flag, forming a defense-in-depth pair with the context manager re-raise fix.

What this PR does / why we need it:

In MPCacheEngine.retrieve(), the finally block unconditionally schedules a GPU callback to call finish_read_prefetched, which releases the read lock. When _retrieve_loop raises an exception, the read_prefetched_results context manager already releases the lock in its own finally clause. However, Python's finally block executes even after a return in an except block, so the GPU callback is still scheduled, causing a read lock double-free.
Fix: Introduce a retrieve_succeeded flag initialized to False, set to True only after the with block exits normally. The GPU callback in finally is now conditional on this flag. This forms a defense-in-depth pair with the context manager re-raise fix (PR #2737) — either fix alone prevents the double-free, but together they provide two independent safety layers.

Special notes for your reviewers:

If applicable:

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

When _retrieve_loop raises an exception inside the with-block, the
read_prefetched_results context manager already releases the read
locks in its finally clause. The retrieve() finally block was
unconditionally scheduling a GPU callback to call
finish_read_prefetched, causing a double-free of read locks.

Introduce retrieve_succeeded flag that is only set to True when the
with-block exits normally. The GPU callback is now conditional on
this flag, forming a defense-in-depth pair with the context manager
re-raise fix.

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
@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 addresses a critical bug in the MPCacheEngine.retrieve() method where a GPU callback could lead to a double-free of read locks under specific error conditions. By implementing a conditional flag, the change ensures that resource release operations are correctly synchronized with the successful completion of retrieval operations, enhancing the stability and robustness of the caching mechanism.

Highlights

  • Prevented double-free of read locks: Introduced a retrieve_succeeded flag to guard the finish_read_prefetched GPU callback, ensuring it is only invoked when the with-block exits without exceptions, thus preventing a double-free of read locks.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • lmcache/v1/multiprocess/server.py
    • Initialized retrieve_succeeded boolean flag to False.
    • Set retrieve_succeeded to True only upon successful exit of the with block.
    • Modified the finally block to conditionally schedule the finish_read_prefetched GPU callback based on the retrieve_succeeded flag.
Activity
  • No specific review comments or activity have been recorded yet.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

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 effectively addresses a critical bug where finish_read_prefetched could be called twice, leading to a double-free of read locks. By introducing the retrieve_succeeded flag, the GPU callback to release read locks is now correctly conditional on the successful exit of the read_prefetched_results context manager. This provides a robust defense-in-depth solution, preventing potential issues related to resource management and ensuring the stability of the cache engine.

The context manager was swallowing exceptions raised inside the
with-block (e.g. TypeError from _retrieve_loop). This caused the
caller to continue as if the retrieve succeeded, leading to a
double-free of read locks: once by the context manager's finally
clause, and again by the caller's GPU callback.

Add 'raise' to propagate the exception so callers correctly enter
their except branch and skip the redundant finish_read_prefetched.

This change was originally in PR LMCache#2737 and is now combined into
this PR per reviewer's suggestion to streamline the merging process.

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
@maobaolong
Copy link
Copy Markdown
Collaborator Author

@ApostaC Thanks for the review at #2737, I closed it and merge it into this PR.

@maobaolong maobaolong enabled auto-merge (squash) March 12, 2026 07:23
@maobaolong maobaolong disabled auto-merge March 12, 2026 07:23
@github-actions github-actions Bot added full Run comprehensive tests on this PR and removed full Run comprehensive tests on this PR labels Mar 12, 2026
Copy link
Copy Markdown
Contributor

@ApostaC ApostaC left a comment

Choose a reason for hiding this comment

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

LGTM!

@ApostaC
Copy link
Copy Markdown
Contributor

ApostaC commented Mar 12, 2026

@KuntaiDu Can you take another look?

@maobaolong maobaolong added the full Run comprehensive tests on this PR label Mar 13, 2026
@maobaolong maobaolong enabled auto-merge (squash) March 13, 2026 02:30
Copy link
Copy Markdown
Collaborator

@chunxiaozheng chunxiaozheng left a comment

Choose a reason for hiding this comment

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

LGTM

@maobaolong maobaolong merged commit e4af8bc into LMCache:dev Mar 13, 2026
37 of 40 checks passed
realAaronWu pushed a commit to realAaronWu/LMCache that referenced this pull request Mar 20, 2026
…lag (LMCache#2736)

* fix(server): guard finish_read_prefetched behind retrieve_succeeded flag

When _retrieve_loop raises an exception inside the with-block, the
read_prefetched_results context manager already releases the read
locks in its finally clause. The retrieve() finally block was
unconditionally scheduling a GPU callback to call
finish_read_prefetched, causing a double-free of read locks.

Introduce retrieve_succeeded flag that is only set to True when the
with-block exits normally. The GPU callback is now conditional on
this flag, forming a defense-in-depth pair with the context manager
re-raise fix.

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* fix(storage_manager): re-raise exception in read_prefetched_results

The context manager was swallowing exceptions raised inside the
with-block (e.g. TypeError from _retrieve_loop). This caused the
caller to continue as if the retrieve succeeded, leading to a
double-free of read locks: once by the context manager's finally
clause, and again by the caller's GPU callback.

Add 'raise' to propagate the exception so callers correctly enter
their except branch and skip the redundant finish_read_prefetched.

This change was originally in PR LMCache#2737 and is now combined into
this PR per reviewer's suggestion to streamline the merging process.

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

---------

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: Aaron Wu <aaron.wu@dell.com>
jooho-XCENA pushed a commit to xcena-dev/LMCache that referenced this pull request Apr 2, 2026
…lag (LMCache#2736)

* fix(server): guard finish_read_prefetched behind retrieve_succeeded flag

When _retrieve_loop raises an exception inside the with-block, the
read_prefetched_results context manager already releases the read
locks in its finally clause. The retrieve() finally block was
unconditionally scheduling a GPU callback to call
finish_read_prefetched, causing a double-free of read locks.

Introduce retrieve_succeeded flag that is only set to True when the
with-block exits normally. The GPU callback is now conditional on
this flag, forming a defense-in-depth pair with the context manager
re-raise fix.

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* fix(storage_manager): re-raise exception in read_prefetched_results

The context manager was swallowing exceptions raised inside the
with-block (e.g. TypeError from _retrieve_loop). This caused the
caller to continue as if the retrieve succeeded, leading to a
double-free of read locks: once by the context manager's finally
clause, and again by the caller's GPU callback.

Add 'raise' to propagate the exception so callers correctly enter
their except branch and skip the redundant finish_read_prefetched.

This change was originally in PR LMCache#2737 and is now combined into
this PR per reviewer's suggestion to streamline the merging process.

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

---------

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
jooho-XCENA pushed a commit to xcena-dev/LMCache that referenced this pull request Apr 2, 2026
…lag (LMCache#2736)

* fix(server): guard finish_read_prefetched behind retrieve_succeeded flag

When _retrieve_loop raises an exception inside the with-block, the
read_prefetched_results context manager already releases the read
locks in its finally clause. The retrieve() finally block was
unconditionally scheduling a GPU callback to call
finish_read_prefetched, causing a double-free of read locks.

Introduce retrieve_succeeded flag that is only set to True when the
with-block exits normally. The GPU callback is now conditional on
this flag, forming a defense-in-depth pair with the context manager
re-raise fix.

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* fix(storage_manager): re-raise exception in read_prefetched_results

The context manager was swallowing exceptions raised inside the
with-block (e.g. TypeError from _retrieve_loop). This caused the
caller to continue as if the retrieve succeeded, leading to a
double-free of read locks: once by the context manager's finally
clause, and again by the caller's GPU callback.

Add 'raise' to propagate the exception so callers correctly enter
their except branch and skip the redundant finish_read_prefetched.

This change was originally in PR LMCache#2737 and is now combined into
this PR per reviewer's suggestion to streamline the merging process.

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

---------

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
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