Skip to content

Commit f99301d

Browse files
authored
Merge pull request #1692 from TheBlueMatt/2022-08-time-goes-backwards
Handle monotonic clock going backwards during runtime
2 parents fc7b14b + 82474d3 commit f99301d

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lightning/src/util/time.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ impl Time for std::time::Instant {
6565
}
6666

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

7176
fn duration_since_epoch() -> Duration {

0 commit comments

Comments
 (0)