summaryrefslogtreecommitdiff
path: root/tests/modules
diff options
context:
space:
mode:
authorBinbin <binloveplay1314@qq.com>2022-05-13 01:10:38 +0800
committerGitHub <noreply@github.com>2022-05-12 20:10:38 +0300
commit586a16ad7907d9742a63cfcec464be7ac54aa495 (patch)
tree726241825a61af44924b19021aeb159b74259bc2 /tests/modules
parentb16d1c2713c2894eadc5d0973b37aeff76382985 (diff)
downloadredis-586a16ad7907d9742a63cfcec464be7ac54aa495.tar.gz
Fix race in module fork kill test (#10717)
The purpose of the test is to kill the child while it is running. From the last two lines we can see the child exits before being killed. ``` - Module fork started pid: 56998 * <fork> fork child started - Killing running module fork child: 56998 * <fork> fork child exiting signal-handler (1652267501) Received SIGUSR1 in child, exiting now. ``` In this commit, we pass an argument to `fork.create` indicating how long it should sleep. For the fork kill test, we use a longer time to avoid the child exiting before being killed. Other changes: use wait_for_condition instead of hardcoded `after 250`. Unify the test for failing fork with the one for killing it (save time)
Diffstat (limited to 'tests/modules')
-rw-r--r--tests/modules/fork.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/tests/modules/fork.c b/tests/modules/fork.c
index 44e3971bd..d7a0d154f 100644
--- a/tests/modules/fork.c
+++ b/tests/modules/fork.c
@@ -23,7 +23,8 @@ void done_handler(int exitcode, int bysignal, void *user_data) {
int fork_create(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
long long code_to_exit_with;
- if (argc != 2) {
+ long long usleep_us;
+ if (argc != 3) {
RedisModule_WrongArity(ctx);
return REDISMODULE_OK;
}
@@ -34,20 +35,22 @@ int fork_create(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
}
RedisModule_StringToLongLong(argv[1], &code_to_exit_with);
+ RedisModule_StringToLongLong(argv[2], &usleep_us);
exitted_with_code = -1;
- child_pid = RedisModule_Fork(done_handler, (void*)0xdeadbeef);
- if (child_pid < 0) {
+ int fork_child_pid = RedisModule_Fork(done_handler, (void*)0xdeadbeef);
+ if (fork_child_pid < 0) {
RedisModule_ReplyWithError(ctx, "Fork failed");
return REDISMODULE_OK;
- } else if (child_pid > 0) {
+ } else if (fork_child_pid > 0) {
/* parent */
+ child_pid = fork_child_pid;
RedisModule_ReplyWithLongLong(ctx, child_pid);
return REDISMODULE_OK;
}
/* child */
RedisModule_Log(ctx, "notice", "fork child started");
- usleep(500000);
+ usleep(usleep_us);
RedisModule_Log(ctx, "notice", "fork child exiting");
RedisModule_ExitFromChild(code_to_exit_with);
/* unreachable */