From 3a77d81c5b30dccc3c1a97d55e00d7e853c5ddf4 Mon Sep 17 00:00:00 2001 From: Julia Vassiliki Date: Wed, 22 Apr 2026 17:29:17 +1000 Subject: [PATCH] Add improved sigint handling for cancellation Actually propagate upwards the SIGINT as the cause of the failure. Signed-off-by: Julia Vassiliki --- scripts/common | 15 +++++++++++++++ scripts/enqueue | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) 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