From f81ab4a5c439a7f6bcfa64fc019e1f09529e3c3f Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Fri, 31 Oct 2025 05:43:58 -0400 Subject: [PATCH] wsd: clean up the coolwsd process cleanup Change-Id: Id1266613699f80350cc9a2098e63b3e76eca7ad7 Signed-off-by: Ashod Nakashian --- wsd/COOLWSD.cpp | 61 ++++++++++++++++++++++++++++++++----------------- wsd/COOLWSD.hpp | 4 ++-- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/wsd/COOLWSD.cpp b/wsd/COOLWSD.cpp index 02bc549911b33..0ce024af27f3f 100644 --- a/wsd/COOLWSD.cpp +++ b/wsd/COOLWSD.cpp @@ -3556,7 +3556,7 @@ std::string COOLWSD::getServerURL() #endif #endif -int COOLWSD::innerMain() +void COOLWSD::innerMain() { #if !MOBILEAPP # ifdef __linux__ @@ -3597,7 +3597,7 @@ int COOLWSD::innerMain() LOG_INF("Locale is set to " + std::string(locale)); ::setenv("LC_ALL", locale, 1); } -#endif +#endif // !IOS #if !MOBILEAPP // We use the same option set for both parent and child coolwsd, @@ -4049,19 +4049,13 @@ int COOLWSD::innerMain() JailUtil::cleanupJails(CleanupChildRoot); #endif // !MOBILEAPP - const int returnValue = UnitBase::uninit(); - - LOG_INF("Process [coolwsd] finished with exit status: " << returnValue); - - SigUtil::addActivity("finished with status " + std::to_string(returnValue)); - if constexpr (Util::isMobileApp()) - Util::forcedExit(returnValue); + { + LOG_INF("Process [coolwsd] finished with exit status: " << EXIT_OK); + Util::forcedExit(EXIT_OK); + } - return returnValue; -#else // IOS - return 0; -#endif +#endif // !IOS } std::shared_ptr COOLWSD:: getWebServerPoll () @@ -4069,10 +4063,30 @@ std::shared_ptr COOLWSD:: getWebServerPoll () return COOLWSDServer::WebServerPoll; } -void COOLWSD::cleanup([[maybe_unused]] int returnValue) +int COOLWSD::cleanup(int returnValue) { try { +#ifndef IOS + if (UnitBase::isUnitTesting()) + { + LOG_DBG("Coolwsd finished with exit status " << returnValue + << "; uninitializing UnitBase"); + const int unitReturnValue = UnitBase::uninit(); + if (unitReturnValue != EXIT_OK) + { + // Overwrite the return value if the unit-test failed. + LOG_INF("Overwriting process [coolwsd] exit status [" + << returnValue << "] with unit-test status: " << unitReturnValue); + returnValue = unitReturnValue; + } + } + + LOG_INF("Process [coolwsd] finished with exit status: " << returnValue); + + SigUtil::addActivity("finished with status " + std::to_string(returnValue)); +#endif // !IOS + COOLWSDServer::Instance.reset(); PrisonerPoll.reset(); @@ -4120,7 +4134,10 @@ void COOLWSD::cleanup([[maybe_unused]] int returnValue) catch (const std::exception& ex) { LOG_ERR("Failed to uninitialize: " << ex.what()); + throw; } + + return returnValue; } int COOLWSD::main(const std::vector& /*args*/) @@ -4129,21 +4146,23 @@ int COOLWSD::main(const std::vector& /*args*/) int returnValue = EXIT_SOFTWARE; - try { - returnValue = innerMain(); + try + { + innerMain(); } catch (const std::exception& e) { LOG_FTL("Exception: " << e.what()); - cleanup(returnValue); + cleanup(EXIT_SOFTWARE); throw; - } catch (...) { - cleanup(returnValue); + } + catch (...) + { + cleanup(EXIT_SOFTWARE); throw; } - cleanup(returnValue); - + returnValue = cleanup(EXIT_OK); LOG_INF("Process [coolwsd] finished with exit status: " << returnValue); #if CODE_COVERAGE diff --git a/wsd/COOLWSD.hpp b/wsd/COOLWSD.hpp index b676fdb310be9..d49d32ff1bbb7 100644 --- a/wsd/COOLWSD.hpp +++ b/wsd/COOLWSD.hpp @@ -281,7 +281,7 @@ class COOLWSD final : public Poco::Util::ServerApplication, int main(const std::vector& args) override; /// Handle various global static destructors. - static void cleanup(int returnValue); + static int cleanup(int returnValue); private: #if !MOBILEAPP @@ -297,7 +297,7 @@ class COOLWSD final : public Poco::Util::ServerApplication, void innerInitialize(Application& self); /// The actual main implementation. - int innerMain(); + void innerMain(); static void appendAllowedHostsFrom(Poco::Util::LayeredConfiguration& conf, const std::string& root, std::vector& allowed); static void appendAllowedAliasGroups(Poco::Util::LayeredConfiguration& conf, std::vector& allowed);