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
- Create the clusters.
Utilized the script
create-clusterprovided in utils.
$ ./utils/create-cluster/create-cluster start
$ ./utils/create-cluster/create-cluster create
- Load the module.
Utilized
redis-cli. UsingMODULE LOADto 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
- 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.
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.