From d301c06a7e097f41878df5299e43057d49db9ae5 Mon Sep 17 00:00:00 2001 From: Nikola Jokic <97525037+nikola-jokic@users.noreply.github.com> Date: Wed, 2 Nov 2022 14:28:14 +0100 Subject: [PATCH] run.sh installs SIGINT and SIGTERM traps to gracefully stop runner (#2233) * run.sh installs SIGINT and SIGTERM traps to gracefully stop runner * replaced trap to force wait for child processes and to send kill to run-helper instead of Runner.Listener * run with signal handling if RUNNER_MANUALLY_TRAP_SIG is set * Update src/Misc/layoutroot/run.sh Co-authored-by: Tingluo Huang Co-authored-by: Tingluo Huang --- src/Misc/layoutroot/run.sh | 62 ++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/src/Misc/layoutroot/run.sh b/src/Misc/layoutroot/run.sh index ca9e5247d..e6610a7f8 100755 --- a/src/Misc/layoutroot/run.sh +++ b/src/Misc/layoutroot/run.sh @@ -9,16 +9,52 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located done DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" -# run the helper process which keep the listener alive -while :; -do - cp -f "$DIR"/run-helper.sh.template "$DIR"/run-helper.sh - "$DIR"/run-helper.sh $* - returnCode=$? - if [[ $returnCode -eq 2 ]]; then - echo "Restarting runner..." - else - echo "Exiting runner..." - exit 0 - fi -done + +run() { + # run the helper process which keep the listener alive + while :; + do + cp -f "$DIR"/run-helper.sh.template "$DIR"/run-helper.sh + "$DIR"/run-helper.sh $* + returnCode=$? + if [[ $returnCode -eq 2 ]]; then + echo "Restarting runner..." + else + echo "Exiting runner..." + exit 0 + fi + done +} + +runWithManualTrap() { + # Set job control + set -m + + trap 'kill -INT -$PID' INT TERM + + # run the helper process which keep the listener alive + while :; + do + cp -f "$DIR"/run-helper.sh.template "$DIR"/run-helper.sh + "$DIR"/run-helper.sh $* & + PID=$! + wait -f $PID + returnCode=$? + if [[ $returnCode -eq 2 ]]; then + echo "Restarting runner..." + else + echo "Exiting runner..." + # Unregister signal handling before exit + trap - INT TERM + # wait for last parts to be logged + wait $PID + exit 0 + fi + done +} + +if [[ -z "$RUNNER_MANUALLY_TRAP_SIG" ]]; then + run +else + runWithManualTrap +fi \ No newline at end of file