Avoid panics from Instant::elapsed
#406
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 forInstant::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.