From ec87ef0e381303e84ad2cb620e650e148260db66 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Thu, 14 May 2015 10:36:50 -0700 Subject: [PATCH] Add a loop to git_prune git remote prune is sometimes hitting the same "GnuTLS recv error (-9): A TLS packet with unexpected length was received" error that git remote update was hitting in bug 1383403. This change takes the timeout/retry logic in git_remote_update and puts it into it's own git_timed method (sort of like the one in devstack) so both git_remote_update and git_prune can leverage it. Closes-Bug: #1455123 Change-Id: I026499c58baf5db644786b0cb4c6d04a5c70f7ae --- functions.sh | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/functions.sh b/functions.sh index 8a2dc187..5405805e 100644 --- a/functions.sh +++ b/functions.sh @@ -196,25 +196,33 @@ function git_has_branch { } function git_prune { - git remote prune origin + git_timed remote prune origin } function git_remote_update { - # Attempt a git remote update. Run for up to 5 minutes before killing. - # If first SIGTERM does not kill the process wait a minute then SIGKILL. - # If update fails try again for up to a total of 3 attempts. - MAX_ATTEMPTS=3 - COUNT=0 - until timeout -k 1m 5m git remote update; do - COUNT=$(($COUNT + 1)) - echo "git remote update failed." - if [ $COUNT -eq $MAX_ATTEMPTS ]; then - echo "Max attempts reached for git remote update; giving up." + git_timed remote update +} + +# git can sometimes get itself infinitely stuck with transient network +# errors or other issues with the remote end. This wraps git in a +# timeout/retry loop and is intended to watch over non-local git +# processes that might hang. Run for up to 5 minutes before killing. +# If first SIGTERM does not kill the process wait a minute then SIGKILL. +# If the git operation fails try again for up to a total of 3 attempts. +# usage: git_timed +function git_timed { + local max_attempts=3 + local count=0 + until timeout -k 1m 5m git "$@"; do + count=$(($count + 1)) + echo "git $@ failed." + if [ $count -eq $max_attempts ]; then + echo "Max attempts reached for git $@; giving up." exit 1 fi - SLEEP_TIME=$((30 + $RANDOM % 60)) - echo "sleep $SLEEP_TIME before retrying." - sleep $SLEEP_TIME + local sleep_time=$((30 + $RANDOM % 60)) + echo "sleep $sleep_time before retrying." + sleep $sleep_time done }