Skip to content

Commit 9d7c713

Browse files
authored
fix: prevent crash on KEYLOCK_ACQUIRED check for NO_KEY_TRANSACTIONAL commands (#5185)
fixed: #5173
1 parent d589e95 commit 9d7c713

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/server/search/search_family_test.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,6 +2768,47 @@ TEST_F(SearchFamilyTest, JsonSetIndexesBug) {
27682768
EXPECT_THAT(resp, IsUnordArrayWithSize(IsMap("text", "some text")));
27692769
}
27702770

2771+
TEST_F(SearchFamilyTest, SearchReindexWriteSearchRace) {
2772+
const std::string kIndexName = "myRaceIdx";
2773+
const int kWriterOps = 200;
2774+
const int kSearcherOps = 200;
2775+
const int kReindexerOps = 200;
2776+
2777+
auto writer_fiber = pp_->at(0)->LaunchFiber([&] {
2778+
for (int i = 1; i <= kWriterOps; ++i) {
2779+
std::string doc_key = absl::StrCat("doc:", i);
2780+
std::string content = absl::StrCat("text data item ", i, " for race condition test");
2781+
std::string tags_val = absl::StrCat("tagA,tagB,", (i % 10));
2782+
std::string numeric_field_val = std::to_string(i);
2783+
Run({"hset", doc_key, "content", content, "tags", tags_val, "numeric_field",
2784+
numeric_field_val});
2785+
}
2786+
});
2787+
2788+
auto searcher_fiber = pp_->at(1)->LaunchFiber([&] {
2789+
for (int i = 1; i <= kSearcherOps; ++i) {
2790+
int random_val_content = 1 + (i % kWriterOps);
2791+
std::string query_content = absl::StrCat("@content:item", random_val_content);
2792+
Run({"ft.search", kIndexName, query_content});
2793+
}
2794+
});
2795+
2796+
auto reindexer_fiber = pp_->at(2)->LaunchFiber([&] {
2797+
for (int i = 1; i <= kReindexerOps; ++i) {
2798+
Run({"ft.create", kIndexName, "ON", "HASH", "PREFIX", "1", "doc:", "SCHEMA", "content",
2799+
"TEXT", "SORTABLE", "tags", "TAG", "SORTABLE", "numeric_field", "NUMERIC", "SORTABLE"});
2800+
Run({"ft.dropindex", kIndexName});
2801+
}
2802+
});
2803+
2804+
// Join fibers
2805+
writer_fiber.Join();
2806+
searcher_fiber.Join();
2807+
reindexer_fiber.Join();
2808+
2809+
ASSERT_FALSE(service_->IsShardSetLocked());
2810+
}
2811+
27712812
TEST_F(SearchFamilyTest, IgnoredOptionsInFtCreate) {
27722813
Run({"HSET", "doc:1", "title", "Test Document"});
27732814

src/server/transaction.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,13 @@ bool Transaction::CancelShardCb(EngineShard* shard) {
12441244
if (IsGlobal()) {
12451245
shard->shard_lock()->Release(LockMode());
12461246
} else {
1247-
auto lock_args = GetLockArgs(shard->shard_id());
1248-
DCHECK(sd.local_mask & KEYLOCK_ACQUIRED);
1249-
DCHECK(!lock_args.fps.empty());
1250-
GetDbSlice(shard->shard_id()).Release(LockMode(), lock_args);
1247+
if ((cid_->opt_mask() & CO::NO_KEY_TRANSACTIONAL) == 0) {
1248+
auto lock_args = GetLockArgs(shard->shard_id());
1249+
DCHECK(sd.local_mask & KEYLOCK_ACQUIRED);
1250+
DCHECK(!lock_args.fps.empty());
1251+
GetDbSlice(shard->shard_id()).Release(LockMode(), lock_args);
1252+
}
1253+
12511254
sd.local_mask &= ~KEYLOCK_ACQUIRED;
12521255
}
12531256

0 commit comments

Comments
 (0)