fix(l1_manager): propagate extra_count through prefetch path to prevent premature eviction#2725
fix(l1_manager): propagate extra_count through prefetch path to prevent premature eviction#2725maobaolong merged 1 commit intoLMCache:devfrom
Conversation
Summary of ChangesHello, 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 issue causing premature eviction of prefetched keys in distributed environments utilizing Tensor Parallelism (TP > 1). Previously, only a single read lock was acquired per key upon prefetch completion, leading to data eviction before all TP workers could access it. The fix involves systematically propagating the Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request addresses a bug causing premature eviction in multi-worker scenarios by propagating the extra_count parameter through the prefetch path. However, it misses a critical propagation point in the read_prefetched_results context manager, which could lead to lock leaks. Additionally, the lack of type validation for the new parameter at API boundaries poses a risk of crashing background processing threads if malformed input is provided.
| request_id = self._prefetch_controller.submit_prefetch_request( | ||
| remaining_keys, | ||
| layout_desc, | ||
| extra_count=extra_count, |
There was a problem hiding this comment.
The propagation of the extra_count parameter is incomplete. While it is correctly passed to the PrefetchController in submit_prefetch_task, the read_prefetched_results context manager (lines 173-233) was not updated to accept or use this parameter.
In its finally block (line 230), it calls self._l1_manager.finish_read(good_keys) with the default extra_count=0. If the objects were originally prefetched with an extra_count > 0, this call will only release one of the multiple read locks acquired. This results in a lock leak, which prevents the objects from being evicted or deleted from the L1 cache, eventually leading to resource exhaustion and a Denial of Service of the cache manager.
| extra_count = _validate_extra_count(extra_count) | ||
| total = 1 + extra_count |
There was a problem hiding this comment.
The extra_count parameter is not validated to be an integer before being used in calculations and loops. While _validate_extra_count performs range checking, it does not verify the type.
In the PrefetchController, this value is queued and processed by a background thread. If an invalid type (e.g., a string or a float) is passed to the public submit_prefetch_request API, it will eventually cause the background thread to crash with a TypeError when it attempts to perform comparisons (line 101), string formatting in the logger (line 103), or use the value in range() (line 569). A crash in the background loop disables prefetch functionality for the entire application. It is recommended to validate that extra_count is a non-negative integer at the entry points of the public APIs.
|
Hey @liuyumoye , can you fix the DCO issue by doing? Thanks! |
Thanks for the reminder! Fixed the DCO issue and force-pushed. |
ApostaC
left a comment
There was a problem hiding this comment.
Can we add some unit test to test the extra_count path for the prefetch controller?
Otherwise LGTM!
Thanks for the review! I've added a
|
|
@liuyumoye Maybe you have to update your branch to the dev, otherwise, the UT will not passed. |
…nt premature eviction Signed-off-by: liuyumoye <adeline_ly2023@outlook.com>
maobaolong
left a comment
There was a problem hiding this comment.
LGTM @liuyumoye Thanks for this fix.
…nt premature eviction (LMCache#2725) Signed-off-by: liuyumoye <adeline_ly2023@outlook.com> Co-authored-by: liuyumoye <adeline_ly2023@outlook.com> Signed-off-by: shaoxiawjc <wjc2800@163.com>
…nt premature eviction (LMCache#2725) Signed-off-by: liuyumoye <adeline_ly2023@outlook.com> Co-authored-by: liuyumoye <adeline_ly2023@outlook.com> Signed-off-by: Aaron Wu <aaron.wu@dell.com>
…nt premature eviction (LMCache#2725) Signed-off-by: liuyumoye <adeline_ly2023@outlook.com> Co-authored-by: liuyumoye <adeline_ly2023@outlook.com>
…nt premature eviction (LMCache#2725) Signed-off-by: liuyumoye <adeline_ly2023@outlook.com> Co-authored-by: liuyumoye <adeline_ly2023@outlook.com>
fix(l1_manager): propagate extra_count through prefetch path to prevent premature eviction
What this PR does / why we need it:
When a prefetch request completes and loaded keys transition from write-locked to read-locked, finish_write_and_reserve_read was always acquiring exactly 1 read lock per key, regardless of the extra_count passed by the caller. This caused premature eviction in Tensor Parallelism (TP > 1) scenarios (e.g. MLA models), where each TP worker needs to consume its own read lock — but only 1 lock was held, so the first worker's finish_read would drop the ref-count to 0 and trigger eviction before the remaining workers could access the data.
This PR propagates extra_count through the entire prefetch path:
StorageManager.submit_prefetch_request → PrefetchController.submit_prefetch_request → _submission_queue / _pending_queue → _start_lookup_phase → InFlightPrefetchRequest.extra_count
On prefetch completion: finish_write_and_reserve_read(loaded_keys, extra_count=request.extra_count) acquires 1 + extra_count read locks per key
Non-prefix loaded keys that are immediately released also pass extra_count to finish_read
L1Manager.finish_write_and_reserve_read now accepts extra_count and calls entry.read_lock.lock() a total of 1 + extra_count times per key
Special notes for your reviewers:
The extra_count semantics are unchanged from the existing submit_prefetch_task / finish_read contract — this PR only ensures the value is correctly threaded through the prefetch pipeline rather than silently dropped.
_pending_queue and _submission_queue tuple types are widened from (..., MemoryLayoutDesc) to (..., MemoryLayoutDesc, int)
No behavior change when extra_count=0 (the default), so existing single-TP deployments are unaffected.
If applicable: