Skip to content

fix: prevent crash on KEYLOCK_ACQUIRED check for NO_KEY_TRANSACTIONAL commands #5185

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 10 commits into from
May 29, 2025
41 changes: 41 additions & 0 deletions src/server/search/search_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2768,6 +2768,47 @@ TEST_F(SearchFamilyTest, JsonSetIndexesBug) {
EXPECT_THAT(resp, IsUnordArrayWithSize(IsMap("text", "some text")));
}

TEST_F(SearchFamilyTest, SearchReindexWriteSearchRace) {
const std::string kIndexName = "myRaceIdx";
const int kWriterOps = 200;
const int kSearcherOps = 200;
const int kReindexerOps = 200;

auto writer_fiber = pp_->at(0)->LaunchFiber([&] {
for (int i = 1; i <= kWriterOps; ++i) {
std::string doc_key = absl::StrCat("doc:", i);
std::string content = absl::StrCat("text data item ", i, " for race condition test");
std::string tags_val = absl::StrCat("tagA,tagB,", (i % 10));
std::string numeric_field_val = std::to_string(i);
Run({"hset", doc_key, "content", content, "tags", tags_val, "numeric_field",
numeric_field_val});
}
});

auto searcher_fiber = pp_->at(1)->LaunchFiber([&] {
for (int i = 1; i <= kSearcherOps; ++i) {
int random_val_content = 1 + (i % kWriterOps);
std::string query_content = absl::StrCat("@content:item", random_val_content);
Run({"ft.search", kIndexName, query_content});
}
});

auto reindexer_fiber = pp_->at(2)->LaunchFiber([&] {
for (int i = 1; i <= kReindexerOps; ++i) {
Run({"ft.create", kIndexName, "ON", "HASH", "PREFIX", "1", "doc:", "SCHEMA", "content",
"TEXT", "SORTABLE", "tags", "TAG", "SORTABLE", "numeric_field", "NUMERIC", "SORTABLE"});
Run({"ft.dropindex", kIndexName});
}
});

// Join fibers
writer_fiber.Join();
searcher_fiber.Join();
reindexer_fiber.Join();

ASSERT_FALSE(service_->IsShardSetLocked());
}

TEST_F(SearchFamilyTest, IgnoredOptionsInFtCreate) {
Run({"HSET", "doc:1", "title", "Test Document"});

Expand Down
11 changes: 7 additions & 4 deletions src/server/transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1244,10 +1244,13 @@ bool Transaction::CancelShardCb(EngineShard* shard) {
if (IsGlobal()) {
shard->shard_lock()->Release(LockMode());
} else {
auto lock_args = GetLockArgs(shard->shard_id());
DCHECK(sd.local_mask & KEYLOCK_ACQUIRED);
DCHECK(!lock_args.fps.empty());
GetDbSlice(shard->shard_id()).Release(LockMode(), lock_args);
if ((cid_->opt_mask() & CO::NO_KEY_TRANSACTIONAL) == 0) {
auto lock_args = GetLockArgs(shard->shard_id());
DCHECK(sd.local_mask & KEYLOCK_ACQUIRED);
DCHECK(!lock_args.fps.empty());
GetDbSlice(shard->shard_id()).Release(LockMode(), lock_args);
}

sd.local_mask &= ~KEYLOCK_ACQUIRED;
}

Expand Down
Loading