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

Fix crash when Instant::now() returns the same value twice #830

Merged
merged 2 commits into from Oct 12, 2023

Conversation

Lorak-mmk
Copy link
Collaborator

@Lorak-mmk Lorak-mmk commented Oct 11, 2023

When Instant::now() returns the same value during 2 calls of
TimestampedAverage::compute_next, division by 0 occurs,
and Duration::from_secs_f64 panics because of NaN input.

This PR fixes the issue, adds additional logs in case another conversion problem
occurs in this code and adds regression test for the issue.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

Copy link
Collaborator

@piodul piodul left a comment

Choose a reason for hiding this comment

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

Looks good, thanks. Only one thing - please swap the commits together. I know it could make some sense to introduce a failing test first and then fix it in order to better show that the fix works (e.g. I checked out the branch and saw how it behaves with and without the fix as a part of the review) - but this breaks bisectability.

@Lorak-mmk
Copy link
Collaborator Author

Done

@@ -2192,13 +2192,37 @@ mod latency_awareness {
Some(prev_avg) => Some({
let delay = (now - prev_avg.timestamp).as_secs_f64();
Copy link
Collaborator

Choose a reason for hiding this comment

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

https://doc.rust-lang.org/std/time/struct.Instant.html#monotonicity

Assuming that compute_next could be called with timestamp that is smaller than prev_avg.timestamp, this code will... work correctly as of now, but may start to panic in the future. Let's use checked_duration_since here to be sure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is the current version ok?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, it's OK. TBF I realized that saturating_duration_since would be nicer and shorter, but I won't insist on changing that (unless clippy complains)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed it, just to be safe in the future

When Instant::now() returns the same value during 2 calls of
TimestampedAverage::compute_next, division by 0 occurs,
and Duration::from_secs_f64 panics because of NaN input.

This commit fixes the issue and adds additional logging
in case another conversion problem occurs in the future.
When Instant::now() returns the same value during 2 calls of
TimestampedAverage::compute_next, division by 0 occurs,
and Duration::from_secs_f64 panics because of NaN input.

This commit adds regression test for this problem.
@piodul piodul merged commit c2746af into scylladb:main Oct 12, 2023
8 checks passed
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