# Sleep functions are not accurate on macOS

**URL:** <https://travis-ci.community/t/sleep-functions-are-not-accurate-on-macos/6122>\
**Category:** macOS\
**Created:** [November 25, 2019, 7:11am UTC](https://travis-ci.community/t/sleep-functions-are-not-accurate-on-macos/6122 "2019-11-25T07:11:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ygj6](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/ygj6/32/3499_2.png) [@ygj6](https://travis-ci.community/u/ygj6)\
**Post date:** [November 25, 2019, 7:11am UTC](https://travis-ci.community/t/sleep-functions-are-not-accurate-on-macos/6122/1 "2019-11-25T07:11:17Z")

</div>

The sleep functions `nanosleep()`, `usleep()` and `select()` are not accurate on macOS. The actual sleep times are much longer than expected. For example, `nanosleep(100000)` is expected to sleep for 100ms, but the actual sleep time is 175ms, The percentage of deviation is 75%, which I think the reasonable value is 20%. `usleep()` and `select()` have the same issue.  
This issue is only happened on macOS [1]. It’s all ok on linux [2].  
My test code is here [3].

In addition, all sleep functions work fine on my own Macbook.

[1] [https://travis-ci.org/ygj6/verify/jobs/616467362](https://travis-ci.org/ygj6/verify/jobs/616467362)  
[2] [https://travis-ci.org/ygj6/verify/jobs/616467361](https://travis-ci.org/ygj6/verify/jobs/616467361)  
[3] [https://github.com/ygj6/verify/blob/master/main.c](https://github.com/ygj6/verify/blob/master/main.c)

---

<div class="post-metadata">

**Author:** ![ygj6](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/ygj6/32/3499_2.png) [@ygj6](https://travis-ci.community/u/ygj6)\
**Post date:** [November 25, 2019, 7:19am UTC](https://travis-ci.community/t/sleep-functions-are-not-accurate-on-macos/6122/2 "2019-11-25T07:19:44Z")

</div>

Is macOS in travis-ci a physical or virtual machine?  
Is there any error in the clock of virtual machine?

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [November 25, 2019, 9:35am UTC](https://travis-ci.community/t/sleep-functions-are-not-accurate-on-macos/6122/3 "2019-11-25T09:35:01Z")

</div>

Possible reason:

> <https://stackoverflow.com/questions/48285535/macos-nanosleep-sleeping-for-far-too-long>

---

<div class="post-metadata">

**Author:** ![ygj6](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/ygj6/32/3499_2.png) [@ygj6](https://travis-ci.community/u/ygj6)\
**Post date:** [November 25, 2019, 1:22pm UTC](https://travis-ci.community/t/sleep-functions-are-not-accurate-on-macos/6122/4 "2019-11-25T13:22:21Z")

</div>

I always get success regardless of whether `app nap` is enabled or not on my Macbook. And I don’t known how to turn on/off `app nap` on travis-ci, So it is unable to verify the reason. Can you give me some advice? Thanks.

---

<div class="post-metadata">

**Author:** ![ygj6](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/ygj6/32/3499_2.png) [@ygj6](https://travis-ci.community/u/ygj6)\
**Post date:** [November 28, 2019, 1:42am UTC](https://travis-ci.community/t/sleep-functions-are-not-accurate-on-macos/6122/5 "2019-11-28T01:42:58Z")

</div>

@BanzaiMan Can you take a time to look in this issue? Many thanks!

---

<div class="post-metadata">

**Author:** ![azat](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@azat](https://travis-ci.community/u/azat)\
**Post date:** [January 8, 2020, 5:23pm UTC](https://travis-ci.community/t/sleep-functions-are-not-accurate-on-macos/6122/6 "2020-01-08T17:23:34Z")

</div>

> [@ygj6](#):
>
> And I don’t known how to turn on/off `app nap` on travis-ci

Looks like it is [disabled already](https://github.com/travis-ci/osx-image-bootstrap/blob/ccf3f7bb0fd34592b32e58b4d3273c71ba6ae3e1/bootstrap.sh#L32)

_If this are used by travis-ci of course_
