From 3af9aecbec7f264983088a7e56a39f2fc2c367c3 Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 23 Jun 2026 13:26:16 +0300 Subject: [PATCH] Phase C: run RPC per-client handlers on a Void ThreadPool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the one-detached-std::thread-per-client model and its manual bookkeeping (the clientThreads_ vector, finished-thread sweep, and detach-after-timeout shutdown) with a Void::Concurrency::ThreadPool: - start() builds clientPool_ (CLIENT_POOL_SIZE workers); the accept loop submits each accepted connection's handleClient + cleanup as a task. - stop() closes the client sockets (so each handler's read() returns) then resets the pool, whose destructor drains and joins every handler. The accept loop stays on its own dedicated thread — a long-lived listen loop is not pool work, and its cancellable/timeout-guarded shutdown is unchanged. CLIENT_POOL_SIZE (64) caps concurrent long-lived client handlers far above any realistic count for this bot's RPC surface. Build green. Runtime behavior (live RPC sessions) is unverified here. Co-Authored-By: Claude Opus 4.8 --- include/echoion/core/RpcServer.hpp | 12 +++-- src/RpcServer.cpp | 74 ++++++++++-------------------- 2 files changed, 32 insertions(+), 54 deletions(-) diff --git a/include/echoion/core/RpcServer.hpp b/include/echoion/core/RpcServer.hpp index 64bd5ec..a18b32e 100644 --- a/include/echoion/core/RpcServer.hpp +++ b/include/echoion/core/RpcServer.hpp @@ -4,10 +4,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -81,9 +83,13 @@ namespace Echoion::Core std::mutex clientsMutex_; std::atomic nextClientId_{1}; - // Client threads (for proper cleanup) - std::vector clientThreads_; - std::mutex clientThreadsMutex_; + // Per-client handlers run on a Void worker pool instead of one + // detached std::thread each; ~ThreadPool drains and joins them on + // stop. Sized well above any realistic concurrent-client count + // (each long-lived handler holds a worker for the connection's + // lifetime). + static constexpr std::size_t CLIENT_POOL_SIZE = 64; + std::unique_ptr clientPool_; // Authenticated clients std::set authenticatedClients_; diff --git a/src/RpcServer.cpp b/src/RpcServer.cpp index 58101a2..dbf52a9 100644 --- a/src/RpcServer.cpp +++ b/src/RpcServer.cpp @@ -29,6 +29,8 @@ namespace Echoion::Core acceptor_->bind(endpoint); acceptor_->listen(); + clientPool_ = std::make_unique( + CLIENT_POOL_SIZE); serverThread_ = std::thread(&RpcServer::acceptLoop, this); ECHOION_LOG_INFO( @@ -69,27 +71,10 @@ namespace Echoion::Core clients_.clear(); } - // Wait for all client threads to finish (with timeout) - { - std::lock_guard lock(clientThreadsMutex_); - for (auto& thread : clientThreads_) { - if (thread.joinable()) { - // Give each thread a short time to finish - auto start = std::chrono::steady_clock::now(); - while (thread.joinable() && - std::chrono::steady_clock::now() - start < - std::chrono::milliseconds(100)) { - std::this_thread::sleep_for( - std::chrono::milliseconds(10)); - } - if (thread.joinable()) { - thread.detach(); // Only detach if still running - // after timeout - } - } - } - clientThreads_.clear(); - } + // Drain and join the client handlers. Their sockets were just + // closed, so each handleClient read() fails and the task returns; + // ~ThreadPool then joins every worker. + clientPool_.reset(); // Wait for server thread with timeout if (serverThread_.joinable()) { @@ -175,37 +160,24 @@ namespace Echoion::Core ECHOION_LOG_DEBUG( std::format("RPC client {} connected", clientId)); - // Handle client in separate thread - std::thread clientThread([this, clientId, ws]() { - handleClient(ws); + // Handle the client on the worker pool (drained/joined by + // ~ThreadPool on stop). + if (clientPool_) { + clientPool_->submit([this, clientId, ws]() { + handleClient(ws); - // Cleanup - { - std::lock_guard lock(clientsMutex_); - clients_.erase(clientId); - } - { - std::lock_guard lock(authMutex_); - authenticatedClients_.erase(clientId); - } - ECHOION_LOG_DEBUG( - std::format("RPC client {} disconnected", clientId)); - }); - - // Track the thread for proper cleanup - { - std::lock_guard lock(clientThreadsMutex_); - // Clean up any finished threads first - clientThreads_.erase(std::remove_if(clientThreads_.begin(), - clientThreads_.end(), - [](std::thread& t) { - if (!t.joinable()) { - return true; - } - return false; - }), - clientThreads_.end()); - clientThreads_.push_back(std::move(clientThread)); + // Cleanup + { + std::lock_guard lock(clientsMutex_); + clients_.erase(clientId); + } + { + std::lock_guard lock(authMutex_); + authenticatedClients_.erase(clientId); + } + ECHOION_LOG_DEBUG(std::format( + "RPC client {} disconnected", clientId)); + }); } } catch (const std::exception& e) {