Skip to content

fix: HNSW schema deleting document crash #4987

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

Merged
merged 6 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 8 additions & 4 deletions src/server/search/doc_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,14 @@ void ShardDocIndex::RemoveDoc(string_view key, const DbContext& db_cntx, const P
if (!indices_)
return;

auto accessor = GetAccessor(db_cntx, pv);
auto id = key_index_.Remove(key);
if (id) {
indices_->Remove(id.value(), *accessor);
try {
Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure I understand. What call exactly throws?

Copy link
Collaborator

Choose a reason for hiding this comment

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

now I understand, I think this should be improved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The exception occurs inside the library:
third_party/libs/hnswlib/include/hnswlib/hnswalg.h

Screenshot 2025-04-23 at 18 06 06

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes, but it's a legitimate flow. so we should protect ourselves and there is no reason for WARNING.
So at least I would move throw/catch into dfly::search::HnswlibAdapter::Remove and remove any logs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

auto accessor = GetAccessor(db_cntx, pv);
auto id = key_index_.Remove(key);
if (id) {
indices_->Remove(id.value(), *accessor);
}
} catch (const std::exception& e) {
LOG(WARNING) << "Exception while removing document " << key << ": " << e.what();
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/server/search/search_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2722,4 +2722,16 @@ TEST_F(SearchFamilyTest, JsonWithNullFields) {
AreDocIds("doc:1", "doc:2"));
}

TEST_F(SearchFamilyTest, TestTwoHsetDocumentCreationCrashes) {
EXPECT_EQ(Run({"FT.CREATE", "idx", "SCHEMA", "n", "NUMERIC", "v", "VECTOR", "HNSW", "8", "TYPE",
"FLOAT16", "DIM", "4", "DISTANCE_METRIC", "L2", "M", "65536"}),
"OK");

auto res = Run({"HSET", "doc", "n", "0"});
EXPECT_EQ(res, 1);

res = Run({"HSET", "doc", "n", "1"});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since it's during removal, I assume if we call DEL doc it would achieve similar effect? If yes, it's a clearer test, imho.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, correct. I checked it. DEL doc archives the same effect. I will change the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

EXPECT_EQ(res, 0);
}

} // namespace dfly
Loading