monotonic timers on OS X?

125 views
Skip to first unread message

st...@keyba.se

unread,
May 26, 2016, 12:42:31 AM5/26/16
to golang-nuts
Hi all,

I encountered an issue where, on OS x with Go 1.6, time.Sleep(10) was called, then the system clock was set backwards by 7 years, and the Sleep never returned (well, it's only been a few days -- I imagine it will return in about 6 years and 358 days).  I found this post from a few years ago that indicates the problem is likely that Go doesn't support monotonic timers on OS X: https://groups.google.com/d/msg/golang-dev/gVFa7DC8UI0/3gfW_2bV1U4J.

My questions are:

1) Is that indeed still the case with Go 1.6?
2) Are there any common patterns people use to get around this?  I guess I can have some logic that tries to detect whether the clock went backward, and if so, somehow interrupt all existing timers.  Or maybe I can write my own sleep method using cgo? Any other ideas?

Thanks,

Jeremy

Matt Harden

unread,
May 26, 2016, 11:19:46 AM5/26/16
to st...@keyba.se, golang-nuts
I think the moral of the story is, never set the clock backwards on a running system. Loads of programs carry the implicit assumption that time only increases. Do you really have a use case where you expect the clock to be set backwards on a running system?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeremy Stribling

unread,
May 26, 2016, 11:40:57 AM5/26/16
to Matt Harden, golang-nuts
I should have clarified -- this isn't a system we control.  This is a program that can be downloaded and run by the general public, and we can't assume anything about their clocks.  In this case, the user was testing certificate expiration by advancing the clock, and when finished setting it back to the real time.

As far as I know, this is exactly why primitives like Sleep should be using a monotonic clock, since then you're guaranteed that time only increases, no matter what the user does.

Tim K

unread,
May 26, 2016, 10:37:57 PM5/26/16
to golang-nuts, st...@keyba.se
Isn't it possible for the time to be adjusted backwards by something like NTP? I know it usually does it in very small increments which may or may not make a difference, but there are also cases where it can make one-time bigger adjustments.

andrey mirtchovski

unread,
May 26, 2016, 10:56:21 PM5/26/16
to Tim K, golang-nuts, st...@keyba.se
among the other options given here:

http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x

is this one:

https://developer.apple.com/library/mac/qa/qa1398/_index.html

we locally use this combo:

host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &mach_clock);
kern_ret = clock_get_time(mach_clock, &mach_curr_time);
mach_port_deallocate(mach_task_self(), mach_clock);
Reply all
Reply to author
Forward
0 new messages