From f86cb67ac7f9e4739357d0f08535b0f84cee697b Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Fri, 20 Jan 2023 22:49:16 -0500 Subject: [PATCH 1/2] fix(core): fix geth --init temp dir race condition * previously, if multiple instances of geth were spawned concurrently, only one directory would be used to populate geth's genesis.json. this can lead to a race condition where the genesis.json would be re-written by a second instance, before the first instance reads the genesis.json for populating its db and genesis block with the geth --init command. this was possible because directory returned by std::env::temp_dir() is often shared * fixes the race condition by using tempfile::tempdir(), which creates a unique directory per call to tempdir() --- ethers-core/Cargo.toml | 2 +- ethers-core/src/utils/geth.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ethers-core/Cargo.toml b/ethers-core/Cargo.toml index 4c48ad1ff..c3a53d13c 100644 --- a/ethers-core/Cargo.toml +++ b/ethers-core/Cargo.toml @@ -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 } @@ -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" } diff --git a/ethers-core/src/utils/geth.rs b/ethers-core/src/utils/geth.rs index 25d7a5538..a266169af 100644 --- a/ethers-core/src/utils/geth.rs +++ b/ethers-core/src/utils/geth.rs @@ -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; @@ -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"); @@ -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 { From 3efab9c72f42bbfe90e3d3800a83b6edc3ffdf90 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Sat, 21 Jan 2023 13:42:05 -0500 Subject: [PATCH 2/2] only use tempfile on non-wasm32 --- ethers-core/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ethers-core/Cargo.toml b/ethers-core/Cargo.toml index c3a53d13c..e2513bbbf 100644 --- a/ethers-core/Cargo.toml +++ b/ethers-core/Cargo.toml @@ -34,7 +34,6 @@ 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 } @@ -45,6 +44,9 @@ syn = { version = "1.0.107", optional = true } proc-macro2 = { version = "1.0.50", optional = true } num_enum = "0.5.7" +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +tempfile = { version = "3.3.0", default-features = false } + [dev-dependencies] serde_json = { version = "1.0.64", default-features = false } bincode = { version = "1.3.3", default-features = false }