diff --git a/nano/nano_node/daemon.cpp b/nano/nano_node/daemon.cpp index 1c0d81051e..30356ee492 100644 --- a/nano/nano_node/daemon.cpp +++ b/nano/nano_node/daemon.cpp @@ -56,8 +56,6 @@ void install_abort_signal_handler () #endif } -volatile sig_atomic_t sig_int_or_term = 0; - constexpr std::size_t OPEN_FILE_DESCRIPTORS_LIMIT = 16384; } @@ -241,5 +239,5 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag logger.critical (nano::log::type::daemon, "Error deserializing config: {}", error.get_message ()); } - logger.info (nano::log::type::daemon, "Daemon exiting"); + logger.info (nano::log::type::daemon, "Daemon stopped"); } diff --git a/nano/nano_rpc/entry.cpp b/nano/nano_rpc/entry.cpp index 30dedb3492..0d1316172e 100644 --- a/nano/nano_rpc/entry.cpp +++ b/nano/nano_rpc/entry.cpp @@ -15,10 +15,10 @@ #include #include +#include + namespace { -volatile sig_atomic_t sig_int_or_term = 0; - nano::logger logger{ "rpc_daemon" }; void run (std::filesystem::path const & data_path, std::vector const & config_overrides) @@ -41,7 +41,7 @@ void run (std::filesystem::path const & data_path, std::vector cons error = nano::read_tls_config_toml (data_path, *tls_config, logger); if (error) { - logger.critical (nano::log::type::daemon, "Error reading RPC TLS config: {}", error.get_message ()); + logger.critical (nano::log::type::daemon_rpc, "Error reading RPC TLS config: {}", error.get_message ()); std::exit (1); } else @@ -51,42 +51,42 @@ void run (std::filesystem::path const & data_path, std::vector cons std::shared_ptr io_ctx = std::make_shared (); - nano::signal_manager sigman; + runner = std::make_unique (io_ctx, rpc_config.rpc_process.io_threads); + try { nano::ipc_rpc_processor ipc_rpc_processor (*io_ctx, rpc_config); auto rpc = nano::get_rpc (io_ctx, rpc_config, ipc_rpc_processor); rpc->start (); - auto signal_handler = [io_ctx_w = std::weak_ptr{ io_ctx }] (int signum) { - logger.warn (nano::log::type::daemon, "Interrupt signal received, stopping..."); + std::latch latch{ 1 }; - if (auto io_ctx_l = io_ctx_w.lock ()) - { - io_ctx_l->stop (); - } - sig_int_or_term = 1; + auto signal_handler = [&latch] (int signum) { + logger.warn (nano::log::type::daemon_rpc, "Interrupt signal received ({}), stopping...", nano::to_signal_name (signum)); + latch.count_down (); }; + nano::signal_manager sigman; sigman.register_signal_handler (SIGINT, signal_handler, true); sigman.register_signal_handler (SIGTERM, signal_handler, false); - runner = std::make_unique (io_ctx, rpc_config.rpc_process.io_threads); - runner->join (); + // Keep running until latch is triggered + latch.wait (); + + logger.info (nano::log::type::daemon_rpc, "Stopping..."); - if (sig_int_or_term == 1) - { - rpc->stop (); - } + rpc->stop (); + io_ctx->stop (); + runner->join (); } catch (std::runtime_error const & e) { - logger.critical (nano::log::type::daemon, "Error while running RPC: {}", e.what ()); + logger.critical (nano::log::type::daemon_rpc, "Error while running RPC: {}", e.what ()); } } else { - logger.critical (nano::log::type::daemon, "Error deserializing config: {}", error.get_message ()); + logger.critical (nano::log::type::daemon_rpc, "Error deserializing config: {}", error.get_message ()); } logger.info (nano::log::type::daemon_rpc, "Daemon stopped (RPC)");