Skip to content

Commit

Permalink
Use try_read to avoid deadlock due to rayon releasing concurrent read…
Browse files Browse the repository at this point in the history
…s all at once
  • Loading branch information
vicsn committed Jan 3, 2025
1 parent 05e8904 commit 970386f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions node/bft/ledger-service/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ impl<N: Network, C: ConsensusStorage<N>> LedgerService<N> for CoreLedgerService<

/// Returns the block for the given block height.
fn get_block(&self, height: u32) -> Result<Block<N>> {
if let Some(block) = self.block_cache.read().get(&height) {
Ok(block.clone())
} else {
self.ledger.get_block(height)
// First, check if the block is in the block cache.
// Using `try_read` to avoid blocking the thread: https://github.com/rayon-rs/rayon/issues/1205
if let Some(block_cache) = self.block_cache.try_read() {
if let Some(block) = block_cache.get(&height) {
return Ok(block.clone());
}
}
// If no block is found in the cache, then retrieve the block from the ledger.
self.ledger.get_block(height)
}

/// Returns the blocks in the given block range.
Expand Down

0 comments on commit 970386f

Please sign in to comment.