common/SigUtil.cpp | 77 +++++++++++++++++++---------------------------------- common/SigUtil.hpp | 12 ++------ wsd/Admin.cpp | 3 +- wsd/LOOLWSD.cpp | 33 +++++++++++++++------- wsd/LOOLWSD.hpp | 10 ++++++ 5 files changed, 67 insertions(+), 68 deletions(-)
New commits: commit d20c3a2db8c2b2e5550292fc9511c8e731793e84 Author: Michael Meeks <michael.me...@collabora.com> Date: Sat Mar 4 17:33:46 2017 +0000 Cleanup shutdown flag handling. Pull the notification pieces out of SigUtil.cpp - not signal safe, and invoked only from LOOLWSD anyway. In a non-blocking world, the socket close notification sends are instant - so more work required to count-down / timeout remaining clients. diff --git a/common/SigUtil.cpp b/common/SigUtil.cpp index 0be2b70..5de5721 100644 --- a/common/SigUtil.cpp +++ b/common/SigUtil.cpp @@ -36,17 +36,15 @@ #include "Socket.hpp" #include "Common.hpp" #include "Log.hpp" -#include "Util.hpp" +/// Flag to request hard termination. std::atomic<bool> TerminationFlag(false); +/// Flag to request dumping of all internal state std::atomic<bool> DumpGlobalState(false); -std::mutex SigHandlerTrap; - -/// Flag to shutdown the server. -std::atomic<bool> ShutdownFlag; - /// Flag to request WSD to notify clients and shutdown. -static std::atomic<bool> ShutdownRequestFlag(false); +std::atomic<bool> ShutdownRequestFlag(false); + +std::mutex SigHandlerTrap; namespace SigUtil { @@ -113,36 +111,43 @@ namespace SigUtil static void handleTerminationSignal(const int signal) { - // FIXME: would love a socket in all SocketPolls to handle shutdown - if (!ShutdownFlag && signal == SIGINT) + bool hardExit = false; + const char *domain; + if (!ShutdownRequestFlag && signal == SIGINT) { - Log::signalLogPrefix(); - Log::signalLog(" Shutdown signal received: "); - Log::signalLog(signalName(signal)); - Log::signalLog("\n"); - SigUtil::requestShutdown(); - SocketPoll::wakeupWorld(); + domain = " Shutdown signal received: "; + ShutdownRequestFlag = true; } else if (!TerminationFlag) { - Log::signalLogPrefix(); - Log::signalLog(" Forced-Termination signal received: "); - Log::signalLog(signalName(signal)); - Log::signalLog("\n"); + domain = " Forced-Termination signal received: "; TerminationFlag = true; - SocketPoll::wakeupWorld(); } else { - Log::signalLogPrefix(); - Log::signalLog(" ok, ok - hard-termination signal received: "); - Log::signalLog(signalName(signal)); - Log::signalLog("\n"); + domain = " ok, ok - hard-termination signal received: "; + hardExit = true; + } + Log::signalLogPrefix(); + Log::signalLog(domain); + Log::signalLog(signalName(signal)); + Log::signalLog("\n"); + + if (!hardExit) + SocketPoll::wakeupWorld(); + else + { ::signal (signal, SIG_DFL); ::raise (signal); } } + void requestShutdown() + { + ShutdownRequestFlag = true; + SocketPoll::wakeupWorld(); + } + void setTerminationSignals() { struct sigaction action; @@ -273,30 +278,6 @@ namespace SigUtil sigaction(SIGUSR1, &action, nullptr); } - - void requestShutdown() - { - ShutdownRequestFlag = true; - } - - bool handleShutdownRequest() - { - if (ShutdownRequestFlag) - { - LOG_INF("Shutdown requested. Initiating WSD shutdown."); - Util::alertAllUsers("close: shuttingdown"); - ShutdownFlag = true; - return true; - } - - return false; - } - - bool isShuttingDown() - { - return ShutdownFlag; - } - bool killChild(const int pid) { LOG_DBG("Killing PID: " << pid); diff --git a/common/SigUtil.hpp b/common/SigUtil.hpp index 9dabcfc..2b69578 100644 --- a/common/SigUtil.hpp +++ b/common/SigUtil.hpp @@ -13,6 +13,9 @@ #include <atomic> #include <mutex> +/// Flag to commence clean shutdown +extern std::atomic<bool> ShutdownRequestFlag; + /// Flag to stop pump loops. extern std::atomic<bool> TerminationFlag; @@ -48,15 +51,6 @@ namespace SigUtil /// then flags for shutdown. void requestShutdown(); - /// Checks for shutdown request and, - /// after notifying clients, flags for - /// shutting down. - /// Returns true if shutdown is requested. - bool handleShutdownRequest(); - - /// Returns true if Shutdown is in progress. - bool isShuttingDown(); - /// Kills a child process and returns true when /// child pid is removed from the process table /// after a certain (short) timeout. diff --git a/wsd/Admin.cpp b/wsd/Admin.cpp index b1e8c11..636c403 100644 --- a/wsd/Admin.cpp +++ b/wsd/Admin.cpp @@ -149,7 +149,8 @@ bool AdminRequestHandler::adminCommandHandler(const std::vector<char>& payload) else if (tokens[0] == "shutdown") { LOG_INF("Shutdown requested by admin."); - SigUtil::requestShutdown(); + ShutdownRequestFlag = true; + SocketPoll::wakeupWorld(); return false; } else if (tokens[0] == "set" && tokens.count() > 1) diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp index 8d9a3a4..8fdf280 100644 --- a/wsd/LOOLWSD.cpp +++ b/wsd/LOOLWSD.cpp @@ -1163,7 +1163,7 @@ private: return session->handleInput(payload.data(), payload.size()); }, [&session]() { session->closeFrame(); }, - []() { return TerminationFlag || SigUtil::isShuttingDown(); }); + []() { return TerminationFlag || isShuttingDown(); }); // Connection terminated. Destroy session. LOG_DBG("Client session [" << id << "] on docKey [" << docKey << "] terminated. Cleaning up."); @@ -1216,7 +1216,7 @@ private: // respond close frame ws->shutdown(); } - else if (!SigUtil::isShuttingDown()) + else if (!isShuttingDown()) { // something wrong, with internal exceptions LOG_TRC("Abnormal close handshake."); @@ -3260,7 +3260,7 @@ private: // respond with close frame _clientSession->shutdown(); } - else if (!SigUtil::isShuttingDown()) + else if (!ShutdownRequestFlag) { // something wrong, with internal exceptions LOG_TRC("Abnormal close handshake."); @@ -3361,7 +3361,7 @@ public: std::cerr << "LOOLWSDServer:\n" << " stop: " << _stop << "\n" << " TerminationFlag: " << TerminationFlag << "\n" - << " isShuttingDown: " << SigUtil::isShuttingDown() << "\n"; + << " isShuttingDown: " << ShutdownRequestFlag << "\n"; std::cerr << "Server poll:\n"; _serverPoll.dumpState(); @@ -3382,7 +3382,7 @@ private: static void runServer(std::atomic<bool>& stop, SocketPoll& serverPoll) { LOG_INF("Starting master server thread."); - while (!stop && !TerminationFlag && !SigUtil::isShuttingDown()) + while (!stop && !TerminationFlag && !ShutdownRequestFlag) { if (DumpGlobalState) { @@ -3395,7 +3395,7 @@ private: static void runDocument(std::atomic<bool>& stop, SocketPoll& documentPoll) { LOG_INF("Starting document thread."); - while (!stop && !TerminationFlag && !SigUtil::isShuttingDown()) + while (!stop && !TerminationFlag && !ShutdownRequestFlag) { documentPoll.poll(5000); } @@ -3436,6 +3436,19 @@ private: LOOLWSDServer srv; +bool LOOLWSD::handleShutdownRequest() +{ + if (ShutdownRequestFlag) + { + LOG_INF("Shutdown requested. Initiating WSD shutdown."); + Util::alertAllUsers("close: shuttingdown"); + ShutdownFlag = true; + return true; + } + + return false; +} + int LOOLWSD::main(const std::vector<std::string>& /*args*/) { #ifndef FUZZER @@ -3582,10 +3595,10 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/) #endif auto last30SecCheckTime = std::chrono::steady_clock::now(); - while (!TerminationFlag && !SigUtil::isShuttingDown()) + while (!TerminationFlag && !ShutdownRequestFlag) { UnitWSD::get().invokeTest(); - if (TerminationFlag || SigUtil::handleShutdownRequest()) + if (TerminationFlag || handleShutdownRequest()) { break; } @@ -3663,7 +3676,7 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/) // Stop the listening to new connections // and wait until sockets close. LOG_INF("Stopping server socket listening. ShutdownFlag: " << - SigUtil::isShuttingDown() << ", TerminationFlag: " << TerminationFlag); + ShutdownRequestFlag << ", TerminationFlag: " << TerminationFlag); // Wait until documents are saved and sessions closed. srv.stop(); @@ -3705,7 +3718,7 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/) FileUtil::removeFile(path, true); } - if (SigUtil::isShuttingDown()) + if (isShuttingDown()) { // At this point there should be no other thread, but... std::lock_guard<std::mutex> lock(ClientWebSocketsMutex); diff --git a/wsd/LOOLWSD.hpp b/wsd/LOOLWSD.hpp index 5126bb1..fee0091 100644 --- a/wsd/LOOLWSD.hpp +++ b/wsd/LOOLWSD.hpp @@ -51,6 +51,16 @@ public: static std::atomic<unsigned> NumConnections; static std::unique_ptr<TraceFileWriter> TraceDumper; + /// Flag to shutdown the server. + std::atomic<bool> ShutdownFlag; + + bool isShuttingDown() + { + return ShutdownFlag; + } + + bool handleShutdownRequest(); + static std::string GenSessionId() { return Util::encodeId(++NextSessionId, 4); _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits