diff options
Diffstat (limited to 'src/zenserver/zenserver.cpp')
| -rw-r--r-- | src/zenserver/zenserver.cpp | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/src/zenserver/zenserver.cpp b/src/zenserver/zenserver.cpp index 789b6f4a6..ac743adef 100644 --- a/src/zenserver/zenserver.cpp +++ b/src/zenserver/zenserver.cpp @@ -222,6 +222,15 @@ namespace utils { class ZenServer : public IHttpStatusProvider { public: + ~ZenServer() + { + m_IoContext.stop(); + if (m_IoRunner.joinable()) + { + m_IoRunner.join(); + } + } + int Initialize(const ZenServerOptions& ServerOptions, ZenServerState::ZenServerEntry* ServerEntry) { m_UseSentry = ServerOptions.NoSentry == false; @@ -489,14 +498,27 @@ public: void RequestExit(int ExitCode) { RequestApplicationExit(ExitCode); - m_Http->RequestExit(); + if (m_Http) + { + m_Http->RequestExit(); + } } void Cleanup() { ZEN_INFO(ZEN_APP_NAME " cleaning up"); - m_GcScheduler.Shutdown(); - m_Http->Close(); + try + { + m_GcScheduler.Shutdown(); + if (m_Http) + { + m_Http->Close(); + } + } + catch (std::exception& Ex) + { + ZEN_ERROR("exception thrown during Cleanup() in {}: '{}'", ZEN_APP_NAME, Ex.what()); + } } void SetDedicatedMode(bool State) { m_IsDedicatedMode = State; } @@ -1209,6 +1231,8 @@ ZenEntryPoint::Run() Server.SetTestMode(ServerOptions.IsTest); Server.SetDedicatedMode(ServerOptions.IsDedicated); + auto ServerCleanup = zen::MakeGuard([&Server] { Server.Cleanup(); }); + int EffectiveBasePort = Server.Initialize(ServerOptions, Entry); Entry->EffectiveListenPort = uint16_t(EffectiveBasePort); @@ -1242,6 +1266,16 @@ ZenEntryPoint::Run() ZEN_INFO("shutdown signal wait() failed"); } }}); + auto CleanupShutdown = zen::MakeGuard([&ShutdownEvent, &ShutdownThread] { + if (ShutdownEvent) + { + ShutdownEvent->Set(); + } + if (ShutdownThread && ShutdownThread->joinable()) + { + ShutdownThread->join(); + } + }); // If we have a parent process, establish the mechanisms we need // to be able to communicate readiness with the parent @@ -1259,14 +1293,14 @@ ZenEntryPoint::Run() }); Server.Run(); - Server.Cleanup(); - - ShutdownEvent->Set(); - ShutdownThread->join(); } catch (std::exception& e) { SPDLOG_CRITICAL("Caught exception in main: {}", e.what()); + if (!IsApplicationExitRequested()) + { + RequestApplicationExit(1); + } } ShutdownLogging(); |