From 26533026c83d666edd5ace349008b4824d385382 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Sat, 31 Jul 2021 12:42:43 +1000 Subject: [PATCH] Avoid panics from `Instant::elapsed` Signed-off-by: Michael Sproul --- src/histogram.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/histogram.rs b/src/histogram.rs index a11e7026..025c81d9 100644 --- a/src/histogram.rs +++ b/src/histogram.rs @@ -517,7 +517,8 @@ impl Instant { pub fn elapsed(&self) -> Duration { match &*self { - Instant::Monotonic(i) => i.elapsed(), + // We use `saturating_duration_since` to avoid panics caused by non-monotonic clocks. + Instant::Monotonic(i) => StdInstant::now().saturating_duration_since(*i), // It is different from `Instant::Monotonic`, the resolution here is millisecond. // The processors in an SMP system do not start all at exactly the same time