Skip to content

Commit

Permalink
Merge pull request #591 from ecpullen/system-time-race
Browse files Browse the repository at this point in the history
tough: Fix race condition in system_time
  • Loading branch information
ecpullen authored Mar 20, 2023
2 parents 64158f0 + 3078ffc commit 1b1769e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions tough/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,15 @@ pub(crate) fn encode_filename<S: AsRef<str>>(name: S) -> String {
/// Ensures that system time has not stepped backward since it was last sampled
fn system_time(datastore: &Datastore) -> Result<DateTime<Utc>> {
let file = "latest_known_time.json";
// Get 'current' system time
let sys_time = Utc::now();
// Load the latest known system time, if it exists
if let Some(Ok(latest_known_time)) = datastore
let poss_latest_known_time = datastore
.reader(file)?
.map(serde_json::from_reader::<_, DateTime<Utc>>)
{
.map(serde_json::from_reader::<_, DateTime<Utc>>);

// Get 'current' system time
let sys_time = Utc::now();

if let Some(Ok(latest_known_time)) = poss_latest_known_time {
// Make sure the sampled system time did not go back in time
ensure!(
sys_time >= latest_known_time,
Expand Down

0 comments on commit 1b1769e

Please sign in to comment.