From f65e58a2d2682912054e767e00692ef4df38227c Mon Sep 17 00:00:00 2001 From: clemahieu Date: Wed, 5 Apr 2023 21:44:13 +0100 Subject: [PATCH] Fixing more locking issues on bootstrap_ascending::stopped identified by TSAN (#4212) * Fixing more locking issues on bootstrap_ascending::stopped * Ensure there is an owned lock on throttle_if_needed --------- Co-authored-by: Thiago Silva --- nano/node/bootstrap/bootstrap_ascending.cpp | 12 ++++++++---- nano/node/bootstrap/bootstrap_ascending.hpp | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/nano/node/bootstrap/bootstrap_ascending.cpp b/nano/node/bootstrap/bootstrap_ascending.cpp index 24f5e9a77..e76ba90f1 100644 --- a/nano/node/bootstrap/bootstrap_ascending.cpp +++ b/nano/node/bootstrap/bootstrap_ascending.cpp @@ -581,9 +581,10 @@ void nano::bootstrap_ascending::inspect (nano::transaction const & tx, nano::pro void nano::bootstrap_ascending::wait_blockprocessor () { + nano::unique_lock lock{ mutex }; while (!stopped && block_processor.half_full ()) { - std::this_thread::sleep_for (500ms); // Blockprocessor is relatively slow, sleeping here instead of using conditions + condition.wait_for (lock, 500ms, [this] () { return stopped; }); // Blockprocessor is relatively slow, sleeping here instead of using conditions } } @@ -718,9 +719,9 @@ bool nano::bootstrap_ascending::run_one () return success; } -void nano::bootstrap_ascending::throttle_if_needed () +void nano::bootstrap_ascending::throttle_if_needed (nano::unique_lock & lock) { - nano::unique_lock lock{ mutex }; + debug_assert (lock.owns_lock ()); if (!iterator.warmup () && throttle.throttled ()) { stats.inc (nano::stat::type::bootstrap_ascending, nano::stat::detail::throttled); @@ -730,11 +731,14 @@ void nano::bootstrap_ascending::throttle_if_needed () void nano::bootstrap_ascending::run () { + nano::unique_lock lock{ mutex }; while (!stopped) { + lock.unlock (); stats.inc (nano::stat::type::bootstrap_ascending, nano::stat::detail::loop); run_one (); - throttle_if_needed (); + lock.lock (); + throttle_if_needed (lock); } } diff --git a/nano/node/bootstrap/bootstrap_ascending.hpp b/nano/node/bootstrap/bootstrap_ascending.hpp index f2f3aa57d..605574a0f 100644 --- a/nano/node/bootstrap/bootstrap_ascending.hpp +++ b/nano/node/bootstrap/bootstrap_ascending.hpp @@ -105,7 +105,7 @@ private: /* Inspects a block that has been processed by the block processor */ void inspect (nano::transaction const &, nano::process_return const & result, nano::block const & block); - void throttle_if_needed (); + void throttle_if_needed (nano::unique_lock & lock); void run (); bool run_one (); void run_timeouts ();