Skip to content

Commit

Permalink
Fix access without lock in active_transactions::confirm_block function (
Browse files Browse the repository at this point in the history
  • Loading branch information
thsfs authored and clemahieu committed May 15, 2023
1 parent bb56ea7 commit 40bce92
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,20 +610,27 @@ bool nano::active_transactions::publish (std::shared_ptr<nano::block> const & bl
// Returns the type of election status requiring callbacks calling later
boost::optional<nano::election_status_type> nano::active_transactions::confirm_block (nano::transaction const & transaction_a, std::shared_ptr<nano::block> const & block_a)
{
auto hash (block_a->hash ());
nano::unique_lock<nano::mutex> lock{ mutex };
auto existing (blocks.find (hash));
auto const hash = block_a->hash ();
std::shared_ptr<nano::election> election = nullptr;
{
nano::lock_guard<nano::mutex> guard{ mutex };
auto existing = blocks.find (hash);
if (existing != blocks.end ())
{
election = existing->second;
}
}

boost::optional<nano::election_status_type> status_type;
if (existing != blocks.end ())
if (election)
{
lock.unlock ();
nano::unique_lock<nano::mutex> election_lock{ existing->second->mutex };
if (existing->second->status.winner && existing->second->status.winner->hash () == hash)
nano::unique_lock<nano::mutex> election_lock{ election->mutex };
if (election->status.winner && election->status.winner->hash () == hash)
{
// Determine if the block was confirmed explicitly via election confirmation or implicitly via confirmation height
if (!existing->second->status_confirmed ())
if (!election->status_confirmed ())
{
existing->second->confirm_once (election_lock, nano::election_status_type::active_confirmation_height);
election->confirm_once (election_lock, nano::election_status_type::active_confirmation_height);
status_type = nano::election_status_type::active_confirmation_height;
}
else
Expand Down

0 comments on commit 40bce92

Please sign in to comment.