From f3272db7f703a7c9f6c43107fc14785d41ff2fba Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Tue, 15 Dec 2020 10:52:22 -0800 Subject: [PATCH] Prevent multiple test-validators from using the same ledger directory --- Cargo.lock | 12 +++++++++ validator/Cargo.toml | 1 + validator/src/bin/solana-test-validator.rs | 31 ++++++++++++++-------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8e3198e82..975547dd1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1085,6 +1085,17 @@ dependencies = [ "ieee754", ] +[[package]] +name = "fd-lock" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a15bec795244d49f5ee3024bdc6c3883fb035f7f6601d4a4821c3d5d60784454" +dependencies = [ + "failure", + "libc", + "winapi 0.3.8", +] + [[package]] name = "feature-probe" version = "0.1.1" @@ -5096,6 +5107,7 @@ dependencies = [ "chrono", "clap", "console", + "fd-lock", "indicatif", "libc", "log 0.4.11", diff --git a/validator/Cargo.toml b/validator/Cargo.toml index 60e34f949f..e1b9aaa2b8 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -14,6 +14,7 @@ base64 = "0.12.3" clap = "2.33.1" chrono = { version = "0.4.11", features = ["serde"] } console = "0.11.3" +fd-lock = "1.1.1" indicatif = "0.15.0" log = "0.4.11" rand = "0.7.0" diff --git a/validator/src/bin/solana-test-validator.rs b/validator/src/bin/solana-test-validator.rs index a57b07c83f..43d1775a06 100644 --- a/validator/src/bin/solana-test-validator.rs +++ b/validator/src/bin/solana-test-validator.rs @@ -188,19 +188,28 @@ fn main() { } } + if !ledger_path.exists() { + fs::create_dir(&ledger_path).unwrap_or_else(|err| { + eprintln!( + "Error: Unable to create directory {}: {}", + ledger_path.display(), + err + ); + exit(1); + }) + } + + let mut ledger_fd_lock = fd_lock::FdLock::new(std::fs::File::open(&ledger_path).unwrap()); + let _ledger_lock = ledger_fd_lock.try_lock().unwrap_or_else(|_| { + eprintln!( + "Error: Unable to lock {} directory. Check if another solana-test-validator is running", + ledger_path.display() + ); + exit(1); + }); + let validator_log_symlink = ledger_path.join("validator.log"); let logfile = if output != Output::Log { - if !ledger_path.exists() { - fs::create_dir(&ledger_path).unwrap_or_else(|err| { - eprintln!( - "Error: Unable to create directory {}: {}", - ledger_path.display(), - err - ); - exit(1); - }) - } - let validator_log_with_timestamp = format!( "validator-{}.log", SystemTime::now()