From 5a698be45c893d1986c50b704e722a26ced14df6 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Tue, 8 Jan 2019 14:39:26 +0200 Subject: [PATCH] MXS-2218 Centralize worker shutdown handling --- include/maxscale/mainworker.hh | 11 ++++++++++- server/core/gateway.cc | 16 ++-------------- server/core/mainworker.cc | 6 ++++++ server/core/misc.cc | 6 ++++++ 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/include/maxscale/mainworker.hh b/include/maxscale/mainworker.hh index 61ab57fbb..1ae2ecbec 100644 --- a/include/maxscale/mainworker.hh +++ b/include/maxscale/mainworker.hh @@ -36,7 +36,16 @@ public: ~MainWorker(); /** - * Return the main worker. + * Does the main worker exist. It is only at startup and shutdown that this + * function may return false. When MaxScale is running normally, it will + * always return true. + * + * @return True, if the main worker has been created, false otherwise. + */ + static bool created(); + + /** + * Returns the main worker. * * @return The main worker. */ diff --git a/server/core/gateway.cc b/server/core/gateway.cc index e6d8ef8a7..336f56bdd 100644 --- a/server/core/gateway.cc +++ b/server/core/gateway.cc @@ -151,7 +151,6 @@ static bool log_to_shm_configured = false; static volatile sig_atomic_t last_signal = 0; static bool unload_modules_at_exit = true; static std::string redirect_output_to; -static maxscale::MainWorker* main_worker = nullptr; static int cnf_preparser(void* data, const char* section, const char* name, const char* value); static int write_pid_file(); /* write MaxScale pidfile */ @@ -353,11 +352,6 @@ static void sigterm_handler(int i) if (n_shutdowns == 1) { - if (main_worker) - { - main_worker->shutdown(); - } - if (!daemon_mode) { if (write(STDERR_FILENO, shutdown_msg, sizeof(shutdown_msg) - 1) == -1) @@ -379,11 +373,6 @@ static void sigint_handler(int i) if (n_shutdowns == 1) { - if (main_worker) - { - main_worker->shutdown(); - } - if (!daemon_mode) { if (write(STDERR_FILENO, shutdown_msg, sizeof(shutdown_msg) - 1) == -1) @@ -1422,6 +1411,7 @@ int main(int argc, char** argv) mxb::Worker* worker; const char* specified_user = NULL; char export_cnf[PATH_MAX + 1] = ""; + maxscale::MainWorker* main_worker = nullptr; /** * The following lambda function is executed as the first event on the main worker. This is what starts @@ -1441,9 +1431,7 @@ int main(int argc, char** argv) const char* logerr = "Failed to start all MaxScale services. Exiting."; print_log_n_stderr(true, true, logerr, logerr, 0); rc = MAXSCALE_NOSERVICES; - RoutingWorker::shutdown_all(); - mxb_assert(main_worker); - main_worker->shutdown(); + maxscale_shutdown(); } else if (daemon_mode) { diff --git a/server/core/mainworker.cc b/server/core/mainworker.cc index 744000d5c..c68e9f10b 100644 --- a/server/core/mainworker.cc +++ b/server/core/mainworker.cc @@ -45,6 +45,12 @@ MainWorker::~MainWorker() this_unit.pCurrent_main = nullptr; } +//static +bool MainWorker::created() +{ + return this_unit.pCurrent_main ? true : false; +} + //static MainWorker& MainWorker::get() { diff --git a/server/core/misc.cc b/server/core/misc.cc index 71be05061..bbdd8c919 100644 --- a/server/core/misc.cc +++ b/server/core/misc.cc @@ -15,6 +15,7 @@ #include +#include #include #include "internal/maxscale.hh" @@ -50,6 +51,11 @@ int maxscale_shutdown() if (n == 0) { + if (mxs::MainWorker::created()) + { + mxs::MainWorker::get().shutdown(); + } + mxs::RoutingWorker::shutdown_all(); }