]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Handle monotonic clock going backwards during runtime 2022-08-time-goes-backwards
authorMatt Corallo <git@bluematt.me>
Fri, 2 Sep 2022 21:57:32 +0000 (21:57 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 2 Sep 2022 21:57:32 +0000 (21:57 +0000)
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.

lightning/src/util/time.rs

index d3768aa7ca6441d2943c5ac080e57113ab9d957d..f450dc2c3015ae4da00faa2d609d01542753b85d 100644 (file)
@@ -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 {