-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Subtracting Duration from Instant on Mac is panic-prone #100141
Copy link
Copy link
Closed
Labels
A-timeArea: TimeArea: TimeC-bugCategory: This is a bug.Category: This is a bug.O-appleOperating system: Apple / Darwin (macOS, iOS, tvOS, visionOS, watchOS)Operating system: Apple / Darwin (macOS, iOS, tvOS, visionOS, watchOS)T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-timeArea: TimeArea: TimeC-bugCategory: This is a bug.Category: This is a bug.O-appleOperating system: Apple / Darwin (macOS, iOS, tvOS, visionOS, watchOS)Operating system: Apple / Darwin (macOS, iOS, tvOS, visionOS, watchOS)T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
The following program:
works on Linux, but could panic on Mac. The reason for that is that, as implemented,
Instanton 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 Instantisn'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
Instantrange 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.