Fix potential memory leak in sparse_attn_decode_interface#174
Open
NJX-njx wants to merge 1 commit intodeepseek-ai:mainfrom
Open
Fix potential memory leak in sparse_attn_decode_interface#174NJX-njx wants to merge 1 commit intodeepseek-ai:mainfrom
NJX-njx wants to merge 1 commit intodeepseek-ai:mainfrom
Conversation
Replace raw `new`/`delete` of `DecodeImplBase` with `std::unique_ptr` to ensure proper cleanup when an exception (e.g. from TORCH_CHECK) is thrown between allocation and the manual `delete` at the end of the function. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Refactors sparse_attn_decode_interface to use RAII for DecodeImplBase lifetime management, preventing leaks if exceptions occur before the manual cleanup path.
Changes:
- Replaced raw
new/deleteofDecodeImplBasewithstd::unique_ptr+std::make_unique. - Added
<memory>include and removed the manualdeleteat function end.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Author
Local Test ResultsSince the target GPU architectures (SM90a/SM100f) are not available locally, I wrote a standalone C++ test that reproduces the exact class hierarchy ( Test Code// Minimal reproduction of the DecodeImplBase hierarchy
static int g_destructor_call_count = 0;
class DecodeImplBase {
public:
virtual ~DecodeImplBase() { g_destructor_call_count++; }
virtual void run() = 0;
};
// OLD code: leaks on exception
void old_interface_raw_ptr(bool should_throw) {
DecodeImplBase* impl = new Decode_Sm90_Impl();
FAKE_TORCH_CHECK(!should_throw, "..."); // throws
impl->run();
delete impl; // never reached on throw
}
// NEW code: safe on exception
void new_interface_unique_ptr(bool should_throw) {
std::unique_ptr<DecodeImplBase> impl = std::make_unique<Decode_Sm90_Impl>();
FAKE_TORCH_CHECK(!should_throw, "..."); // throws
impl->run();
// RAII cleanup
}Results (MSVC 19.44, C++20, Windows x64)
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
csrc/api/sparse_decode.h, the functionsparse_attn_decode_interfaceallocates aDecodeImplBaseobject via rawnew(line 362-381) and manuallydeletes it at the end of the function (line 492).However, between allocation and deallocation, there are numerous operations that can throw exceptions:
TORCH_CHECK/KU_CHECK_*validation calls (lines 442-449)torch::empty(lines 420-421, 456-457)If any of these throw, the
delete implat line 492 is never reached, causing a memory leak. In a long-running inference service, repeated invalid requests would accumulate leakedDecodeImplBaseobjects.Fix
Replace the raw
new/deletepattern withstd::unique_ptr<DecodeImplBase>, which guarantees cleanup via RAII regardless of the exit path (normal return or exception).Changes
#include <memory>DecodeImplBase* impl->std::unique_ptr<DecodeImplBase> implnew Decode_*_Impl()->std::make_unique<Decode_*_Impl>()delete impl;After
The
implobject is now automatically destroyed when it goes out of scope, whether the function returns normally or exits via an exception. No behavioral change to the normal (non-exception) code path.Test plan
unique_ptruses the same virtual destructor chain as the previousdeletecall.test_flash_mla_sparse_decoding.py) should pass unchanged.