From 0b3f0f518a21bc2e40a378dbcffff0648e472373 Mon Sep 17 00:00:00 2001 From: Wesley Shillingford Date: Tue, 9 Jul 2019 07:52:17 +0100 Subject: [PATCH] RPC stop action can take a long time to close the node down (#2135) * RPC stop action can take a long time to close node down * Formatting --- nano/nano_node/daemon.cpp | 5 ++++- nano/node/confirmation_height_processor.cpp | 16 ++++++++-------- nano/node/ipc.cpp | 3 +++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/nano/nano_node/daemon.cpp b/nano/nano_node/daemon.cpp index 578f72eba..1d4f6ede2 100644 --- a/nano/nano_node/daemon.cpp +++ b/nano/nano_node/daemon.cpp @@ -133,8 +133,11 @@ void nano_daemon::daemon::run (boost::filesystem::path const & data_path, nano:: { throw std::runtime_error ("Could not deserialize rpc_config file"); } - rpc_handler = std::make_unique (*node, config.rpc, [&ipc_server]() { + rpc_handler = std::make_unique (*node, config.rpc, [&ipc_server, &alarm, &io_ctx]() { ipc_server.stop (); + alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (3), [&io_ctx]() { + io_ctx.stop (); + }); }); rpc = nano::get_rpc (io_ctx, rpc_config, *rpc_handler); rpc->start (); diff --git a/nano/node/confirmation_height_processor.cpp b/nano/node/confirmation_height_processor.cpp index 072d5464e..b5d4e1b5e 100644 --- a/nano/node/confirmation_height_processor.cpp +++ b/nano/node/confirmation_height_processor.cpp @@ -158,6 +158,13 @@ void nano::confirmation_height_processor::add_confirmation_height (nano::block_h collect_unconfirmed_receive_and_sources_for_account (block_height, iterated_height, current, account, read_transaction); } + // Exit early when the processor has been stopped, otherwise this function may take a + // while (and hence keep the process running) if updating a long chain. + if (stopped) + { + break; + } + // No longer need the read transaction read_transaction.reset (); @@ -246,13 +253,6 @@ void nano::confirmation_height_processor::add_confirmation_height (nano::block_h } } - // Exit early when the processor has been stopped, otherwise this function may take a - // while (and hence keep the process running) if updating a long chain. - if (stopped) - { - break; - } - read_transaction.renew (); } while (!receive_source_pairs.empty () || current != hash_a); } @@ -328,7 +328,7 @@ void nano::confirmation_height_processor::collect_unconfirmed_receive_and_source // Store heights of blocks constexpr auto height_not_set = std::numeric_limits::max (); auto next_height = height_not_set; - while ((num_to_confirm > 0) && !hash.is_zero ()) + while ((num_to_confirm > 0) && !hash.is_zero () && !stopped) { nano::block_sideband sideband; auto block (store.block_get (transaction_a, hash, &sideband)); diff --git a/nano/node/ipc.cpp b/nano/node/ipc.cpp index 85c0783f1..7d119e353 100644 --- a/nano/node/ipc.cpp +++ b/nano/node/ipc.cpp @@ -129,6 +129,9 @@ public: // Note that if the rpc action is async, the shared_ptr lifetime will be extended by the action handler auto handler (std::make_shared (node, server.node_rpc_config, body, response_handler_l, [& server = server]() { server.stop (); + server.node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (3), [& io_ctx = server.node.alarm.io_ctx]() { + io_ctx.stop (); + }); })); // For unsafe actions to be allowed, the unsafe encoding must be used AND the transport config must allow it handler->process_request (allow_unsafe && config_transport.allow_unsafe);