eglimi/cpptime

Cannot work reliably on Linux

Closed this issue · 5 comments

Hi,
I just tested this great timer on both Windows and Ubuntu.
When I set the system time both backward and forward, the timer always invokes the callback function.
But on Ubuntu it only works when I set the system time forward.
I have no idea what's wrong with this code, or the Linux system. Thanks!

Leo

Hello Leo,

Good observation, thank you. However, I cannot reproduce this issue on my machine (Linux 5.13, Pop-OS). The code uses std::chrono::steady_clock, which is a monotonic, never decreasing clock:

Could you please share the exact sequence that you run on your machine that triggers this issue?

Hello,
Sorry for the late reply.
I use cpptime to print text to stdin every 10 seconds. This is the code:

CppTime::Timer t;
auto id = t.add(seconds(1), [](CppTime::timer_id) { std::cout << "timer\n"; }, seconds(10));
std::this_thread::sleep_for(minutes(60));
t.remove(id);

I build the demo with gcc v9.3.0 and run it on Ubuntu 20.04.4 LTS.
When I change the system time backward in setttings, the demo stops printing text.
Thanks!

Hi,

Thank you for the example code. I cannot reproduce the issue on my host system, Pop!_OS 21.10 with gcc 11.2.0. For completeness, the following is my test sequence.

# Current time
➜ timedatectl                                                                         
               Local time: Tue 2022-03-01 15:09:27 CET
           Universal time: Tue 2022-03-01 14:09:27 UTC                                
                 RTC time: Tue 2022-03-01 14:09:27

# Stop NTP
sudo systemctl stop systemd-timesyncd

# Adjust time
timedatectl set-time "2021-10-01 18:00:00"
➜ timedatectl                                                                          
               Local time: Fri 2021-10-01 18:00:13 CEST
           Universal time: Fri 2021-10-01 16:00:13 UTC
                 RTC time: Fri 2021-10-01 16:00:14

However, I could reproduce the issue with the above sequence when running in Docker with Ubuntu 20.04 and gcc 9.3.0 (the default and same as you).
With the same Ubuntu 20.04 system, but gcc 11.1.0, the problem doesn't occur. Therefore, it seems to be an issue with the compiler that I need to investigate (any hints are welcome).

This seems to be the relevant issue:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=41861

I extended the README to explain this problem and will therefore close this issue now. Thank you again for reporting this issue, @leochou0729 !