[Bugfix] Fix memory leak in asynchronous mode#2559
Conversation
Summary of ChangesHello @deng451e, 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 memory leak occurring in the asynchronous execution path of the cache, particularly when LMCACHE_LOCAL_CPU is active. The leak stemmed from pinned memory objects not being properly released. The solution involves integrating explicit unpinning logic into the memory cleanup process for asynchronous operations and refining event retrieval to prevent premature removal, thereby ensuring all resources are correctly managed and deallocated. 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 aims to fix a memory leak in the asynchronous execution path by adding explicit unpin logic and modifying event handling. However, it introduces critical security vulnerabilities, including a race condition in LMCacheEngine's get_event_future that can lead to double-free and use-after-free issues, and a lack of authentication/encryption in the P2P backend, exposing sensitive KV cache data to unauthenticated remote exfiltration and cache poisoning attacks. Additionally, the changes introduce a new memory leak in the synchronous path within the retrieve method and a fragile condition in lookup_unpin.
4aeac12 to
f5f672e
Compare
maobaolong
left a comment
There was a problem hiding this comment.
@deng451e Thanks for the fixing! It would be better if you could double check the exception happen or corner case again.
BTW, the DCO failed.
2cd94d0 to
27f4048
Compare
Will do — I’ll add more async test cases to cover the exception paths and catch any missed corner cases. |
Signed-off-by: deng451e <838677410@qq.com>
Signed-off-by: deng451e <838677410@qq.com>
8490232 to
ba6e377
Compare
|
👍🏻, there was indeed a memory leak during asynchronous loading, and after I cherry-picked the code to 0.3.12, the memory leak seems to be gone. But some warnings appeared when I run in my env, the log like below: Please help confirm if my description is accurate. |
Signed-off-by: deng451e <838677410@qq.com>
Signed-off-by: deng451e <838677410@qq.com>
32e160d to
24b705c
Compare
Thank you for reporting this issue. Similar logs were also observed in CI when running tests with async.yaml. The root cause is that memory_obj was not pinned in host memory when data was retrieved from disk via batched_get_non_blocking in LocalDiskBackend under asynchronous mode, while an explicit unpin operation was subsequently invoked. The latest commit adds the missing pin logic in LocalDiskBackend, which should resolve the warning. |
* fix memory leak bug for async mode Signed-off-by: deng451e <838677410@qq.com> * Fix async pinned memory unpin Signed-off-by: deng451e <838677410@qq.com> --------- Signed-off-by: deng451e <838677410@qq.com> Co-authored-by: Samuel Shen <slshen@uchicago.edu>
* fix memory leak bug for async mode Signed-off-by: deng451e <838677410@qq.com> * Fix async pinned memory unpin Signed-off-by: deng451e <838677410@qq.com> --------- Signed-off-by: deng451e <838677410@qq.com> Co-authored-by: Samuel Shen <slshen@uchicago.edu> Signed-off-by: shaoxiawjc <wjc2800@163.com>
* fix memory leak bug for async mode Signed-off-by: deng451e <838677410@qq.com> * Fix async pinned memory unpin Signed-off-by: deng451e <838677410@qq.com> --------- Signed-off-by: deng451e <838677410@qq.com> Co-authored-by: Samuel Shen <slshen@uchicago.edu> Signed-off-by: Aaron Wu <aaron.wu@dell.com>
* fix memory leak bug for async mode Signed-off-by: deng451e <838677410@qq.com> * Fix async pinned memory unpin Signed-off-by: deng451e <838677410@qq.com> --------- Signed-off-by: deng451e <838677410@qq.com> Co-authored-by: Samuel Shen <slshen@uchicago.edu>
* fix memory leak bug for async mode Signed-off-by: deng451e <838677410@qq.com> * Fix async pinned memory unpin Signed-off-by: deng451e <838677410@qq.com> --------- Signed-off-by: deng451e <838677410@qq.com> Co-authored-by: Samuel Shen <slshen@uchicago.edu>
What this PR does / why we need it
This PR fixes the memory leak identified in PR #2434 in the asynchronous execution path when LMCACHE_LOCAL_CPU is enabled, where pinned memory objects were not released and metric inconsistencies occurred between active_memory_objs_count and local_cpu_hot_cache_count.
Root cause
In synchronous mode, self.lookup_pins directly tracks key–KV pairs and is used for ref-countdown and unpin.
In asynchronous mode, key–KV pairs are stored in self.event_manager. The event was popped in _async_process_tokens_internal before memory release, so the objects skipped the unpin/ref-countdown path and leaked. Additionally, the async code path did not release the retrieved memory_obj after use, unlike the synchronous flow.
Changes introduced in this PR
lookup_unpin.get_event_futuremethod inevent_managerto retrieve events without removing them.Special notes for reviewers