aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2023-05-16 16:36:05 +0200
committerGitHub <[email protected]>2023-05-16 16:36:05 +0200
commit72dd4a93de812030df9decdb338e1acd7b948e1d (patch)
tree0fd66cd142eeb2bf61dd60a457b1343501f0511f /src
parentAdditional trace instrumentation (#312) (diff)
downloadzen-72dd4a93de812030df9decdb338e1acd7b948e1d.tar.xz
zen-72dd4a93de812030df9decdb338e1acd7b948e1d.zip
exit without SIGABRT if exception is thrown during startup (#313)
* Safeguard ZenServer::RequestExit() and ZenServer::Cleanup() when partially initialized * Set exit code to non-zero if exception is thrown in ZenEntryPoint::Run() * changelog
Diffstat (limited to 'src')
-rw-r--r--src/zenserver/zenserver.cpp48
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();