async-io icon indicating copy to clipboard operation
async-io copied to clipboard

`Timer` is paused on Android in "doze" state

Open link2xt opened this issue 3 years ago • 6 comments

Because std::time::Instant is implemented using libc::clock_gettime(CLOCK_MONOTONIC) and this time does not advance on Android while the app is in "doze" state, the timer may be effectively paused on Android: https://users.rust-lang.org/t/std-now-with-android/41774

One way to fix this is to use SystemTime instead and handle time occasionally going backwards properly. Another is to lift this issue to the standard library so it uses Android-specific ioctl on /dev/alarm as described here: https://users.rust-lang.org/t/std-now-with-android/41774/2

link2xt avatar May 09 '21 22:05 link2xt

Another is to lift this issue to the standard library so it uses Android-specific ioctl on /dev/alarm as described here:

See https://github.com/rust-lang/rust/issues/71860

taiki-e avatar May 10 '21 11:05 taiki-e

Commenting to note that this behavior is actually desirable in my use case, as it prevents massive thundering-herds of timers suddenly expiring when Doze ends, something I've seen in Go.

My use case does involve massive amounts of small, fine-grained timers (essentially a userspace TCP implementation), so it's somewhat of a worst-case for this though.

nullchinchilla avatar Nov 26 '21 21:11 nullchinchilla

With Rust 1.64 coming out in a month or so, this issue should be fixed on libstd relatively soon. I'd rather wait for it to be fixed on that end than implement a patch in here that will soon become unnecessary.

notgull avatar Aug 19 '22 17:08 notgull

As of Rust 1.64, the Instant type now uses CLOCK_BOOTTIME, which should close this issue.

notgull avatar Sep 23 '22 20:09 notgull

Actually, I just remembered that polling uses CLOCK_MONOTONIC, so I should reopen this issue until that is fixed.

notgull avatar Sep 23 '22 20:09 notgull

As of Rust 1.64, the Instant type now uses CLOCK_BOOTTIME, which should close this issue.

This URL points to line with CLOCK_MONOTONIC, and if I switch to master, it's still CLOCK_MONOTONIC. Where does the Instant implementation use CLOCK_BOOTTIME constant?

There is a PR proposing a switch to CLOCK_BOOTTIME, but it's not merged: https://github.com/rust-lang/rust/pull/88714

link2xt avatar Sep 24 '22 10:09 link2xt