From eb6accad40577cd11846c4f5391c6351eb46d7b6 Mon Sep 17 00:00:00 2001 From: "Meir Shpilraien (Spielrein)" Date: Wed, 12 Oct 2022 13:09:51 +0300 Subject: Fix crash on RM_Call inside module load (#11346) PR #9320 introduces initialization order changes. Now cluster is initialized after modules. This changes causes a crash if the module uses RM_Call inside the load function on cluster mode (the code will try to access `server.cluster` which at this point is NULL). To solve it, separate cluster initialization into 2 phases: 1. Structure initialization that happened before the modules initialization 2. Listener initialization that happened after. Test was added to verify the fix. --- tests/modules/basics.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests/modules') diff --git a/tests/modules/basics.c b/tests/modules/basics.c index 55b36fa5e..b6f9766c6 100644 --- a/tests/modules/basics.c +++ b/tests/modules/basics.c @@ -893,6 +893,28 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) if (RedisModule_Init(ctx,"test",1,REDISMODULE_APIVER_1) == REDISMODULE_ERR) return REDISMODULE_ERR; + /* Perform RM_Call inside the RedisModule_OnLoad + * to verify that it works as expected without crashing. + * The tests will verify it on different configurations + * options (cluster/no cluster). A simple ping command + * is enough for this test. */ + RedisModuleCallReply *reply = RedisModule_Call(ctx, "ping", ""); + if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) { + RedisModule_FreeCallReply(reply); + return REDISMODULE_ERR; + } + size_t len; + const char *reply_str = RedisModule_CallReplyStringPtr(reply, &len); + if (len != 4) { + RedisModule_FreeCallReply(reply); + return REDISMODULE_ERR; + } + if (memcmp(reply_str, "PONG", 4) != 0) { + RedisModule_FreeCallReply(reply); + return REDISMODULE_ERR; + } + RedisModule_FreeCallReply(reply); + if (RedisModule_CreateCommand(ctx,"test.call", TestCall,"write deny-oom",1,1,1) == REDISMODULE_ERR) return REDISMODULE_ERR; -- cgit v1.2.1