From 5e40e54c803845a0b833158d912ffbfaaaf4b121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:58:32 +0200 Subject: [PATCH] Graceful shutdown --- nano/node/confirming_set.cpp | 11 +++++++++-- nano/node/confirming_set.hpp | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/nano/node/confirming_set.cpp b/nano/node/confirming_set.cpp index f98baaa929..3577cc00ed 100644 --- a/nano/node/confirming_set.cpp +++ b/nano/node/confirming_set.cpp @@ -133,10 +133,11 @@ void nano::confirming_set::run_batch (std::unique_lock & lock) notification.already_cemented.swap (already); std::unique_lock lock{ mutex }; + while (notification_workers.num_queued_tasks () >= config.max_queued_notifications) { stats.inc (nano::stat::type::confirming_set, nano::stat::detail::cooldown); - condition.wait_for (lock, 100ms, [this] { return stopped; }); + condition.wait_for (lock, 100ms, [this] { return stopped.load (); }); if (stopped) { return; @@ -168,11 +169,17 @@ void nano::confirming_set::run_batch (std::unique_lock & lock) { transaction.refresh_if_needed (); - stats.inc (nano::stat::type::confirming_set, nano::stat::detail::cementing); + // Cementing deep dependency chains might take a long time, allow for graceful shutdown, ignore notifications + if (stopped) + { + break; + } // Issue notifications here, so that `cemented` set is not too large before we add more blocks notify_maybe (transaction); + stats.inc (nano::stat::type::confirming_set, nano::stat::detail::cementing); + auto added = ledger.confirm (transaction, hash, config.max_blocks); if (!added.empty ()) { diff --git a/nano/node/confirming_set.hpp b/nano/node/confirming_set.hpp index 04fbbcbb4e..2f363b51e6 100644 --- a/nano/node/confirming_set.hpp +++ b/nano/node/confirming_set.hpp @@ -75,7 +75,7 @@ class confirming_set final nano::thread_pool notification_workers; - bool stopped{ false }; + std::atomic stopped{ false }; mutable std::mutex mutex; std::condition_variable condition; std::thread thread;