====== Shell executor ======
Known issue with job cancellation.
Shell executor is using ''bash --login user'' and for every ''script:'' section another shell (bash) is spawned.
In result proces tree looks like:
{{:git:gitlab:runner:pasted:20220105-175808.png}}
Default bash behavior is to ignore signals when waiting for child.
If bash is waiting for a command to complete and receives a signal for which a trap has been set,
the trap will not be executed until the command completes. When bash is waiting for an asynchronous
command via the wait builtin, the reception of a signal for which a trap has been set will cause
the wait builtin to return immediately with an exit status greater than 128, immediately after which
the trap is executed.
The same issue described here:
* [[https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3101|Canceling jobs in shell executor doesnt work]]
Some references:
* [[https://unix.stackexchange.com/questions/146756/forward-sigterm-to-child-in-bash|Forward SIGTERM to child in Bash]]
* [[https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3031|Gracefully halt cancelled jobs]]
Solution:
* Using bash wrapper script plus replace current shell with new process using ''exec''
build:
stage: build
script:
- exec tools/ci/launch_job.sh make all
- exec tools/ci/launch_job.sh make deploy
#!/bin/bash
#_term() {
# echo "$0 caught SIGTERM signal!"
# trap - SIGTERM
# kill -TERM $child $$
#}
launch_job() {
PARENT=$1
shift
"$@" &
CHILD=$!
trap "trap - SIGTERM; kill -TERM $CHILD $$" SIGTERM
while sleep 1; do
if [ ! -e /proc/$PARENT ]; then
echo "Parent PID $PARENT disappears, terminating child $CHILD"
kill -TERM $CHILD
exit
fi
if [ ! -e /proc/$CHILD ]; then
wait $CHILD
trap - SIGTERM
exit $?
fi
done
}
#trap _term SIGTERM
#launch_job $$ "$@" &
#echo "$0 Waiting for $! to finish"
#wait $!
launch_job $PPID "$@"