From 78bbb9b58d7e7b0816f11fe1d4c97ad5a37df01e Mon Sep 17 00:00:00 2001 From: Oran Agra Date: Sun, 25 Aug 2019 10:11:48 +0300 Subject: [PATCH] Modlue fork is killed when the parent exists --- src/module.c | 26 +++++++++++++++----------- src/server.c | 6 ++++++ src/server.h | 1 + 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/module.c b/src/module.c index 03cc3670..6f3be61a 100644 --- a/src/module.c +++ b/src/module.c @@ -5086,20 +5086,11 @@ int RM_ExitFromChild(int retcode) return REDISMODULE_OK; } -/* Can be used to kill the forked child process from the parent process. - * child_pid whould be the return value of RedisModule_Fork. */ -int RM_KillForkChild(int child_pid) -{ +void TerminateModuleForkChild(int wait) { int statloc; - /* No module child? return. */ - if (server.module_child_pid == -1) return REDISMODULE_ERR; - /* Make sure the module knows the pid it wants to kill (not trying to - * randomly kill other module's forks) */ - if (server.module_child_pid != child_pid) return REDISMODULE_ERR; - /* Kill module child, wait for child exit. */ serverLog(LL_NOTICE,"Killing running module fork child: %ld", (long) server.module_child_pid); - if (kill(server.module_child_pid,SIGUSR1) != -1) { + if (kill(server.module_child_pid,SIGUSR1) != -1 && wait) { while(wait3(&statloc,0,NULL) != server.module_child_pid); } /* Reset the buffer accumulating changes while the child saves. */ @@ -5108,6 +5099,19 @@ int RM_KillForkChild(int child_pid) moduleForkInfo.done_handler_user_data = NULL; closeChildInfoPipe(); updateDictResizePolicy(); +} + +/* Can be used to kill the forked child process from the parent process. + * child_pid whould be the return value of RedisModule_Fork. */ +int RM_KillForkChild(int child_pid) +{ + /* No module child? return. */ + if (server.module_child_pid == -1) return REDISMODULE_ERR; + /* Make sure the module knows the pid it wants to kill (not trying to + * randomly kill other module's forks) */ + if (server.module_child_pid != child_pid) return REDISMODULE_ERR; + /* Kill module child, wait for child exit. */ + TerminateModuleForkChild(1); return REDISMODULE_OK; } diff --git a/src/server.c b/src/server.c index 675b638d..31418e01 100644 --- a/src/server.c +++ b/src/server.c @@ -3551,6 +3551,12 @@ int prepareForShutdown(int flags) { killRDBChild(); } + /* Kill module child if there is one. */ + if (server.module_child_pid != -1) { + serverLog(LL_WARNING,"There is a module fork child. Killing it!"); + TerminateModuleForkChild(0); + } + if (server.aof_state != AOF_OFF) { /* Kill the AOF saving child as the AOF we already have may be longer * but contains the full dataset anyway. */ diff --git a/src/server.h b/src/server.h index deaaf263..7aa4bc2b 100644 --- a/src/server.h +++ b/src/server.h @@ -1532,6 +1532,7 @@ void moduleReleaseGIL(void); void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid); void moduleCallCommandFilters(client *c); void ModuleForkDoneHandler(int exitcode, int bysignal); +void TerminateModuleForkChild(int wait); /* Utils */ long long ustime(void);