Skip to content

Commit

Permalink
Capture SIGINT and SIGTERM to clean up sockets (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
wezrule authored and Russel Waters committed Jun 11, 2019
1 parent c04591e commit c56941a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nano/load_test/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <boost/dll/runtime_symbol_info.hpp>
#include <boost/program_options.hpp>

#include <csignal>
#include <iomanip>
#include <random>

Expand Down Expand Up @@ -476,6 +477,15 @@ int main (int argc, char * const * argv)
std::cout << "Connecting nodes..." << std::endl;

boost::asio::io_context ioc;

assert (!nano::signal_handler_impl);
nano::signal_handler_impl = [&ioc]() {
ioc.stop ();
};

std::signal (SIGINT, &nano::signal_handler);
std::signal (SIGTERM, &nano::signal_handler);

tcp::resolver resolver{ ioc };
auto const primary_node_results = resolver.resolve ("::1", std::to_string (rpc_port_start));

Expand Down
14 changes: 14 additions & 0 deletions nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ void nano_daemon::daemon::run (boost::filesystem::path const & data_path, nano::
}
}

assert (!nano::signal_handler_impl);
nano::signal_handler_impl = [&io_ctx, &ipc_server, &rpc, &node]() {
ipc_server.stop ();
node->stop ();
if (rpc)
{
rpc->stop ();
}
io_ctx.stop ();
};

std::signal (SIGINT, &nano::signal_handler);
std::signal (SIGTERM, &nano::signal_handler);

runner = std::make_unique<nano::thread_runner> (io_ctx, node->config.io_threads);
runner->join ();
#if BOOST_PROCESS_SUPPORTED
Expand Down
12 changes: 12 additions & 0 deletions nano/nano_rpc/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <boost/log/utility/setup/file.hpp>
#include <boost/program_options.hpp>

#include <csignal>

namespace
{
void logging_init (boost::filesystem::path const & application_path_a)
Expand Down Expand Up @@ -49,6 +51,16 @@ void run (boost::filesystem::path const & data_path)
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 ();

assert (!nano::signal_handler_impl);
nano::signal_handler_impl = [&io_ctx, &rpc]() {
rpc->stop ();
io_ctx.stop ();
};

std::signal (SIGINT, &nano::signal_handler);
std::signal (SIGTERM, &nano::signal_handler);

runner = std::make_unique<nano::thread_runner> (io_ctx, rpc_config.rpc_process.io_threads);
runner->join ();
}
Expand Down
14 changes: 14 additions & 0 deletions nano/secure/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@ 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 ();
}
std::exit (sig);
}
}
3 changes: 3 additions & 0 deletions nano/secure/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ bool migrate_working_path (std::string &);
boost::filesystem::path unique_path ();
// 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 c56941a

Please sign in to comment.