-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Subtracting Duration from Instant on Mac is panic-prone #100141
Comments
Is the situation on Windows also concerning? It uses |
To be honest, I think I agree that That said, there is definitely a use case for adding/subtracting precise times. But couldn't that be satisfied by |
You can do this instead: |
This is really surprising behavior. If |
Although monotonic and therefore safe from certain failure modes that SystemTime arithmetic can encounter, Instant's implementation is not consistent and can panic in certain implementations (rust-lang/rust#100141). Despite the risk of errors I've also (mostly) convinced myself that SystemTime is the more-correct type to use for this purpose, and my attempts to avoid it by converting them to Instants are mostly unhelpful (e.g. if the system time has changed the Instant representations would be wrong too). This PR includes a breaking API change to the `CacheStatus` enum, but should not affect CLI users.
Although monotonic and therefore safe from certain failure modes that SystemTime arithmetic can encounter, Instant's implementation is not consistent and can panic in certain implementations (rust-lang/rust#100141). Despite the risk of errors I've also (mostly) convinced myself that SystemTime is the more-correct type to use for this purpose, and my attempts to avoid it by converting them to Instants are mostly unhelpful (e.g. if the system time has changed the Instant representations would be wrong too). This PR includes a breaking API change to the `CacheStatus` enum, but should not affect CLI users. Fixes #45
An alternative is to store a signed integer inside This is a worse problem when the implementation for a given system uses the time since the program start. See sebcrozet/instant#45, for example. |
It is worth noting that subtracting two u64 converted from Instant/SystemTime may also cause issues due to the non-monotone of Instance. |
The following program:
works on Linux, but could panic on Mac. The reason for that is that, as implemented,
Instant
on mac starts at zero at reboot, so it can't represent times before the machine started.This is pretty unfortunate -- it means that
impl Sub<Duration> for Instant
isn't really usable on mac, as subtracting even a small duration can panic. And subtracing duration is pretty useful and natural too implement tasks like "periodically, cleanup data crated earlier thanInstant::now() - Duration::from_secs(60)
".While technically we don't give any guarantees on
Instant
range and document this as system-dependent, it is really unfortunate that subtracting API exists, works fine on Linux, and fails occasionally on macs. I think we should just go and patch the time on our side, to get some sort of a reasonable range bothways.I think just
would do the trick, but I am wondering if there's a better way to do this.
Manual for
mach_absolute_time
(https://developer.apple.com/documentation/kernel/1462446-mach_absolute_time) also suggests usingclock_gettime_nsec_np(CLOCK_UPTIME_RAW)
instead, so we can perhaps switch to that while we are at it.The text was updated successfully, but these errors were encountered: