From bbbfe92cffe6ba861085c7eb3d21193bc141aaba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20W=C3=B3jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Mon, 15 Jan 2024 20:05:06 +0100 Subject: [PATCH] Bound depth of hinted scheduler block traversal (#4359) * Bound depth of hinted scheduler block traversal * Return early when stopped --- nano/node/scheduler/hinted.cpp | 13 +++++++++++-- nano/node/scheduler/hinted.hpp | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/nano/node/scheduler/hinted.cpp b/nano/node/scheduler/hinted.cpp index 18ae184d65..1b9a8f688b 100644 --- a/nano/node/scheduler/hinted.cpp +++ b/nano/node/scheduler/hinted.cpp @@ -61,10 +61,14 @@ bool nano::scheduler::hinted::predicate () const void nano::scheduler::hinted::activate (const nano::store::read_transaction & transaction, const nano::block_hash & hash, bool check_dependents) { + const int max_iterations = 64; + + std::set visited; std::stack stack; stack.push (hash); - while (!stack.empty ()) + int iterations = 0; + while (!stack.empty () && iterations++ < max_iterations) { transaction.refresh_if_needed (); @@ -91,7 +95,7 @@ void nano::scheduler::hinted::activate (const nano::store::read_transaction & tr auto dependents = node.ledger.dependent_blocks (transaction, *block); for (const auto & dependent_hash : dependents) { - if (!dependent_hash.is_zero ()) + if (!dependent_hash.is_zero () && visited.insert (dependent_hash).second) // Avoid visiting the same block twice { stack.push (dependent_hash); // Add dependent block to the stack } @@ -125,6 +129,11 @@ void nano::scheduler::hinted::run_iterative () for (auto const & entry : tops) { + if (stopped) + { + return; + } + if (!predicate ()) { return; diff --git a/nano/node/scheduler/hinted.hpp b/nano/node/scheduler/hinted.hpp index 9d85404665..38df0e115c 100644 --- a/nano/node/scheduler/hinted.hpp +++ b/nano/node/scheduler/hinted.hpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -80,7 +81,7 @@ class hinted final private: hinted_config const & config; - bool stopped{ false }; + std::atomic stopped{ false }; nano::condition_variable condition; mutable nano::mutex mutex; std::thread thread;