service: start restart always at two seconds between restarts

A crashing service with restart always waited five seconds before
every restart.  It should wait two seconds for the first five, like a
service with the default restart-max:

    service foo {
        restart = "always"
        command = "foo"
    }

The back-off starts after half of restart-max attempts.  For restart
always restart-max is -1, so half of it was zero and the back-off
began with the first restart.  Use the default restart-max instead.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-19 20:39:40 +01:00
1 parent 631baab756
commit 4e1b0d4fbe
5 files changed
+67 -5

No files matched your search

+2
View File
@@ -188,6 +188,8 @@ All relevant changes are documented in this file.
- Fix a command whose name starts like a service option, e.g. `logger` or - Fix a command whose name starts like a service option, e.g. `logger` or
`pidof`. It was taken as the `log` or `pid` option and the next argument `pidof`. It was taken as the `log` or `pid` option and the next argument
ran as the command, which also broke the `logger` example in finit.conf(5) ran as the command, which also broke the `logger` example in finit.conf(5)
- Fix `restart = "always"` skipping the initial two second delay between
restarts, every restart waited five seconds
- Reject a .conf file that declares the same block title twice. The title is - Reject a .conf file that declares the same block title twice. The title is
the service identity and libConfuse merges two sections sharing one, so the the service identity and libConfuse merges two sections sharing one, so the
file loaded as a single service holding a mix of both declarations, file loaded as a single service holding a mix of both declarations,
+3 -2
View File
@@ -176,8 +176,9 @@ following settings are available:
* `restart-sec = SEC` -- number of seconds before Finit tries to * `restart-sec = SEC` -- number of seconds before Finit tries to
restart a crashing service. The default is 2 seconds for the first restart a crashing service. The default is 2 seconds for the first
half of `restart-max` attempts, then a back-off to 5 seconds -- with half of `restart-max` attempts, then a back-off to 5 seconds -- with
the default `restart-max` that is the first five retries. The the default `restart-max`, and with `restart = "always"`, that is the
greater of this configured value and the back-off is used first five retries. The greater of this configured value and the
back-off is used
* `restart = "always"` -- no upper limit on the number of times Finit * `restart = "always"` -- no upper limit on the number of times Finit
tries to restart a crashing service tries to restart a crashing service
* `restart = "never"` -- do not restart on failures. `false` is * `restart = "never"` -- do not restart on failures. `false` is
+4 -3
View File
@@ -3267,7 +3267,7 @@ static void service_cleanup_script(svc_t *svc)
static void service_retry(svc_t *svc) static void service_retry(svc_t *svc)
{ {
char *restart_cnt = (char *)&svc->restart_cnt; char *restart_cnt = (char *)&svc->restart_cnt;
int timeout; int timeout, half;
service_timeout_cancel(svc); service_timeout_cancel(svc);
@@ -3326,8 +3326,9 @@ static void service_retry(svc_t *svc)
dbg("%s crashed, trying to start it again, attempt %d", svc_ident(svc, NULL, 0), *restart_cnt); dbg("%s crashed, trying to start it again, attempt %d", svc_ident(svc, NULL, 0), *restart_cnt);
if ((*restart_cnt) == 1) if ((*restart_cnt) == 1)
svc->restart_saved = svc->restart_tmo; svc->restart_saved = svc->restart_tmo;
/* Wait 2s for the first 5 respawns, then back off to 5s */ /* Wait 2s for the first half of the attempts, then back off to 5s */
timeout = ((*restart_cnt) <= (svc->restart_max / 2)) ? 2000 : 5000; half = (svc->restart_max == -1 ? SVC_RESPAWN_MAX : svc->restart_max) / 2;
timeout = *restart_cnt <= half ? 2000 : 5000;
/* If a longer timeout was specified in the conf, use that instead. */ /* If a longer timeout was specified in the conf, use that instead. */
svc->restart_tmo = max(svc->restart_tmo, timeout); svc->restart_tmo = max(svc->restart_tmo, timeout);
if (WIFEXITED(svc->status)) if (WIFEXITED(svc->status))
+2
View File
@@ -65,6 +65,7 @@ EXTRA_DIST += process-depends.sh
EXTRA_DIST += rclocal.sh EXTRA_DIST += rclocal.sh
EXTRA_DIST += ready-serv.sh EXTRA_DIST += ready-serv.sh
EXTRA_DIST += remain-task-control.sh EXTRA_DIST += remain-task-control.sh
EXTRA_DIST += restart-backoff.sh
EXTRA_DIST += restart-self.sh EXTRA_DIST += restart-self.sh
EXTRA_DIST += runlevel.sh EXTRA_DIST += runlevel.sh
EXTRA_DIST += run-restart-forever.sh EXTRA_DIST += run-restart-forever.sh
@@ -139,6 +140,7 @@ TESTS += process-depends.sh
TESTS += rclocal.sh TESTS += rclocal.sh
TESTS += ready-serv.sh TESTS += ready-serv.sh
TESTS += remain-task-control.sh TESTS += remain-task-control.sh
TESTS += restart-backoff.sh
TESTS += restart-self.sh TESTS += restart-self.sh
TESTS += runlevel.sh TESTS += runlevel.sh
TESTS += sysvparts.sh TESTS += sysvparts.sh
+56
View File
@@ -0,0 +1,56 @@
#!/bin/sh
# Verify the delay between restarts of a crashing service. It logs
# the uptime, in centiseconds, of each start and then exits.
set -eu
TEST_DIR=$(dirname "$0")
STARTS=/tmp/starts
# Wait for start $2, then check the delay since start $1, in sec
assert_delay()
{
retry "texec sh -c 'test \$(wc -l < $STARTS) -ge $2'" 150 0.1
# shellcheck disable=SC2016 # $1 is awk's field reference, not the shell's
delay=$(texec awk -v a="$1" -v b="$2" 'NR == a { t = $1 } NR == b { print $1 - t }' $STARTS)
assert "Delay between start $1 and $2 is $3-$4 sec" "$delay" -ge "$(($3 * 100))"
assert "Delay between start $1 and $2 is $3-$4 sec" "$delay" -le "$(($4 * 100))"
}
# Replace the service, settings in $1, and let it start crashing
crash()
{
run "rm -f $FINIT_CONF"
run "initctl reload"
run "rm -f $STARTS"
run "cat > $FINIT_CONF" <<CONF
service crash {
$1
command = "crash.sh"
}
CONF
run "initctl reload"
}
test_teardown()
{
say "Running test teardown."
run "rm -f $FINIT_CONF $STARTS /bin/crash.sh"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
cat > "$SYSROOT/bin/crash.sh" <<SCRIPT
#!/bin/sh
tr -d . < /proc/uptime | cut -d' ' -f1 >> $STARTS
exit 1
SCRIPT
chmod +x "$SYSROOT/bin/crash.sh"
sep
say 'restart always, at once the first time, then every other second'
crash 'restart = "always"'
assert_delay 1 2 0 1
assert_delay 2 3 2 3
assert_delay 3 4 2 3