Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid panics in uses of Instant #2746

Merged
merged 1 commit into from Feb 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It definitely feels like Instant::elapsed() should handle this, or otherwise it's not really safe to use. Looks like rust-lang/rust#89926 agrees.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. It will probably take at least a few months for those changes to be available in a stable release (the RFC still appears to be open). This change would eliminate these problems well in advance of that, even for projects that are not on the latest stable Rust version.

Is there something I can do to make the temporary nature of this hack/fix better documented?

None => false,
}
}
Expand Down