Skip to content

Replace gettimeofday with clock_gettime(CLOCK_MONOTONIC) - #40

Open
jEsuSdA wants to merge 1 commit into
tycho-kirchner:masterfrom
jEsuSdA:fix/clock-gettime-monotonic
Open

Replace gettimeofday with clock_gettime(CLOCK_MONOTONIC)#40
jEsuSdA wants to merge 1 commit into
tycho-kirchner:masterfrom
jEsuSdA:fix/clock-gettime-monotonic

Conversation

@jEsuSdA

@jEsuSdA jEsuSdA commented Jul 2, 2026

Copy link
Copy Markdown

Summary

Replaces gettimeofday() with clock_gettime(CLOCK_MONOTONIC) for all internal timers, fixing a correctness bug where system clock changes could cause fade timeouts and other timers to misbehave.

Problem

gettimeofday() returns wall-clock time, which is vulnerable to NTP jumps or manual system clock changes. If the system clock is adjusted while fastcompmgr is running, internal timers (fade timeouts, configure debounce, etc.) can appear to run too fast or too slow, or even hang indefinitely.

Solution

clock_gettime(CLOCK_MONOTONIC) is guaranteed to never go backwards and is not affected by system time changes. This is the correct clock for measuring elapsed time.

Changes

  • cm-util.h — replaces gettimeofday() with clock_gettime(CLOCK_MONOTONIC) in get_time_in_milliseconds()
  • cm-util.c — removes the unused _program_start_secs variable (it was initialized to 0 and never set, so the subtraction was a no-op)
  • cm-util.h — updates #include <sys/time.h> to #include <time.h>

Scope

  • cm-util.h and cm-util.c only
  • make clean && make produces zero warnings, zero errors
  • All existing call sites of get_time_in_milliseconds() are unaffected

Impact

Correctness improvement: timers now behave correctly even when the system clock changes. No performance impact — clock_gettime is at least as fast as gettimeofday on modern Linux kernels.

… timers

gettimeofday() is wall-clock time and vulnerable to NTP jumps or manual
system clock changes. This can cause timers to misbehave (e.g. fade
timeouts appearing to run too fast or too slow after a clock adjustment).

clock_gettime(CLOCK_MONOTONIC) is guaranteed to never go backwards and is
not affected by system time changes, making it the correct clock for
measuring elapsed time.

Also removes the unused _program_start_secs variable which was initialized
to 0 and never set, so the subtraction in get_time_in_milliseconds() was
a no-op anyway.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant