Skip to content

Commit

Permalink
Fix vote generator stopping (#4788)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev authored Nov 22, 2024
1 parent fa71c5c commit d843dbb
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions nano/node/vote_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,20 @@ bool nano::vote_generator::should_vote (transaction_variant_t const & transactio
void nano::vote_generator::start ()
{
debug_assert (!thread.joinable ());
thread = std::thread ([this] () { run (); });

thread = std::thread ([this] () {
nano::thread_role::set (nano::thread_role::name::voting);
run ();
});
vote_generation_queue.start ();
}

void nano::vote_generator::stop ()
{
vote_generation_queue.stop ();

nano::unique_lock<nano::mutex> lock{ mutex };
stopped = true;

lock.unlock ();
{
nano::lock_guard<nano::mutex> lock{ mutex };
stopped = true;
}
condition.notify_all ();

if (thread.joinable ())
Expand Down Expand Up @@ -284,16 +285,22 @@ void nano::vote_generator::broadcast_action (std::shared_ptr<nano::vote> const &

void nano::vote_generator::run ()
{
nano::thread_role::set (nano::thread_role::name::voting);
nano::unique_lock<nano::mutex> lock{ mutex };
while (!stopped)
{
condition.wait_for (lock, config.vote_generator_delay, [this] () { return broadcast_predicate () || !requests.empty (); });
condition.wait_for (lock, config.vote_generator_delay, [this] () {
return stopped || broadcast_predicate () || !requests.empty ();
});

if (stopped)
{
return;
}

if (broadcast_predicate ())
{
broadcast (lock);
next_broadcast = std::chrono::steady_clock::now () + std::chrono::milliseconds (config.vote_generator_delay);
next_broadcast = std::chrono::steady_clock::now () + config.vote_generator_delay;
}

if (!requests.empty ())
Expand All @@ -307,11 +314,13 @@ void nano::vote_generator::run ()

bool nano::vote_generator::broadcast_predicate () const
{
debug_assert (!mutex.try_lock ());

if (candidates.size () >= nano::network::confirm_ack_hashes_max)
{
return true;
}
if (candidates.size () > 0 && std::chrono::steady_clock::now () > next_broadcast)
if (!candidates.empty () && std::chrono::steady_clock::now () > next_broadcast)
{
return true;
}
Expand Down

0 comments on commit d843dbb

Please sign in to comment.