cpptime icon indicating copy to clipboard operation
cpptime copied to clipboard

Cannot work reliably on Linux

Open leochou0729 opened this issue 3 years ago • 4 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

leochou0729 avatar Oct 11 '21 04:10 leochou0729

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:

  • https://en.cppreference.com/w/cpp/chrono/steady_clock

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

eglimi avatar Oct 17 '21 11:10 eglimi

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!

leochou0729 avatar Feb 28 '22 08:02 leochou0729

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).

eglimi avatar Mar 01 '22 14:03 eglimi

This seems to be the relevant issue:

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

eglimi avatar Mar 01 '22 17:03 eglimi

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

eglimi avatar Mar 06 '23 17:03 eglimi