From b1a8d3b6e4eae6d04f83b4b2ead096d1429e2b6c Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 17 Jan 2017 13:36:14 +0000 Subject: [PATCH] [Fix] Fix race condition in SIGUSR2 handler MFH: true --- src/libserver/worker_util.c | 2 ++ src/rspamd.c | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index 5d0747a66..6bb97024d 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -44,6 +44,7 @@ #include #endif +static void rspamd_worker_ignore_signal (int signo); /** * Return worker's control structure by its type * @param type @@ -113,6 +114,7 @@ rspamd_worker_usr2_handler (struct rspamd_worker_signal_handler *sigh, void *arg struct timeval tv; if (!sigh->worker->wanna_die) { + rspamd_worker_ignore_signal (SIGUSR2); tv.tv_sec = SOFT_SHUTDOWN_TIME; tv.tv_usec = 0; sigh->worker->wanna_die = TRUE; diff --git a/src/rspamd.c b/src/rspamd.c index 962e67147..fd9dcdd08 100644 --- a/src/rspamd.c +++ b/src/rspamd.c @@ -915,6 +915,7 @@ rspamd_cld_handler (gint signo, short what, gpointer arg) gint res = 0; struct rspamd_worker *cur; pid_t wrk; + gboolean need_refork = TRUE; /* Turn off locking for logger */ rspamd_log_nolock (rspamd_main->logger); @@ -941,27 +942,36 @@ rspamd_cld_handler (gint signo, short what, gpointer arg) #ifdef WCOREDUMP if (WCOREDUMP (res)) { msg_warn_main ( - "%s process %P terminated abnormally by signal: %d" + "%s process %P terminated abnormally by signal: %s" " and created core file", g_quark_to_string (cur->type), cur->pid, - WTERMSIG (res)); + g_strsignal (WTERMSIG (res))); } else { msg_warn_main ( - "%s process %P terminated abnormally by signal: %d" + "%s process %P terminated abnormally by signal: %s" " but NOT created core file", g_quark_to_string (cur->type), cur->pid, - WTERMSIG (res)); + g_strsignal (WTERMSIG (res))); } #else msg_warn_main ( - "%s process %P terminated abnormally by signal: %d", + "%s process %P terminated abnormally by signal: %s", g_quark_to_string (cur->type), cur->pid, - WTERMSIG (res)); + g_strsignal (WTERMSIG (res))); #endif + if (WTERMSIG (res) == SIGUSR2) { + /* + * It is actually race condition when not started process + * has been requested to be reloaded. + * + * We shouldn't refork on this + */ + need_refork = FALSE; + } } else { msg_warn_main ("%s process %P terminated abnormally " @@ -970,9 +980,12 @@ rspamd_cld_handler (gint signo, short what, gpointer arg) cur->pid, WEXITSTATUS (res)); } - /* Fork another worker in replace of dead one */ - rspamd_check_core_limits (rspamd_main); - rspamd_fork_delayed (cur->cf, cur->index, rspamd_main); + + if (need_refork) { + /* Fork another worker in replace of dead one */ + rspamd_check_core_limits (rspamd_main); + rspamd_fork_delayed (cur->cf, cur->index, rspamd_main); + } } event_del (&cur->srv_ev); -- 2.39.5