From 41f42224b4533ff7c078e250f613f03728b03eb4 Mon Sep 17 00:00:00 2001 From: CodeGonshik Date: Fri, 4 Sep 2026 23:32:25 +0300 Subject: [PATCH] fix: infinite loop in server.cpp --- brazier/include/brazier/server.hpp | 3 +++ brazier/src/Server.cpp | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/brazier/include/brazier/server.hpp b/brazier/include/brazier/server.hpp index d9d41c3..894b474 100644 --- a/brazier/include/brazier/server.hpp +++ b/brazier/include/brazier/server.hpp @@ -65,6 +65,9 @@ namespace brazier { std::atomic connection_count_{ 0 }; std::atomic total_requests_{ 0 }; + std::thread stats_thread_; + std::atomic shutdown_flag_{ false }; + public: Server(const std::string& host, unsigned short port); diff --git a/brazier/src/Server.cpp b/brazier/src/Server.cpp index 39ac386..1f0ff5f 100644 --- a/brazier/src/Server.cpp +++ b/brazier/src/Server.cpp @@ -72,19 +72,20 @@ void brazier::Server::run() { Logger::log("Starting " + std::to_string(threads_count) + " worker threads", "INFO"); for (int i = 0; i < threads_count; ++i) { - threads_.emplace_back([this, i] { + threads_.emplace_back([this] { io_.run(); - }); + }); } - std::thread stats_thread([this] { - while (true) { + shutdown_flag_.store(false, std::memory_order_release); + stats_thread_ = std::thread([this] { + while (!shutdown_flag_.load(std::memory_order_acquire)) { std::this_thread::sleep_for(10s); + if (shutdown_flag_.load(std::memory_order_acquire)) break; Logger::log("STATS - Active connections: " + std::to_string(connection_count_.load()) + ", Total requests: " + std::to_string(total_requests_.load()), "INFO"); } }); - stats_thread.detach(); for (auto& t : threads_) { if (t.joinable()) { @@ -99,8 +100,14 @@ void brazier::Server::run() { } void brazier::Server::stop() { + shutdown_flag_.store(true); + work_guard_.reset(); io_.stop(); + + if (stats_thread_.joinable()) { + stats_thread_.join(); + } Logger::log("Server stopped", "INFO"); }