diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 3f975b99..bed1d7e2 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -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 `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) +- 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 the service identity and libConfuse merges two sections sharing one, so the file loaded as a single service holding a mix of both declarations, diff --git a/doc/config/service-opts.md b/doc/config/service-opts.md index e60b484f..70a27c9c 100644 --- a/doc/config/service-opts.md +++ b/doc/config/service-opts.md @@ -176,8 +176,9 @@ following settings are available: * `restart-sec = SEC` -- number of seconds before Finit tries to 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 - the default `restart-max` that is the first five retries. The - greater of this configured value and the back-off is used + the default `restart-max`, and with `restart = "always"`, that is the + 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 tries to restart a crashing service * `restart = "never"` -- do not restart on failures. `false` is diff --git a/src/service.c b/src/service.c index f28f68b2..bb3f1947 100644 --- a/src/service.c +++ b/src/service.c @@ -3267,7 +3267,7 @@ static void service_cleanup_script(svc_t *svc) static void service_retry(svc_t *svc) { char *restart_cnt = (char *)&svc->restart_cnt; - int timeout; + int timeout, half; 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); if ((*restart_cnt) == 1) svc->restart_saved = svc->restart_tmo; - /* Wait 2s for the first 5 respawns, then back off to 5s */ - timeout = ((*restart_cnt) <= (svc->restart_max / 2)) ? 2000 : 5000; + /* Wait 2s for the first half of the attempts, then back off to 5s */ + 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. */ svc->restart_tmo = max(svc->restart_tmo, timeout); if (WIFEXITED(svc->status)) diff --git a/test/Makefile.am b/test/Makefile.am index e28f4c56..846ec291 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -65,6 +65,7 @@ EXTRA_DIST += process-depends.sh EXTRA_DIST += rclocal.sh EXTRA_DIST += ready-serv.sh EXTRA_DIST += remain-task-control.sh +EXTRA_DIST += restart-backoff.sh EXTRA_DIST += restart-self.sh EXTRA_DIST += runlevel.sh EXTRA_DIST += run-restart-forever.sh @@ -139,6 +140,7 @@ TESTS += process-depends.sh TESTS += rclocal.sh TESTS += ready-serv.sh TESTS += remain-task-control.sh +TESTS += restart-backoff.sh TESTS += restart-self.sh TESTS += runlevel.sh TESTS += sysvparts.sh diff --git a/test/restart-backoff.sh b/test/restart-backoff.sh new file mode 100755 index 00000000..0d1c363e --- /dev/null +++ b/test/restart-backoff.sh @@ -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" < "$SYSROOT/bin/crash.sh" <