Rust: Instant::now can go backward

Created on 7 Dec 2018  路  6Comments  路  Source: rust-lang/rust

As in https://github.com/rust-lang/rust/issues/51648#issuecomment-441063768.

I've seen "specified instant was later than self" panics in the wild, in particular on Windows (32-bit x86). Googling reveals stuff like https://stackoverflow.com/questions/44020619/queryperformancecounter-on-multi-core-processor-under-windows-10-behaves-erratic which suggests that QueryPerformanceCounter can't really be relied on... frustratingly.

Perhaps libstd should work around this and enforce that Instant::now() never go backward (with an atomic global, or something?)

O-windows

All 6 comments

Ok, I'm not sure that SO link was the best reference, but e.g. https://chromium.googlesource.com/chromium/src/base/+/master/time/time_win.cc#14 also claims that QPC is buggy.

The linked bug appears to be on a virtual machine, Some hypervisors may emulate TSC instructions with coarse granularity and possibly not core-synchronized. Operating systems try to detect the accuracy of the TSCs based on CPU flags (invariant, nonstop tsc). If the hypervisor is not setting those correctly that's basically out-of-spec behavior.

We've seen things like this in Firefox and have deployed large hammers to fix. Further investigation suggests that the problems may be limited to certain AMD CPUs.

I've opened https://github.com/rust-lang/rust/pull/56988 to apply such a hammer to libstd as well

This hammer is a code smell. This means that I can continue to get the wrong time even after a server's internal clock has been corrected by a time synchronizing service such as NTP. We should not be panicking when the diff between two instants is negative. That possibility should merely be stated in the API documentation for functions where it is likely for an inexperienced software engineer to make an assumption of monotonic increase on a system with more than one processor.

Time is an illusion. Lunchtime doubly so.

Don鈥檛 panic

https://gist.github.com/timvisee/fcda9bbdff88d45cc9061606b4b923ca

You already posted exactly the same comment in #56988, please do not spam.

Also note that Instant has no notion of "wrong time" in the sense of NTP since all it does is measure relative intervals, not absolute time.

Was this page helpful?
0 / 5 - 0 ratings