From d83cef669b81a1430c619750953603a7e71ffb4b Mon Sep 17 00:00:00 2001 From: aiwe Date: Sat, 27 May 2023 11:09:16 -0500 Subject: [PATCH] Fix shutting down walletd --- src/JsonRpcServer/JsonRpcServer.cpp | 29 +++++++++++-------- src/JsonRpcServer/JsonRpcServer.h | 9 +++--- .../PaymentServiceJsonRpcServer.cpp | 2 +- src/PaymentGate/PaymentServiceJsonRpcServer.h | 2 +- src/PaymentGateService/PaymentGateService.cpp | 2 +- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/JsonRpcServer/JsonRpcServer.cpp b/src/JsonRpcServer/JsonRpcServer.cpp index 5a2bef8d9c..a59c2efd71 100755 --- a/src/JsonRpcServer/JsonRpcServer.cpp +++ b/src/JsonRpcServer/JsonRpcServer.cpp @@ -42,7 +42,7 @@ namespace CryptoNote { -JsonRpcServer::JsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, Logging::ILogger& loggerGroup) : +JsonRpcServer::JsonRpcServer(System::Dispatcher* sys, System::Event* stopEvent, Logging::ILogger& loggerGroup) : m_dispatcher(sys), stopEvent(stopEvent), logger(loggerGroup, "JsonRpcServer"), @@ -56,16 +56,12 @@ JsonRpcServer::~JsonRpcServer() { void JsonRpcServer::start(const std::string& bindAddress, uint16_t bindPort, uint16_t bindPortSSL) { if (m_enable_ssl) { - m_workers.emplace_back(std::unique_ptr>( - new System::RemoteContext(m_dispatcher, std::bind(&JsonRpcServer::listen_ssl, this, bindAddress, bindPortSSL))) - ); + m_workers.push_back(std::thread(std::bind(&JsonRpcServer::listen_ssl, this, bindAddress, bindPortSSL))); } - m_workers.emplace_back(std::unique_ptr>( - new System::RemoteContext(m_dispatcher, std::bind(&JsonRpcServer::listen, this, bindAddress, bindPort))) - ); + m_workers.push_back(std::thread(std::bind(&JsonRpcServer::listen, this, bindAddress, bindPort))); - stopEvent.wait(); + stopEvent->wait(); } void JsonRpcServer::stop() { @@ -75,10 +71,19 @@ void JsonRpcServer::stop() { http->stop(); - m_dispatcher.remoteSpawn([this] - { - stopEvent.set(); - }); + if (m_dispatcher != nullptr) { + m_dispatcher->remoteSpawn([&]() { + if (stopEvent != nullptr) { + stopEvent->set(); + } + }); + } + + for (auto& th : m_workers) { + if (th.joinable()) { + th.join(); + } + } m_workers.clear(); } diff --git a/src/JsonRpcServer/JsonRpcServer.h b/src/JsonRpcServer/JsonRpcServer.h index 2c519be773..93fb37c103 100755 --- a/src/JsonRpcServer/JsonRpcServer.h +++ b/src/JsonRpcServer/JsonRpcServer.h @@ -20,6 +20,7 @@ #pragma once #include +#include #include "System/Dispatcher.h" #include "System/Event.h" @@ -46,7 +47,7 @@ namespace CryptoNote { class JsonRpcServer { public: - JsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, Logging::ILogger& loggerGroup); + JsonRpcServer(System::Dispatcher* sys, System::Event* stopEvent, Logging::ILogger& loggerGroup); JsonRpcServer(const JsonRpcServer&) = delete; ~JsonRpcServer(); @@ -74,13 +75,13 @@ class JsonRpcServer { void listen_ssl(const std::string address, const uint16_t port); bool authenticate(const httplib::Request& request) const; - System::Dispatcher& m_dispatcher; - System::Event& stopEvent; + System::Dispatcher* m_dispatcher; + System::Event* stopEvent; Logging::LoggerRef logger; httplib::Server* http; httplib::SSLServer* https; - std::vector>> m_workers; + std::list m_workers; std::string m_chain_file; std::string m_key_file; diff --git a/src/PaymentGate/PaymentServiceJsonRpcServer.cpp b/src/PaymentGate/PaymentServiceJsonRpcServer.cpp index 72ba1dec53..258c146321 100755 --- a/src/PaymentGate/PaymentServiceJsonRpcServer.cpp +++ b/src/PaymentGate/PaymentServiceJsonRpcServer.cpp @@ -32,7 +32,7 @@ namespace PaymentService { -PaymentServiceJsonRpcServer::PaymentServiceJsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, WalletService& service, Logging::ILogger& loggerGroup) +PaymentServiceJsonRpcServer::PaymentServiceJsonRpcServer(System::Dispatcher* sys, System::Event* stopEvent, WalletService& service, Logging::ILogger& loggerGroup) : JsonRpcServer(sys, stopEvent, loggerGroup) , service(service) , logger(loggerGroup, "PaymentServiceJsonRpcServer") diff --git a/src/PaymentGate/PaymentServiceJsonRpcServer.h b/src/PaymentGate/PaymentServiceJsonRpcServer.h index dbb27439b0..40bc32e7f7 100644 --- a/src/PaymentGate/PaymentServiceJsonRpcServer.h +++ b/src/PaymentGate/PaymentServiceJsonRpcServer.h @@ -34,7 +34,7 @@ class WalletService; class PaymentServiceJsonRpcServer : public CryptoNote::JsonRpcServer { public: - PaymentServiceJsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, WalletService& service, Logging::ILogger& loggerGroup); + PaymentServiceJsonRpcServer(System::Dispatcher* sys, System::Event* stopEvent, WalletService& service, Logging::ILogger& loggerGroup); PaymentServiceJsonRpcServer(const PaymentServiceJsonRpcServer&) = delete; protected: diff --git a/src/PaymentGateService/PaymentGateService.cpp b/src/PaymentGateService/PaymentGateService.cpp index fc75fc844b..18c13c2532 100755 --- a/src/PaymentGateService/PaymentGateService.cpp +++ b/src/PaymentGateService/PaymentGateService.cpp @@ -325,7 +325,7 @@ void PaymentGateService::runWalletService(const CryptoNote::Currency& currency, } } else { - PaymentService::PaymentServiceJsonRpcServer rpcServer(*dispatcher, *stopEvent, *service, logger); + PaymentService::PaymentServiceJsonRpcServer rpcServer(dispatcher, stopEvent, *service, logger); bool rpc_run_ssl = false; std::string rpc_chain_file = "";