Skip to content

Commit 82474d3

Browse files
committed
Handle monotonic clock going backwards during runtime
We've had some users complain that `duration_since` is panic'ing for them. This is possible if the machine being run on is buggy and the "monotonic clock" goes backwards, which sadly some ancient systems can do. Rust addressed this issue in 1.60 by forcing `Instant::duration_since` to not panic if the machine is buggy (and time goes backwards), but for users on older rust versions we do the same by hand here.
1 parent 36bffb5 commit 82474d3

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)