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 from Instant::elapsed #406

Merged
merged 1 commit into from Jul 31, 2021

Conversation

michaelsproul
Copy link
Contributor

To prevent timers from panicking when observed, this PR modifies the HistogramTimer to use saturating arithmetic.

The docs for std::time::Instant::elapsed note that it can panic if clock monotonicity is violated. Further, the source for Instant::now notes that buggy clocks on various platforms have caused issues:

https://github.com/rust-lang/rust/blob/fe1c942eee3489743d655d81ca89166217db0547/library/std/src/time.rs#L223-L250

I recently observed a panic caused by a monotonicity violation in a virtualised environment. There's a full backtrace available here: sigp/lighthouse#2485

The alternative to saturating arithmetic would be to use checked arithmetic and avoid updating the histogram in the case of a failure. However I noticed that the coarse timer saturates to 0, so figured it would be best to keep the two consistent.

If I can isolate the issue on that virtualised environment I will also file a bug report with upstream Rust.

Signed-off-by: Michael Sproul <michael@sigmaprime.io>
Copy link
Member

@breezewish breezewish left a comment

Choose a reason for hiding this comment

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

Thanks. Looks good to me.

@breezewish breezewish merged commit a93ed8c into tikv:master Jul 31, 2021
@michaelsproul
Copy link
Contributor Author

Thank you for the quick turnaround!

@michaelsproul michaelsproul deleted the panic-free-instant branch July 31, 2021 08:26
@lucab lucab mentioned this pull request Sep 27, 2021
JanBerktold pushed a commit to JanBerktold/rust-prometheus that referenced this pull request Nov 12, 2022
Signed-off-by: Michael Sproul <michael@sigmaprime.io>
Signed-off-by: Jan Berktold <jberktold@roblox.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants