Skip to content

Commit

Permalink
Drop lockfile completely before delete
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Nov 12, 2021
1 parent c79b403 commit fe46936
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 9 additions & 5 deletions validator_client/src/initialized_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use eth2::lighthouse_vc::std_types::DeleteKeystoreStatus;
use eth2_keystore::Keystore;
use lighthouse_metrics::set_gauge;
use lockfile::{Lockfile, LockfileError};
use parking_lot::{MappedMutexGuard, Mutex, MutexGuard};
use reqwest::{Certificate, Client, Error as ReqwestError};
use slog::{debug, error, info, warn, Logger};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -109,12 +110,15 @@ pub struct InitializedValidator {

impl InitializedValidator {
/// Return a reference to this validator's lockfile if it has one.
pub fn keystore_lockfile(&self) -> Option<&Lockfile> {
pub fn keystore_lockfile(&self) -> Option<MappedMutexGuard<Lockfile>> {
match self.signing_method.as_ref() {
SigningMethod::LocalKeystore {
ref voting_keystore_lockfile,
..
} => Some(voting_keystore_lockfile),
} => MutexGuard::try_map(voting_keystore_lockfile.lock(), |option_lockfile| {
option_lockfile.as_mut()
})
.ok(),
// Web3Signer validators do not have any lockfiles.
SigningMethod::Web3Signer { .. } => None,
}
Expand Down Expand Up @@ -221,7 +225,7 @@ impl InitializedValidator {
let lockfile_path = get_lockfile_path(&voting_keystore_path)
.ok_or_else(|| Error::BadVotingKeystorePath(voting_keystore_path.clone()))?;

let voting_keystore_lockfile = Lockfile::new(lockfile_path)?;
let voting_keystore_lockfile = Mutex::new(Some(Lockfile::new(lockfile_path)?));

SigningMethod::LocalKeystore {
voting_keystore_path,
Expand Down Expand Up @@ -473,8 +477,8 @@ impl InitializedValidators {
..
} = *initialized_validator.signing_method
{
// Unlock the lock file so that it may be deleted.
voting_keystore_lockfile.unlock()?;
// Drop the lock file so that it may be deleted.
drop(voting_keystore_lockfile.lock().take());

self.delete_keystore_or_validator_dir(voting_keystore_path, voting_keystore)?;
}
Expand Down
3 changes: 2 additions & 1 deletion validator_client/src/signing_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use crate::http_metrics::metrics;
use eth2_keystore::Keystore;
use lockfile::Lockfile;
use parking_lot::Mutex;
use reqwest::Client;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -74,7 +75,7 @@ pub enum SigningMethod {
/// A validator that is defined by an EIP-2335 keystore on the local filesystem.
LocalKeystore {
voting_keystore_path: PathBuf,
voting_keystore_lockfile: Lockfile,
voting_keystore_lockfile: Mutex<Option<Lockfile>>,
voting_keystore: Keystore,
voting_keypair: Arc<Keypair>,
},
Expand Down

0 comments on commit fe46936

Please sign in to comment.