Honour {httpd,sshd}.gracefulStopTimeout in gerrit.sh

The httpd and sshd's gracefulStopTimeout values were not
taken into consideration in gerrit.sh for the maximum time
to wait for Gerrit to stop.

The misalignment between the {httpd,sshd}.gracefulStopTimemout
and the hardcoded 30 seconds in gerrit.sh was causing Gerrit
to remove prematurely some incoming SSH and HTTPD connections
and risking to killing a write operation.

Read the timeout values from gerrit.config and convert them
in seconds using a more limited implementation of getTimeUnit().

Bug: Issue 14613
Change-Id: Id3cb96f5ace75c2390d9fcfd00a81bf091e1d484
This commit is contained in:
Luca Milanesio 2021-05-28 23:51:29 +01:00
parent 2794dd53c0
commit a3d0b96090
1 changed files with 41 additions and 2 deletions

View File

@ -96,6 +96,36 @@ get_config() {
fi
}
# Limited support for Gerrit's getTimeUnit() limited from seconds to days
# because having gerrit startup/shutdown that wait for weeks or years would
# not make so much sense.
get_time_unit_sec() {
TIME_LC=`echo $1 | tr '[:upper:]' '[:lower:]'`
if [[ "$TIME_LC" =~ ^(0|[1-9][0-9]*)$ ]]
then
echo $TIME_LC
elif [[ "$TIME_LC" =~ ^[1-9][0-9]*\ *(s|sec|second|seconds)$ ]]
then
echo "$TIME_LC" | tr -d -c 0-9
elif [[ "$TIME_LC" =~ ^[1-9][0-9]*\ *(m|min|minute|minutes)$ ]]
then
expr `echo "$TIME_LC" | tr -d -c 0-9` '*' 60
elif [[ "$TIME_LC" =~ ^[1-9][0-9]*\ *(h|hr|hour|hours)$ ]]
then
expr `echo "$TIME_LC" | tr -d -c 0-9` '*' 3600
elif [[ "$TIME_LC" =~ ^[1-9][0-9]*\ *(d|day|days)$ ]]
then
expr `echo "$TIME_LC" | tr -d -c 0-9` '*' 86400
else
>&2 echo "Unsupported time format $1"
exit 1
fi
}
max() {
echo $(( $1 > $2 ? $1 : $2 ))
}
##################################################
# Get the action and options
##################################################
@ -315,6 +345,15 @@ ulimit -v unlimited ; # virtual memory
ulimit -x >/dev/null 2>&1 && ulimit -x unlimited ; # file locks
#####################################################
# Configure the maximum wait time for shutdown
#####################################################
EXTRA_STOP_TIMEOUT=30
HTTPD_STOP_TIMEOUT=$(get_time_unit_sec "$(get_config --get httpd.gracefulStopTimeout || echo 0)")
SSHD_STOP_TIMEOUT=$(get_time_unit_sec "$(get_config --get sshd.gracefulStopTimeout || echo 0)")
STOP_TIMEOUT=`expr $(max $HTTPD_STOP_TIMEOUT $SSHD_STOP_TIMEOUT) '+' $EXTRA_STOP_TIMEOUT`
#####################################################
# This is how the Gerrit server will be started
#####################################################
@ -477,7 +516,7 @@ case "$ACTION" in
if running "$GERRIT_PID" ; then
sleep 3
if running "$GERRIT_PID" ; then
sleep 30
sleep $STOP_TIMEOUT
if running "$GERRIT_PID" ; then
start-stop-daemon -K -p "$GERRIT_PID" -s KILL
fi
@ -487,7 +526,7 @@ case "$ACTION" in
echo OK
else
PID=`cat "$GERRIT_PID" 2>/dev/null`
TIMEOUT=30
TIMEOUT=$STOP_TIMEOUT
while running "$GERRIT_PID" && test $TIMEOUT -gt 0 ; do
kill $PID 2>/dev/null
sleep 1