Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe simple way to improve accuracy of "cron_sleep" function ("seconds_to_wait" variable) #125

Closed
steffenweber opened this issue Nov 16, 2022 · 4 comments

Comments

@steffenweber
Copy link

I've noticed that cronjobs always start 1.00 to 1.99 seconds too late. Not a big issue, of course, but it has caught my attention.

The "cron_sleep" function (https://github.com/cronie-crond/cronie/blob/v4.3/src/cron.c#L372) computes a variable seconds_to_wait. The left part of the expression (everything to the left of the + 1) computes a value that looks as accurate as possible for full-seconds sleeps to me (it's up to 0.00 and 0.99 too big but afaik that cannot be avoided if you use full-second integer sleep calls instead of something like usleep). However, for some reason 1 second is then added to the value which makes it 1.00 to 1.99 too big).

I've tried to find a rationale for this + 1 addition but couldn't. It was introduced as part of "changes #12-17 from tcmiller (for *BSD alignment)" in 2003: vixie/cron@1b363f1

From my limited understanding, the addition of 1 seems unecessary. A patched cronie that removes the addition of 1 seems to be working fine. But maybe I'm missing something?

@t8m
Copy link
Member

t8m commented Nov 16, 2022

I think it is there to avoid calling sleep(0) in some case which perhaps had some unwanted behavior on some old systems.

So we could just make it min(1, seconds_to_wait) instead of adding 1.

@steffenweber
Copy link
Author

So something like this:

diff --git a/src/cron.c b/src/cron.c
--- a/src/cron.c
+++ b/src/cron.c
@@ -628,7 +628,8 @@ static void cron_sleep(int target, cron_db * db) {
 	int seconds_to_wait;
 
 	t1 = time(NULL) + GMToff;
-	seconds_to_wait = (int) (target * SECONDS_PER_MINUTE - t1) + 1;
+	seconds_to_wait = (int) (target * SECONDS_PER_MINUTE - t1);
+	if (seconds_to_wait < 1) seconds_to_wait = 1;
 	Debug(DSCH, ("[%ld] Target time=%ld, sec-to-wait=%d\n",
 			(long) getpid(), (long) target * SECONDS_PER_MINUTE,
 			seconds_to_wait));

@t8m
Copy link
Member

t8m commented Oct 19, 2023

I've done something similar on master branch so this should be now fixed.

@t8m t8m closed this as completed Oct 19, 2023
@t8m
Copy link
Member

t8m commented Oct 19, 2023

78e6349

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

No branches or pull requests

2 participants