Skip to content

Commit

Permalink
Signal handlers cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Apr 14, 2024
1 parent 9de6957 commit 1b23f51
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 28 deletions.
10 changes: 6 additions & 4 deletions nano/load_test/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <nano/boost/process/child.hpp>
#include <nano/lib/blocks.hpp>
#include <nano/lib/logging.hpp>
#include <nano/lib/signal_manager.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/lib/tomlconfig.hpp>
Expand Down Expand Up @@ -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> ();
boost::asio::io_context & ioc{ *ioc_shared };

debug_assert (!nano::signal_handler_impl);
nano::signal_handler_impl = [&ioc] () {
nano::signal_manager sigman;

auto signal_handler = [&ioc] (int signum) {
ioc.stop ();
};

std::signal (SIGINT, &nano::signal_handler);
std::signal (SIGTERM, &nano::signal_handler);
sigman.register_signal_handler (SIGINT, signal_handler, true);
sigman.register_signal_handler (SIGTERM, signal_handler, false);

tcp::resolver resolver{ ioc };
auto const primary_node_results = resolver.resolve ("::1", std::to_string (rpc_port_start));
Expand Down
7 changes: 3 additions & 4 deletions nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag
}
}

debug_assert (!nano::signal_handler_impl);
nano::signal_handler_impl = [this, io_ctx_w = std::weak_ptr{ io_ctx }] () {
auto signal_handler = [this, io_ctx_w = std::weak_ptr{ io_ctx }] (int signum) {
logger.warn (nano::log::type::daemon, "Interrupt signal received, stopping...");

if (auto io_ctx_l = io_ctx_w.lock ())
Expand All @@ -207,10 +206,10 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag
nano::signal_manager sigman;

// 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
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->join ();
Expand Down
7 changes: 3 additions & 4 deletions nano/nano_rpc/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
rpc->start ();

debug_assert (!nano::signal_handler_impl);
nano::signal_handler_impl = [io_ctx_w = std::weak_ptr{ io_ctx }] () {
auto signal_handler = [io_ctx_w = std::weak_ptr{ io_ctx }] (int signum) {
logger.warn (nano::log::type::daemon, "Interrupt signal received, stopping...");

if (auto io_ctx_l = io_ctx_w.lock ())
Expand All @@ -69,8 +68,8 @@ void run (std::filesystem::path const & data_path, std::vector<std::string> cons
sig_int_or_term = 1;
};

sigman.register_signal_handler (SIGINT, &nano::signal_handler, true);
sigman.register_signal_handler (SIGTERM, &nano::signal_handler, false);
sigman.register_signal_handler (SIGINT, signal_handler, true);
sigman.register_signal_handler (SIGTERM, signal_handler, false);

runner = std::make_unique<nano::thread_runner> (io_ctx, rpc_config.rpc_process.io_threads);
runner->join ();
Expand Down
13 changes: 0 additions & 13 deletions nano/secure/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
}
}
3 changes: 0 additions & 3 deletions nano/secure/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// Remove all unique tmp directories created by the process
void remove_temporary_directories ();
// Generic signal handler declarations
extern std::function<void ()> signal_handler_impl;
void signal_handler (int sig);
}

0 comments on commit 1b23f51

Please sign in to comment.