diff --git a/scripts/common b/scripts/common index 8955fcc..9049039 100755 --- a/scripts/common +++ b/scripts/common @@ -16,6 +16,21 @@ HOST=tftp.keg.cse.unsw.edu.au # Config file location CONFIG=~/.mq +# Bash implements the "Wait and Cooperative Exit" protocol +# If, when we receive a trap like SIGINT, we do not then exit because of a SIGINT, +# bash assumes that we have handled SIGINT and continues as if we did not try +# to just stop the program execution, making it look like nothing happened. +# Hence, we must emulate this behaviour for bash to believe that we have died +# due to the SIGINT (i.e. that after the wait() syscall, WTERMSIG(status) is SIGINT). +# Ref: https://www.cons.org/cracauer/sigint.html +# Ref: https://mywiki.wooledge.org/SignalTrap#Special_Note_On_SIGINT_and_SIGQUIT +TrapExitSigInt() { + # Restore default handling for SIGINT + trap - SIGINT + kill -s INT $$ +} + + # Add all the other bits and pieces . "${SCRIPT_PATH}/scripts/remote" diff --git a/scripts/enqueue b/scripts/enqueue index 92d56d5..f162d15 100755 --- a/scripts/enqueue +++ b/scripts/enqueue @@ -230,7 +230,7 @@ Enqueue() { # We can setup the trap early as we check if we actually own the # the lock before releasing it - trap "UnlockSystem \"${system}\" 0 \"${key}\"; exit 1" ${SIGNALS} + trap "UnlockSystem \"${system}\" 0 \"${key}\"; TrapExitSigInt" ${SIGNALS} if ! LockSystem "${system}" "${retry_period}" "${total_retries}" "${key}"; then echo "Failed to acquire lock for system (${system})" @@ -257,11 +257,21 @@ Enqueue() { # Ditto for 'bitstreamflag' SystemRunImage "${system}" "${completion}" "${completion_timeout}" "${errortxt}" "${logfile}" "${keep_alive}" ${linux} ${dtbflag} ${bitstreamflag} $files ret=$? + + # Special case: SSH does not return indicating a SIGINT, so we special + # case instead that it returns 255 in this case. + # https://github.com/openssh/openssh-portable/blob/V_10_0_P1/mux.c#L2057-L2059 + # (However, sometimes it will instead forward through status 130 from the process + # which received SIGINT on the other end of the SSH session. Sometimes...) + if [ "${ret}" == "255" ]; then + TrapExitSigInt + fi fi if ! ${no_lock_mods} ; then UnlockSystem "${system}" 0 "${key}" - trap "exit 1" ${SIGNALS} + # Reset trap behaviour to default. + trap - ${SIGNALS} fi exit $ret