Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This fixes a deadlock in the code.
The
RwLock
allows a mutex to exist that allows concurrent reads but blocks concurrent writes, this behaves differently depending on the OS.On Linux systems: The lock favors reads, meaning that as long as there exists a reader lock, it will allow a new reader lock to open. This means that the system can cause starvation of writers.
On Mac/Win the lock has equal ordering, this means that as soon as a writer queues for a lock, all additional readers will be blocked till after the writer has acquired and released its lock.
This behavior can be dangerous if recursive locks are used, as was the case here.
At about the same time, a block was submitted, and a template was constructed for a new miner.
The
add_block
process requires a write lock, while theblock template
process requires a read lock.The
template process
was first in acquiring a lock on the read, followed shortly by theadd_block
on the blocking for a write. But the deadlock was caused after theadd_block
blocked for a write, theblock template
required an additional read_lock on the calculation of the mmr roots. And thus, the entireblock_chain db
class is deadlocked.Fixes: #4668