Skip to content

fix(datacell): fix memory leak in FlattenDataCell query on exception#1680

Merged
wxyucs merged 3 commits intomainfrom
copilot/fix-hgraph-memory-leak
Mar 13, 2026
Merged

fix(datacell): fix memory leak in FlattenDataCell query on exception#1680
wxyucs merged 3 commits intomainfrom
copilot/fix-hgraph-memory-leak

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 12, 2026

When MemoryBlockIO::DirectReadImpl allocates a buffer across block boundaries (need_release = true), exceptions thrown by subsequent GetCodesById calls or ComputeDistsBatch4/ComputeDist skip the corresponding Release() calls, leaking memory.

Changes

  • query() batch-of-4 loop: Initialize all four code pointers to nullptr; wrap GetCodesById × 4 + ComputeDistsBatch4 in try/catch with a release_batch lambda that null-guards each pointer before releasing.
  • query() single-item loop: Wrap GetCodesById + ComputeDist in try/catch; release on both exception and normal paths with consistent release && codes guard.
  • ComputePairVectors(): Same pattern — initialize to nullptr, release_pair lambda, try/catch guarantees cleanup on exception.
// Before — leaks codes1..4 if ComputeDistsBatch4 (or any GetCodesById) throws
const auto* codes1 = this->GetCodesById(idx[i],     release1);
const auto* codes2 = this->GetCodesById(idx[i + 1], release2);
...
computer->ComputeDistsBatch4(codes1, codes2, codes3, codes4, ...);
if (release1) this->io_->Release(codes1);  // unreachable on exception

// After — guaranteed release via lambda + try/catch
auto release_batch = [&]() {
    if (release1 && codes1) this->io_->Release(codes1);
    ...
};
try {
    codes1 = this->GetCodesById(idx[i], release1);
    ...
    computer->ComputeDistsBatch4(...);
} catch (...) { release_batch(); throw; }
release_batch();
Original prompt

This section details on the original issue you should resolve

<issue_title>HGraph memory leak</issue_title>
<issue_description>Describe the bug
leak backtrace

vsag::SafeAllocator::Allocate(unsigned long) at ??:?
vsag::MemoryBlockIO::DirectReadImpl(unsigned long, unsigned long, bool&) const at ??:?
vsag::FlattenDataCell<vsag::FP32Quantizer<(vsag::MetricType)0>, vsag::MemoryBlockIO>::query(float*, vsag::Computer<vsag::FP32Quantizer<(vsag::MetricType)0> >*, unsigned int const*, unsigned int, vsag::QueryContext*) at ??:?
std::shared_ptr<vsag::DistanceHeap> vsag::BasicSearcher::search_impl<(vsag::InnerSearchMode)1>(std::shared_ptr<vsag::GraphInterface> const&, std::shared_ptr<vsag::FlattenInterface> const&, std::shared_ptr<vsag::VisitedList> const&, void const*, vsag::InnerSearchParam const&, std::shared_ptr<vsag::LabelTable> const&, vsag::QueryContext*) const at ??:?
vsag::BasicSearcher::Search(std::shared_ptr<vsag::GraphInterface> const&, std::shared_ptr<vsag::FlattenInterface> const&, std::shared_ptr<vsag::VisitedList> const&, void const*, vsag::InnerSearchParam const&, std::shared_ptr<vsag::LabelTable> const&, vsag::QueryContext*) const [clone .localalias] at xpointer.c:?
std::shared_ptr<vsag::DistanceHeap> vsag::HGraph::search_one_graph<(vsag::InnerSearchMode)1>(void const*, std::shared_ptr<vsag::GraphInterface> const&, std::shared_ptr<vsag::FlattenInterface> const&, vsag::InnerSearchParam&, std::shared_ptr<vsag::VisitedList> const&, vsag::QueryContext*) const at ??:?
vsag::HGraph::graph_add_one(void const*, int, unsigned int) at ??:?
vsag::HGraph::add_one_point(void const*, int, unsigned int) at ??:?
vsag::HGraph::Add(std::shared_ptr<vsag::Dataset> const&) at ??:?
vsag::IndexImpl<vsag::HGraph>::Add(std::shared_ptr<vsag::Dataset> const&) at ??:?

Analysis
Allocation and Deallocation Logic

  1. Allocation Location (memory_block_io.cpp) memory_block_io.cpp
  2. Release position (flatten_datacell.h) flatten_datacell.h Lines 354-391

Between GetCodesById and Release, ComputeDistsBatch4/ComputeDist is called. Once these functions throw an exception, Release will not be executed, and the allocated 1-4 buffers will leak.

To Reproduce
Codes to reproduce the behavior:

// paste codes here

Environment
Please run bash scripts/check_environment.sh and paste the output here:

  • OS: Linux
  • vsag version: v0.18.2
  • compiler version: e.g. GCC9
  • interface: cpp

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.
</issue_description>

Comments on the Issue (you are @copilot in this section)


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

…ception

Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>
Copilot AI changed the title [WIP] [HGRAPH-101] Fix HGraph memory leak Fix memory leak in FlattenDataCell when exceptions occur during query Mar 12, 2026
@wxyucs wxyucs marked this pull request as ready for review March 12, 2026 07:44
@wxyucs wxyucs requested a review from jiaweizone as a code owner March 12, 2026 07:44
Copilot AI review requested due to automatic review settings March 12, 2026 07:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an exception-path memory leak in FlattenDataCell queries by ensuring buffers returned from MemoryBlockIO::DirectReadImpl are always released even when downstream distance computations (or subsequent reads) throw.

Changes:

  • Add null-safe cleanup lambdas and try/catch guards around the batch-of-4 query loop to guarantee Release() runs on exceptions.
  • Add exception-safe cleanup to the single-item query loop with consistent release && codes guarding.
  • Wrap ComputePairVectors() body to ensure both code buffers are released on any exception.

Comment on lines +372 to 405
try {
codes1 = this->GetCodesById(idx[i], release1);
codes2 = this->GetCodesById(idx[i + 1], release2);
codes3 = this->GetCodesById(idx[i + 2], release3);
codes4 = this->GetCodesById(idx[i + 3], release4);
computer->ComputeDistsBatch4(codes1,
codes2,
codes3,
codes4,
result_dists[i],
result_dists[i + 1],
result_dists[i + 2],
result_dists[i + 3]);
} catch (...) {
release_batch();
throw;
}
release_batch();
}
for (; i < id_count; ++i) {
bool release = false;
const auto* codes = this->GetCodesById(idx[i], release);
computer->ComputeDist(codes, result_dists + i);
if (release) {
const uint8_t* codes = nullptr;
try {
codes = this->GetCodesById(idx[i], release);
computer->ComputeDist(codes, result_dists + i);
} catch (...) {
if (release && codes) {
this->io_->Release(codes);
}
throw;
}
if (release && codes) {
this->io_->Release(codes);
}
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

Missing regression test coverage for the new exception-safety paths. The leak fix is specifically about exceptions thrown between GetCodesById() and Release(), but current unit tests for FlattenDataCell appear to exercise only the non-throwing query path. Please add a Catch2 test that forces need_release=true (e.g., by using block_memory_io with a small block size / boundary-crossing reads) and then injects an exception from ComputeDist/ComputeDistsBatch4 (or an equivalent hook) to assert the allocated buffers are released (e.g., via a counting allocator or LSAN/ASAN expectation).

Copilot generated this review using guidance from repository custom instructions.
Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>
Copilot AI changed the title Fix memory leak in FlattenDataCell when exceptions occur during query fix(datacell): fix memory leak in FlattenDataCell query on exception Mar 12, 2026
Copy link
Copy Markdown
Collaborator

@inabao inabao left a comment

Choose a reason for hiding this comment

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

LGTM

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 68.62745% with 16 lines in your changes missing coverage. Please review.

@@            Coverage Diff             @@
##             main    #1680      +/-   ##
==========================================
+ Coverage   90.66%   90.91%   +0.25%     
==========================================
  Files         329      329              
  Lines       19455    19474      +19     
==========================================
+ Hits        17639    17705      +66     
+ Misses       1816     1769      -47     
Flag Coverage Δ
cpp 90.91% <68.62%> (+0.25%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
common 85.81% <ø> (-0.18%) ⬇️
datacell 92.09% <ø> (+0.40%) ⬆️
index 90.58% <ø> (+0.66%) ⬆️
simd 100.00% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d091c9e...a533980. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@LHT129 LHT129 left a comment

Choose a reason for hiding this comment

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

LHTM

@wxyucs wxyucs merged commit bf48a2a into main Mar 13, 2026
23 checks passed
@wxyucs wxyucs deleted the copilot/fix-hgraph-memory-leak branch March 13, 2026 03:41
wxyucs added a commit that referenced this pull request Mar 13, 2026
…1680)

* Initial plan

* Fix memory leak in FlattenDataCell query and ComputePairVectors on exception

Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>

* fix(datacell): fix memory leak in FlattenDataCell query on exception

Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>
wxyucs added a commit that referenced this pull request Mar 16, 2026
…1680)

* Initial plan

* Fix memory leak in FlattenDataCell query and ComputePairVectors on exception

Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>

* fix(datacell): fix memory leak in FlattenDataCell query on exception

Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>
wxyucs added a commit that referenced this pull request Mar 30, 2026
…1680)

* Initial plan

* Fix memory leak in FlattenDataCell query and ComputePairVectors on exception

Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>

* fix(datacell): fix memory leak in FlattenDataCell query on exception

Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wxyucs <12595343+wxyucs@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HGraph memory leak

5 participants