Prevent panicking in sleep() with large durations #4495
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.
Closes #4494.
Replaced an
expect()with a graceful failure of returning u64::MAX.When looking at the usages of deadline/instant_to_tick, this new
behavior appears to be consistent with the expectations of the callers.
Motivation
A user of my crate reported a panic in code that was calling
sleep()with a large duration. The code behindsleep()does a checked_add()`, which to me implied that the code handled large durations. The documentation doesn't mention this panic as a possible outcome, despite a unit test being present to test for the failure.Solution
My initial report was that this was purely a bug, but after looking at the code the panic was intentionally added due to the resolution of Instant on some platforms. However, when looking at the code that causes the panic, it has nothing to do with platform-specific behaviors.
The code in
time/driver/entry.rsseems to only care about the relative order of the returned u64, and a value of u64::MAX seems to me to produce consistent, predictable results. So, this PR's proposed fix is to remove the panic and gracefully fail withu64::MAX.The only test broken by this change is the panic-testing unit test. After removing that one unit test, the entire test suite passes.