Skip to content

Commit

Permalink
fix: deadlock on StaticFileProvider::get_writer (#10069)
Browse files Browse the repository at this point in the history
Co-authored-by: joshieDo <[email protected]>
  • Loading branch information
fgimenez and joshieDo authored Aug 5, 2024
1 parent cea8b7a commit 444c7a9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
7 changes: 6 additions & 1 deletion crates/engine/tree/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ mod tests {

persistence_handle.save_blocks(blocks, tx).unwrap();

let actual_hash = rx.await.unwrap().unwrap();
let actual_hash = tokio::time::timeout(std::time::Duration::from_secs(10), rx)
.await
.expect("test timed out")
.expect("channel closed unexpectedly")
.expect("no hash returned");

assert_eq!(block_hash, actual_hash);
}

Expand Down
25 changes: 15 additions & 10 deletions crates/storage/provider/src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,6 @@ where
let first_number = first_block.number;
let last_block_number = last_block.number;

// Only write receipts to static files if there is no receipt pruning configured.
let mut state_writer = if self.database().prune_modes_ref().has_receipts_pruning() {
UnifiedStorageWriter::from_database(self.database())
} else {
UnifiedStorageWriter::from(
self.database(),
self.static_file().get_writer(first_block.number, StaticFileSegment::Receipts)?,
)
};

debug!(target: "provider::storage_writer", block_count = %blocks.len(), "Writing blocks and execution data to storage");

// TODO: remove all the clones and do performant / batched writes for each type of object
Expand All @@ -185,6 +175,21 @@ where
// Write state and changesets to the database.
// Must be written after blocks because of the receipt lookup.
let execution_outcome = block.execution_outcome().clone();

// Only write receipts to static files if there is no receipt pruning configured.
let mut state_writer = if self.database().prune_modes_ref().has_receipts_pruning() {
UnifiedStorageWriter::from_database(self.database())
} else {
// This should be inside the hotloop, because preferably there should only be one
// mutable reference to a static file writer, since there's a 3 in 100 chance that
// another segment shares the same shard as the `Receipts` one. Which would result
// in a deadlock.
UnifiedStorageWriter::from(
self.database(),
self.static_file()
.get_writer(first_block.number, StaticFileSegment::Receipts)?,
)
};
state_writer.write_to_storage(execution_outcome, OriginalValuesKnown::No)?;

// insert hashes and intermediate merkle nodes
Expand Down

0 comments on commit 444c7a9

Please sign in to comment.