Describe the bug

In the call back function PingReceiver, the returned RedisModuleCallReply object will not be free'd and thus become a memory leak.

To reproduce It is tested with Redis 6.2.6, redis_git_sha1: 4930d19e

  1. Create the clusters. Utilized the script create-cluster provided in utils.
$ ./utils/create-cluster/create-cluster start
$ ./utils/create-cluster/create-cluster create
  1. Load the module. Utilized redis-cli. Using MODULE LOAD to load the module.
$ ./src/redis-cli -p 30001
127.0.0.1:30001> MOUDLE LOAD ./src/modules/hellocluster.so
OK
$ ./src/redis-cli -p 30002
127.0.0.1:30001> MOUDLE LOAD ./src/modules/hellocluster.so
OK
  1. Trigger the callback.
$ ./src/redis-cli -p 30001
127.0.0.1:30001> hellocluster.pingall
OK

Then the cluster 30002 will trigger the call back.

Expected behavior

RedisModuleCall has a return value which could either be freed by RedisModule_FreeCallReply or cleaned by automatic memory management (enabled by calling RedisModule_AutoMemory at the beginning of a command).

I used gdb to check that the context didn't enable auto memory when the call back is triggered. Screenshot from 2021-11-17 16-38-44

void PingReceiver(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {
    RedisModule_Log(ctx,"notice","PING (type %d) RECEIVED from %.*s: '%.*s'",
        type,REDISMODULE_NODE_ID_LEN,sender_id,(int)len, payload);
    RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PONG,(unsigned char*)"Ohi!",4);
    RedisModule_Call(ctx, "INCR", "c", "pings_received");
}

Additional information

The bug could be fixed by:

void PingReceiver(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {
    RedisModule_Log(ctx,"notice","PING (type %d) RECEIVED from %.*s: '%.*s'",
        type,REDISMODULE_NODE_ID_LEN,sender_id,(int)len, payload);
    RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PONG,(unsigned char*)"Ohi!",4);
    RedisModuleCallReply reply = RedisModule_Call(ctx, "INCR", "c", "pings_received");
    RedisModule_FreeCallReply(reply);
}

Comment From: oranagra

@RuiliF wanna issue a PR with the fix?

Comment From: RuiliF

@RuiliF wanna issue a PR with the fix?

Thank you for your reply! I'll issue a PR later today.