time: clean up Instant overflow prevention#8128
Open
vilgotf wants to merge 1 commit into
Open
Conversation
The `Instant::far_future` ctor unnecessarily called `Instant::now` (again). Shrinking `Duration` before calling `Instant::add` achieves the same result more succinctly. Also fixed `timeout_at` using the wrong `Location` for its delay.
Contributor
Author
|
Supersedes #7193 |
Darksonn
reviewed
May 11, 2026
Comment on lines
-126
to
-129
| match Instant::now().checked_add(duration) { | ||
| Some(deadline) => Sleep::new_timeout(deadline, location), | ||
| None => Sleep::new_timeout(Instant::far_future(), location), | ||
| } |
Member
There was a problem hiding this comment.
I think I would prefer to keep the checked_add() logic, to always perform the addition correctly when at all possible.
Contributor
Author
There was a problem hiding this comment.
The previous code has no "correct" addition, even though it looks like it. This becomes clear when you inline Instant::far_future():
const SAFE_DELAY: Duration = Duration::from_secs(86400 * 365 * 30);
let deadline = Instant::now()
.checked_add(duration)
.unwrap_or_else(|| Instant::now().checked_add(SAFE_DELAY).unwrap());This PR changes the above to:
const SAFE_DELAY: Duration = Duration::from_secs(86400 * 365 * 30);
let deadline = Instant::now().checked_add(duration.min(SAFE_DELAY)).unwrap();
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
Instant::far_futurector unnecessarily calledInstant::now(again). ShrinkingDurationbefore callingInstant::addachieves the same result more succinctly.Also fixed
timeout_atusing the wrongLocationfor its delay.