Skip to content

Commit

Permalink
Avoid deadlock
Browse files Browse the repository at this point in the history
Storage & blockchain locks need to be taken out atomically.  This makes
Blockchain and the crappy epee lock wrapper both implement Lockable so
that std::lock can be used to establish the locks.
  • Loading branch information
jagerman authored and mbg033 committed Apr 30, 2019
1 parent 91511ea commit 588f83c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions contrib/epee/include/syncobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ namespace epee
return m_section.try_lock();
}

bool try_lock()
{
return tryLock();
}

// to make copy fake
critical_section& operator=(const critical_section& section)
{
Expand Down
5 changes: 5 additions & 0 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4334,6 +4334,11 @@ void Blockchain::unlock()
m_blockchain_lock.unlock();
}

bool Blockchain::try_lock()
{
return m_blockchain_lock.tryLock();
}

bool Blockchain::for_all_key_images(std::function<bool(const crypto::key_image&)> f) const
{
return m_db->for_all_key_images(f);
Expand Down
1 change: 1 addition & 0 deletions src/cryptonote_core/blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ namespace cryptonote

void lock();
void unlock();
bool try_lock();

void cancel();

Expand Down
4 changes: 3 additions & 1 deletion src/cryptonote_core/stake_transaction_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ void StakeTransactionProcessor::process_block(uint64_t block_index, const block&

void StakeTransactionProcessor::synchronize()
{
CRITICAL_REGION_LOCAL1(m_storage_lock);
std::unique_lock<epee::critical_section> storage_lock{m_storage_lock, std::defer_lock};
std::unique_lock<Blockchain> blockchain_lock{m_blockchain, std::defer_lock};
std::lock(storage_lock, blockchain_lock);

uint64_t height = m_blockchain.get_current_blockchain_height();

Expand Down

0 comments on commit 588f83c

Please sign in to comment.