Signal handlers cleanup
This commit is contained in:
parent
6f8e1d3be2
commit
71a265f50e
5 changed files with 12 additions and 28 deletions
|
|
@ -6,6 +6,7 @@
|
||||||
#include <nano/boost/process/child.hpp>
|
#include <nano/boost/process/child.hpp>
|
||||||
#include <nano/lib/blocks.hpp>
|
#include <nano/lib/blocks.hpp>
|
||||||
#include <nano/lib/logging.hpp>
|
#include <nano/lib/logging.hpp>
|
||||||
|
#include <nano/lib/signal_manager.hpp>
|
||||||
#include <nano/lib/thread_runner.hpp>
|
#include <nano/lib/thread_runner.hpp>
|
||||||
#include <nano/lib/threading.hpp>
|
#include <nano/lib/threading.hpp>
|
||||||
#include <nano/lib/tomlconfig.hpp>
|
#include <nano/lib/tomlconfig.hpp>
|
||||||
|
|
@ -595,13 +596,14 @@ int main (int argc, char * const * argv)
|
||||||
std::shared_ptr<boost::asio::io_context> ioc_shared = std::make_shared<boost::asio::io_context> ();
|
std::shared_ptr<boost::asio::io_context> ioc_shared = std::make_shared<boost::asio::io_context> ();
|
||||||
boost::asio::io_context & ioc{ *ioc_shared };
|
boost::asio::io_context & ioc{ *ioc_shared };
|
||||||
|
|
||||||
debug_assert (!nano::signal_handler_impl);
|
nano::signal_manager sigman;
|
||||||
nano::signal_handler_impl = [&ioc] () {
|
|
||||||
|
auto signal_handler = [&ioc] (int signum) {
|
||||||
ioc.stop ();
|
ioc.stop ();
|
||||||
};
|
};
|
||||||
|
|
||||||
std::signal (SIGINT, &nano::signal_handler);
|
sigman.register_signal_handler (SIGINT, signal_handler, true);
|
||||||
std::signal (SIGTERM, &nano::signal_handler);
|
sigman.register_signal_handler (SIGTERM, signal_handler, false);
|
||||||
|
|
||||||
tcp::resolver resolver{ ioc };
|
tcp::resolver resolver{ ioc };
|
||||||
auto const primary_node_results = resolver.resolve ("::1", std::to_string (rpc_port_start));
|
auto const primary_node_results = resolver.resolve ("::1", std::to_string (rpc_port_start));
|
||||||
|
|
|
||||||
|
|
@ -193,8 +193,7 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
debug_assert (!nano::signal_handler_impl);
|
auto signal_handler = [this, io_ctx_w = std::weak_ptr{ io_ctx }] (int signum) {
|
||||||
nano::signal_handler_impl = [this, io_ctx_w = std::weak_ptr{ io_ctx }] () {
|
|
||||||
logger.warn (nano::log::type::daemon, "Interrupt signal received, stopping...");
|
logger.warn (nano::log::type::daemon, "Interrupt signal received, stopping...");
|
||||||
|
|
||||||
if (auto io_ctx_l = io_ctx_w.lock ())
|
if (auto io_ctx_l = io_ctx_w.lock ())
|
||||||
|
|
@ -207,10 +206,10 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag
|
||||||
nano::signal_manager sigman;
|
nano::signal_manager sigman;
|
||||||
|
|
||||||
// keep trapping Ctrl-C to avoid a second Ctrl-C interrupting tasks started by the first
|
// keep trapping Ctrl-C to avoid a second Ctrl-C interrupting tasks started by the first
|
||||||
sigman.register_signal_handler (SIGINT, &nano::signal_handler, true);
|
sigman.register_signal_handler (SIGINT, signal_handler, true);
|
||||||
|
|
||||||
// sigterm is less likely to come in bunches so only trap it once
|
// sigterm is less likely to come in bunches so only trap it once
|
||||||
sigman.register_signal_handler (SIGTERM, &nano::signal_handler, false);
|
sigman.register_signal_handler (SIGTERM, signal_handler, false);
|
||||||
|
|
||||||
runner = std::make_unique<nano::thread_runner> (io_ctx, node->config.io_threads);
|
runner = std::make_unique<nano::thread_runner> (io_ctx, node->config.io_threads);
|
||||||
runner->join ();
|
runner->join ();
|
||||||
|
|
|
||||||
|
|
@ -58,8 +58,7 @@ void run (std::filesystem::path const & data_path, std::vector<std::string> cons
|
||||||
auto rpc = nano::get_rpc (io_ctx, rpc_config, ipc_rpc_processor);
|
auto rpc = nano::get_rpc (io_ctx, rpc_config, ipc_rpc_processor);
|
||||||
rpc->start ();
|
rpc->start ();
|
||||||
|
|
||||||
debug_assert (!nano::signal_handler_impl);
|
auto signal_handler = [io_ctx_w = std::weak_ptr{ io_ctx }] (int signum) {
|
||||||
nano::signal_handler_impl = [io_ctx_w = std::weak_ptr{ io_ctx }] () {
|
|
||||||
logger.warn (nano::log::type::daemon, "Interrupt signal received, stopping...");
|
logger.warn (nano::log::type::daemon, "Interrupt signal received, stopping...");
|
||||||
|
|
||||||
if (auto io_ctx_l = io_ctx_w.lock ())
|
if (auto io_ctx_l = io_ctx_w.lock ())
|
||||||
|
|
@ -69,8 +68,8 @@ void run (std::filesystem::path const & data_path, std::vector<std::string> cons
|
||||||
sig_int_or_term = 1;
|
sig_int_or_term = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
sigman.register_signal_handler (SIGINT, &nano::signal_handler, true);
|
sigman.register_signal_handler (SIGINT, signal_handler, true);
|
||||||
sigman.register_signal_handler (SIGTERM, &nano::signal_handler, false);
|
sigman.register_signal_handler (SIGTERM, signal_handler, false);
|
||||||
|
|
||||||
runner = std::make_unique<nano::thread_runner> (io_ctx, rpc_config.rpc_process.io_threads);
|
runner = std::make_unique<nano::thread_runner> (io_ctx, rpc_config.rpc_process.io_threads);
|
||||||
runner->join ();
|
runner->join ();
|
||||||
|
|
|
||||||
|
|
@ -74,16 +74,3 @@ void nano::remove_temporary_directories ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace nano
|
|
||||||
{
|
|
||||||
/** A wrapper for handling signals */
|
|
||||||
std::function<void ()> signal_handler_impl;
|
|
||||||
void signal_handler (int sig)
|
|
||||||
{
|
|
||||||
if (signal_handler_impl != nullptr)
|
|
||||||
{
|
|
||||||
signal_handler_impl ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,4 @@ std::filesystem::path working_path (nano::networks network = nano::network_const
|
||||||
std::filesystem::path unique_path (nano::networks network = nano::network_constants::active_network);
|
std::filesystem::path unique_path (nano::networks network = nano::network_constants::active_network);
|
||||||
// Remove all unique tmp directories created by the process
|
// Remove all unique tmp directories created by the process
|
||||||
void remove_temporary_directories ();
|
void remove_temporary_directories ();
|
||||||
// Generic signal handler declarations
|
|
||||||
extern std::function<void ()> signal_handler_impl;
|
|
||||||
void signal_handler (int sig);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue