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 module assertion crash when timer and timeout are unlocked in the same event loop #13015

Merged
merged 2 commits into from Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/blocked.c
Expand Up @@ -707,6 +707,11 @@ static void moduleUnblockClientOnKey(client *c, robj *key) {
* we want to remove the pending flag to indicate we already responded to the
* command with timeout reply. */
void unblockClientOnTimeout(client *c) {
if (c->bstate.btype == BLOCKED_MODULE) {
/* The client has been unlocked (in the moduleUnblocked list), return ASAP. */
if (moduleClientUnblocked(c)) return;
}
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved

replyToBlockedClientTimedOut(c);
if (c->flags & CLIENT_PENDING_COMMAND)
c->flags &= ~CLIENT_PENDING_COMMAND;
Expand Down
7 changes: 7 additions & 0 deletions src/module.c
Expand Up @@ -7698,6 +7698,13 @@ void RM_LatencyAddSample(const char *event, mstime_t latency) {
* https://redis.io/topics/modules-blocking-ops.
* -------------------------------------------------------------------------- */

/* Returns 1 if the client already in the moduleUnblocked list, 0 otherwise. */
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved
int moduleClientUnblocked(client *c) {
RedisModuleBlockedClient *bc = c->bstate.module_blocked_handle;

return bc->unblocked == 1;
}

/* This is called from blocked.c in order to unblock a client: may be called
* for multiple reasons while the client is in the middle of being blocked
* because the client is terminated, but is also called for cleanup when a
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Expand Up @@ -2532,6 +2532,7 @@ const char *moduleTypeModuleName(moduleType *mt);
const char *moduleNameFromCommand(struct redisCommand *cmd);
void moduleFreeContext(struct RedisModuleCtx *ctx);
void moduleCallCommandUnblockedHandler(client *c);
int moduleClientUnblocked(client *c);
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved
void unblockClientFromModule(client *c);
void moduleHandleBlockedClients(void);
void moduleBlockedClientTimedOut(client *c, int from_module);
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/moduleapi/blockedclient.tcl
Expand Up @@ -290,6 +290,12 @@ foreach call_type {nested normal} {
after 120
$rd close
}

test {block time is equal to timer period} {
# These time is equal, they will be unlocked in the same event loop,
# when the client is unlock, we will get the OK reply from timer.
assert_match "OK" [r unblock_by_timer 100 100]
}

test "Unload the module - blockedclient" {
assert_equal {OK} [r module unload blockedclient]
Expand Down