Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rax_tree): Fix crash caused by element destruction in RaxTreeMap #4228

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/core/search/rax_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ template <typename V> struct RaxTreeMap {
}

~RaxTreeMap() {
for (auto it = begin(); it != end(); ++it) {
V* ptr = &(*it).second;
std::allocator_traits<decltype(alloc_)>::destroy(alloc_, ptr);
alloc_.deallocate(ptr, 1);
while (true) {
Copy link
Contributor

@kostasrim kostasrim Nov 29, 2024

Choose a reason for hiding this comment

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

There are two things that concern me here:

  1. Looking at how raxRecursiveFree is implemented it calls free_callback(raxGetData(n) and what I introduced before seems to be on par in that respect because V* ptr = &(*it).second; is actually the result of raxGetData(n).
  2. Since we can reproduce this we should also include an isolated test to assert the root cause of the issue (my mistake not to suggest this earlier)

I would like to understand why deleting the result of raxGetData(n) result in segfault.

P.s. I also checked what Valkey is doing and indeed they use free_callback (they don't iterate like we do) but let's make sure we understand this

Copy link
Contributor Author

@BagritsevichStepan BagritsevichStepan Nov 29, 2024

Choose a reason for hiding this comment

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

raxFree passes NULL callback to the raxResursiveFree

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it's not called there

Copy link
Collaborator

Choose a reason for hiding this comment

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

@BagritsevichStepan do you know how to reproduce this?

auto it = begin();
if (it == end())
break;
auto find_it = find((*it).first);
erase(find_it);
}
raxFree(tree_);
}
Expand Down
Loading