[PR #1164] [MERGED] Fix memory leak in MooncakeBackend destructor #2046

Closed
opened 2026-10-29 21:32:22 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/kvcache-ai/Mooncake/pull/1164
Author: @Copilot
Created: 12/4/2025
Status: ✅ Merged
Merged: 12/4/2025
Merged by: @UNIDY2002

Base: sunxun/ep-ci ← Head: copilot/sub-pr-1099


📝 Commits (4)

  • 7870a53 Initial plan
  • d668c79 Add destructor to fix memory leak in MooncakeBackend
  • bba7cf8 Final update: Memory leak fix completed
  • e7425f4 Delete _codeql_detected_source_root

📊 Changes

2 files changed (+12 additions, -1 deletions)

View changed files

📝 mooncake-ep/include/mooncake_backend.h (+1 -1)
📝 mooncake-ep/src/mooncake_backend.cpp (+11 -0)

📄 Description

Description

Addresses memory leak in MooncakeBackend where meta_.activeRanks was allocated but never freed. The allocation occurred via new bool[] (CPU path) or cudaHostAlloc (CUDA path) in the constructor, but the default destructor did not deallocate.

Changes:

  • Implemented custom destructor to free meta_.activeRanks
    • CPU path: delete[] for memory allocated with new
    • CUDA path: cudaFreeHost for memory allocated with cudaHostAlloc
    • Sets pointer to nullptr after deallocation

Type of Change

  • Types
    • Bug fix
    • New feature
      • Transfer Engine
      • Mooncake Store
      • Mooncake EP
      • Integration
      • P2P Store
      • Python Wheel
    • Breaking change
    • CI/CD
    • Documentation update
    • Other

How Has This Been Tested?

Code review and security scan completed with no issues.

Checklist

  • I have performed a self-review of my own code.
  • I have updated the documentation.
  • I have added tests to prove my changes are effective.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/kvcache-ai/Mooncake/pull/1164 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 12/4/2025 **Status:** ✅ Merged **Merged:** 12/4/2025 **Merged by:** [@UNIDY2002](https://github.com/UNIDY2002) **Base:** `sunxun/ep-ci` ← **Head:** `copilot/sub-pr-1099` --- ### 📝 Commits (4) - [`7870a53`](https://github.com/kvcache-ai/Mooncake/commit/7870a5305050cb41d7f93a49ca9369957c3bb2c4) Initial plan - [`d668c79`](https://github.com/kvcache-ai/Mooncake/commit/d668c79ac3e87946d7e9e4ec1cc1906cb16f11e8) Add destructor to fix memory leak in MooncakeBackend - [`bba7cf8`](https://github.com/kvcache-ai/Mooncake/commit/bba7cf86997a7773d8fa941cdd6adc48a6234c98) Final update: Memory leak fix completed - [`e7425f4`](https://github.com/kvcache-ai/Mooncake/commit/e7425f4bdbf23a944b57f910e96e105a0a859c73) Delete _codeql_detected_source_root ### 📊 Changes **2 files changed** (+12 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `mooncake-ep/include/mooncake_backend.h` (+1 -1) 📝 `mooncake-ep/src/mooncake_backend.cpp` (+11 -0) </details> ### 📄 Description ## Description Addresses memory leak in `MooncakeBackend` where `meta_.activeRanks` was allocated but never freed. The allocation occurred via `new bool[]` (CPU path) or `cudaHostAlloc` (CUDA path) in the constructor, but the default destructor did not deallocate. **Changes:** - Implemented custom destructor to free `meta_.activeRanks` - CPU path: `delete[]` for memory allocated with `new` - CUDA path: `cudaFreeHost` for memory allocated with `cudaHostAlloc` - Sets pointer to `nullptr` after deallocation ## Type of Change * Types - [x] Bug fix - [ ] New feature - [ ] Transfer Engine - [ ] Mooncake Store - [ ] Mooncake EP - [ ] Integration - [ ] P2P Store - [ ] Python Wheel - [ ] Breaking change - [ ] CI/CD - [ ] Documentation update - [ ] Other ## How Has This Been Tested? Code review and security scan completed with no issues. ## Checklist - [x] I have performed a self-review of my own code. - [ ] I have updated the documentation. - [ ] I have added tests to prove my changes are effective. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:32:22 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
kvcache-ai/Mooncake#2046
No description provided.