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(core): fix geth --init temp dir race condition #2068

Merged
merged 2 commits into from Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethers-core/Cargo.toml
Expand Up @@ -34,6 +34,7 @@ hex = { version = "0.4.3", default-features = false, features = ["std"] }
once_cell = { version = "1.17.0", optional = true }
unicode-xid = "0.2.4"
strum = { version = "0.24", features = ["derive"] }
tempfile = { version = "3.3.0", default-features = false }

# macros feature enabled dependencies
cargo_metadata = { version = "0.15.2", optional = true }
Expand All @@ -45,7 +46,6 @@ proc-macro2 = { version = "1.0.50", optional = true }
num_enum = "0.5.7"

[dev-dependencies]
tempfile = { version = "3.3.0", default-features = false }
serde_json = { version = "1.0.64", default-features = false }
bincode = { version = "1.3.3", default-features = false }
once_cell = { version = "1.17.0" }
Expand Down
12 changes: 10 additions & 2 deletions ethers-core/src/utils/geth.rs
Expand Up @@ -6,13 +6,13 @@ use crate::{
utils::secret_key_to_address,
};
use std::{
env::temp_dir,
fs::{create_dir, File},
io::{BufRead, BufReader},
path::PathBuf,
process::{Child, ChildStderr, Command, Stdio},
time::{Duration, Instant},
};
use tempfile::tempdir;

/// How long we will wait for geth to indicate that it is ready.
const GETH_STARTUP_TIMEOUT_MILLIS: u64 = 10_000;
Expand Down Expand Up @@ -402,7 +402,11 @@ impl Geth {

if let Some(ref genesis) = self.genesis {
// create a temp dir to store the genesis file
let temp_genesis_path = temp_dir().join("genesis.json");
let temp_genesis_dir_path =
tempdir().expect("should be able to create temp dir for genesis init").into_path();

// create a temp dir to store the genesis file
let temp_genesis_path = temp_genesis_dir_path.join("genesis.json");

// create the genesis file
let mut file = File::create(&temp_genesis_path).expect("could not create genesis file");
Expand All @@ -425,6 +429,10 @@ impl Geth {
.expect("failed to spawn geth init")
.wait()
.expect("failed to wait for geth init to exit");

// clean up the temp dir which is now persisted
std::fs::remove_dir_all(temp_genesis_dir_path)
.expect("could not remove genesis temp dir");
}

if let Some(ref data_dir) = self.data_dir {
Expand Down