Skip to content

Commit

Permalink
fix(client): avoid panics in uses of Instant (#2746)
Browse files Browse the repository at this point in the history
Even though this is almost definitely a bug in Rust, it seems most
prudent to actively avoid the uses of `Instant` that are prone to this
bug.

This change replaces uses of `Instant::elapsed` and `Instant::sub` with
calls to `Instant::saturating_duration_since` to prevent this class of
panic.
  • Loading branch information
olix0r committed Feb 1, 2022
1 parent f1b89c1 commit dcdd6d1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/client/pool.rs
Expand Up @@ -458,7 +458,9 @@ impl<T: Poolable> PoolInner<T> {
trace!("idle interval evicting closed for {:?}", key);
return false;
}
if now - entry.idle_at > dur {

// Avoid `Instant::sub` to avoid issues like rust-lang/rust#86470.
if now.saturating_duration_since(entry.idle_at) > dur {
trace!("idle interval evicting expired for {:?}", key);
return false;
}
Expand Down Expand Up @@ -721,7 +723,8 @@ impl Expiration {

fn expires(&self, instant: Instant) -> bool {
match self.0 {
Some(timeout) => instant.elapsed() > timeout,
// Avoid `Instant::elapsed` to avoid issues like rust-lang/rust#86470.
Some(timeout) => Instant::now().saturating_duration_since(instant) > timeout,
None => false,
}
}
Expand Down

0 comments on commit dcdd6d1

Please sign in to comment.