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 9506a60446..4bb30cd9ae 100644 --- a/nano/node/scheduler/hinted.hpp +++ b/nano/node/scheduler/hinted.hpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -79,7 +80,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;