Skip to content

Read the clock once when scheduling a check for a given time - #549

Open
nook24 wants to merge 1 commit into
naemon:masterfrom
nook24:fix-schedule-clock
Open

nook24 wants to merge 1 commit into
naemon:masterfrom
nook24:fix-schedule-clock

Conversation

@nook24

@nook24 nook24 commented Sep 29, 2026

Copy link
Copy Markdown
Member

t-tap/test_commands fails at random, about once in 40 runs, in these four assertions:

not ok 436 - SCHEDULE_HOST_SVC_CHECKS schedules host service checks
not ok 473 - SCHEDULE_FORCED_HOST_SVC_CHECKS schedules forced checks for services on a host
not ok 495 - SCHEDULE_HOST_CHECK schedules a host check
not ok 497 - SCHEDULE_FORCED_HOST_CHECK schedules a host check

schedule_service_check() and schedule_host_check() turn check_time into a delay with time(NULL), and schedule_next_*_check() turns it back into next_check with gettimeofday(). On Linux time() reads the coarse clock, which lags behind until the next timer tick. For a few milliseconds after every second boundary it still shows the previous second (0.29% of the time in a tight loop here). A call in that window schedules the check one second later than asked for.

Both functions now read the clock once and pass it to a static helper. The public signatures in checks_service.h and checks_host.h are unchanged. Outside the tests the effect is small: a check scheduled for a fixed time through SCHEDULE_SVC_CHECK and friends landed a second late about 0.3% of the time.

schedule_service_check() and schedule_host_check() turned check_time
into a delay with time(NULL), and schedule_next_*_check() turned it back
with gettimeofday(). On Linux time() reads the coarse clock, which can
still show the previous second for a few milliseconds after the other
has moved on, so now and then the check landed a second later than
asked for. That made test_commands fail at random, about once in 40
runs; with time() forced a second behind it fails every time.

Both now read the clock once and pass it down.

Signed-off-by: nook24 <info@nook24.eu>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WY8mbGLNkt5eQfc57cTnZ5
@nook24
nook24 marked this pull request as ready for review September 29, 2026 14:23
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