Skip to content

Commit

Permalink
Merge pull request #1692 from TheBlueMatt/2022-08-time-goes-backwards
Browse files Browse the repository at this point in the history
Handle monotonic clock going backwards during runtime
  • Loading branch information
TheBlueMatt committed Sep 3, 2022
2 parents fc7b14b + 82474d3 commit f99301d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lightning/src/util/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ impl Time for std::time::Instant {
}

fn duration_since(&self, earlier: Self) -> Duration {
self.duration_since(earlier)
// On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
// However, we support rust versions prior to 1.60 and some users appear to have "monotonic
// clocks" that go backwards in practice (likely relatively ancient kernels/etc). Thus, we
// manually check for time going backwards here and return a duration of zero in that case.
let now = Self::now();
if now > earlier { now - earlier } else { Duration::from_secs(0) }
}

fn duration_since_epoch() -> Duration {
Expand Down

0 comments on commit f99301d

Please sign in to comment.