Skip to content

Commit

Permalink
fix(server): Fix SCAN deadlock (#3235)
Browse files Browse the repository at this point in the history
  • Loading branch information
dranikpg authored and adiholden committed Jun 30, 2024
1 parent a931177 commit ca98411
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/server/generic_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,16 @@ uint64_t ScanGeneric(uint64_t cursor, const ScanOpts& scan_opts, StringVec* keys
DbContext db_cntx{.db_index = cntx->conn_state.db_index, .time_now_ms = GetCurrentTimeMs()};

do {
ess->Await(sid, [&] {
auto cb = [&] {
OpArgs op_args{EngineShard::tlocal(), 0, db_cntx};
OpScan(op_args, scan_opts, &cursor, keys);
});
};

// Avoid deadlocking, if called from shard queue script
if (EngineShard::tlocal() && EngineShard::tlocal()->shard_id() == sid)
cb();
else
ess->Await(sid, cb);

if (cursor == 0) {
++sid;
Expand Down
9 changes: 7 additions & 2 deletions src/server/multi_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,13 @@ TEST_F(MultiTest, Eval) {
resp = Run({"eval", "return redis.call('get', 'foo')", "0"});
EXPECT_THAT(resp, "42");

Run({"script", "flush"});
Run({"script", "flush"}); // Reset global flag due to lua_allow_undeclared_auto_correct effect

resp = Run({"eval", "return redis.call('get', 'foo')", "1", "bar"});
EXPECT_THAT(resp, ErrArg("undeclared"));
ASSERT_FALSE(service_->IsLocked(0, "foo"));

Run({"script", "flush"});
Run({"script", "flush"}); // Reset global flag from autocorrect

resp = Run({"eval", "return redis.call('get', 'foo')", "1", "foo"});
EXPECT_THAT(resp, "42");
Expand Down Expand Up @@ -459,6 +459,11 @@ TEST_F(MultiTest, Eval) {
"1", "foo"}),
"42");
fb.Join();

// Call multi-shard command scan from single shard mode
resp = Run({"eval", "return redis.call('scan', '0'); ", "1", "key"});
EXPECT_EQ(resp.GetVec()[0], "0");
EXPECT_EQ(resp.GetVec()[1].type, RespExpr::Type::ARRAY);
}
#endif

Expand Down

0 comments on commit ca98411

Please sign in to comment.