Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix access without lock in active_transactions::confirm_block function #4230

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading