diff --git a/lib/config.cpp b/lib/config.cpp index d2bc7392..790d1844 100644 --- a/lib/config.cpp +++ b/lib/config.cpp @@ -576,6 +576,11 @@ void Util::Config::activate(){ is_active = true; } +tthread::mutex * mutabort = 0; +void Util::Config::setMutexAborter(void * mutex){ + mutabort = (tthread::mutex*)mutex; +} + /// Basic signal handler. Sets is_active to false if it receives /// a SIGINT, SIGHUP or SIGTERM signal, reaps children for the SIGCHLD /// signal, and ignores all other signals. @@ -584,8 +589,11 @@ void Util::Config::signal_handler(int signum, siginfo_t *sigInfo, void *ignore){ case SIGINT: // these three signals will set is_active to false. case SIGHUP: case SIGTERM: - if (serv_sock_pointer){serv_sock_pointer->close();} - if (stdin){fclose(stdin);} + if (!mutabort || mutabort->try_lock()){ + if (serv_sock_pointer){serv_sock_pointer->close();} + if (stdin){fclose(stdin);} + if (mutabort){mutabort->unlock();} + } #if DEBUG >= DLVL_DEVEL static int ctr = 0; if (!is_active && ++ctr > 4){BACKTRACE;} diff --git a/lib/config.h b/lib/config.h index d5b66ce6..819f9e63 100644 --- a/lib/config.h +++ b/lib/config.h @@ -36,6 +36,7 @@ namespace Util{ static void signal_handler(int signum, siginfo_t *sigInfo, void *ignore); public: + static void setMutexAborter(void * mutex); // variables static bool is_active; ///< Set to true by activate(), set to false by the signal handler. static bool is_restarting; ///< Set to true when restarting, set to false on boot. diff --git a/src/controller/controller.cpp b/src/controller/controller.cpp index 02cfc2e8..4975836e 100644 --- a/src/controller/controller.cpp +++ b/src/controller/controller.cpp @@ -403,6 +403,7 @@ int main_loop(int argc, char **argv){ Controller::updateBandwidthConfig(); createAccount(Controller::conf.getString("account")); Controller::conf.activate(); // activate early, so threads aren't killed. + Controller::conf.setMutexAborter(&Controller::configMutex); #if !defined(__CYGWIN__) && !defined(_WIN32) { @@ -630,6 +631,7 @@ int main(int argc, char **argv){ Controller::conf = Util::Config(argv[0]); Util::Config::binaryType = Util::CONTROLLER; Controller::conf.activate(); + Controller::conf.setMutexAborter(&Controller::configMutex); if (getenv("ATHEIST")){return main_loop(argc, argv);} uint64_t reTimer = 0; while (Controller::conf.is_active){