-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Crashes the server when the main thread is idle for too long
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq ([email protected]) | ||
// This file is part of the "This Space Of Mine" project | ||
// For conditions of distribution and use, see copyright notice in LICENSE | ||
|
||
#pragma once | ||
|
||
#ifndef TSOM_COMMONLIB_HEALTHCHECKERAPPCOMPONENT_HPP | ||
#define TSOM_COMMONLIB_HEALTHCHECKERAPPCOMPONENT_HPP | ||
|
||
#include <CommonLib/Export.hpp> | ||
#include <Nazara/Core/ApplicationComponent.hpp> | ||
#include <atomic> | ||
#include <semaphore> | ||
#include <thread> | ||
|
||
namespace tsom | ||
{ | ||
class TSOM_COMMONLIB_API HealthCheckerAppComponent : public Nz::ApplicationComponent | ||
{ | ||
public: | ||
HealthCheckerAppComponent(Nz::ApplicationBase& app, unsigned int maxHangSeconds); | ||
HealthCheckerAppComponent(const HealthCheckerAppComponent&) = delete; | ||
HealthCheckerAppComponent(HealthCheckerAppComponent&&) = delete; | ||
~HealthCheckerAppComponent(); | ||
|
||
HealthCheckerAppComponent& operator=(const HealthCheckerAppComponent&) = delete; | ||
HealthCheckerAppComponent& operator=(HealthCheckerAppComponent&&) = delete; | ||
|
||
private: | ||
void Update(Nz::Time elapsedTime) override; | ||
|
||
std::atomic_uint m_updateCounter; | ||
std::binary_semaphore m_exitChecker; | ||
std::thread m_healthCheckThread; | ||
unsigned int m_maxHangSeconds; | ||
}; | ||
} | ||
|
||
#include <CommonLib/HealthCheckerAppComponent.inl> | ||
|
||
#endif // TSOM_COMMONLIB_HEALTHCHECKERAPPCOMPONENT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq ([email protected]) | ||
// This file is part of the "This Space Of Mine" project | ||
// For conditions of distribution and use, see copyright notice in LICENSE | ||
|
||
namespace tsom | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq ([email protected]) | ||
// This file is part of the "This Space Of Mine" project | ||
// For conditions of distribution and use, see copyright notice in LICENSE | ||
|
||
#include <CommonLib/HealthCheckerAppComponent.hpp> | ||
#include <fmt/color.h> | ||
#include <chrono> | ||
#include <csignal> | ||
|
||
namespace tsom | ||
{ | ||
HealthCheckerAppComponent::HealthCheckerAppComponent(Nz::ApplicationBase& app, unsigned int maxHangSeconds) : | ||
ApplicationComponent(app), | ||
m_updateCounter(0), | ||
m_exitChecker(0), | ||
m_maxHangSeconds(maxHangSeconds) | ||
{ | ||
m_healthCheckThread = std::thread([this] | ||
{ | ||
unsigned int lastUpdateCounter = 0; | ||
unsigned int hangCounter = 0; | ||
|
||
while (!m_exitChecker.try_acquire_for(std::chrono::seconds(1))) | ||
{ | ||
unsigned int updateCount = m_updateCounter.load(std::memory_order_relaxed); | ||
if NAZARA_LIKELY(updateCount != lastUpdateCounter) | ||
{ | ||
hangCounter = 0; | ||
lastUpdateCounter = updateCount; | ||
} | ||
else | ||
{ | ||
if (++hangCounter >= m_maxHangSeconds) | ||
{ | ||
fmt::print(fg(fmt::color::red), "main loop has been unresponsive for {} seconds, exiting...", hangCounter); | ||
std::abort(); | ||
break; //< just in case | ||
} | ||
} | ||
} | ||
|
||
// Normal exit | ||
}); | ||
} | ||
|
||
HealthCheckerAppComponent::~HealthCheckerAppComponent() | ||
{ | ||
m_exitChecker.release(); | ||
m_healthCheckThread.join(); | ||
} | ||
|
||
void HealthCheckerAppComponent::Update(Nz::Time elapsedTime) | ||
{ | ||
m_updateCounter.fetch_add(1, std::memory_order_relaxed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters