Skip to content

Commit

Permalink
Fix TSAN error with work_watcher (#2061)
Browse files Browse the repository at this point in the history
  • Loading branch information
wezrule authored and Russel Waters committed Jun 11, 2019
1 parent 596adf2 commit d2e2eaf
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ void nano::work_watcher::run ()
next_attempt = std::chrono::steady_clock::now () + std::chrono::seconds (5);
for (auto i (blocks.begin ()), n (blocks.end ()); i != n;)
{
std::unique_lock<std::mutex> lock (node.active.mutex);
std::unique_lock<std::mutex> active_lock (node.active.mutex);
auto confirmed (false);
auto existing (node.active.roots.find ((i->second)->qualified_root ()));
if (node.active.roots.end () != existing)
Expand All @@ -1428,7 +1428,7 @@ void nano::work_watcher::run ()
confirmed = this->node.block_confirmed_or_being_confirmed (transaction, (i->second)->hash ());
}
}
lock.unlock ();
active_lock.unlock ();

if (confirmed)
{
Expand All @@ -1442,24 +1442,27 @@ void nano::work_watcher::run ()
for (auto & i : blocks)
{
uint64_t difficulty (0);
nano::work_validate (i.second->root (), i.second->block_work (), &difficulty);
auto root = i.second->root ();
nano::work_validate (root, i.second->block_work (), &difficulty);
if (node.active.active_difficulty () > difficulty)
{
lock.unlock ();
auto qualified_root = i.second->qualified_root ();
auto hash = i.second->hash ();
nano::state_block_builder builder;
std::error_code ec;
builder.from (*i.second);
builder.work (node.work_generate_blocking (i.second->root (), node.active.active_difficulty ()));
lock.unlock ();
builder.work (node.work_generate_blocking (root, node.active.active_difficulty ()));
std::shared_ptr<state_block> block (builder.build (ec));
if (!ec)
{
{
std::lock_guard<std::mutex> active_lock (node.active.mutex);
auto existing (node.active.roots.find (i.second->qualified_root ()));
auto existing (node.active.roots.find (qualified_root));
if (existing != node.active.roots.end ())
{
auto election (existing->election);
if (election->status.winner->hash () == i.second->hash ())
if (election->status.winner->hash () == hash)
{
election->status.winner = block;
}
Expand All @@ -1471,10 +1474,18 @@ void nano::work_watcher::run ()
node.network.flood_block (block);
node.active.update_difficulty (*block.get ());
lock.lock ();
if (stopped)
{
break;
}
i.second = block;
lock.unlock ();
}
lock.lock ();
if (stopped)
{
break;
}
}
}
} // !stopped
Expand Down

0 comments on commit d2e2eaf

Please sign in to comment.