Skip to content

Commit

Permalink
Merge pull request #4115 from esl/handle-errors-when-test-runner-is-p…
Browse files Browse the repository at this point in the history
…arallel

Stop the test-runner process if any of parallel subtasks fail
  • Loading branch information
NelsonVides authored Sep 5, 2023
2 parents c140a5b + bbffef6 commit a09755a
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions tools/parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ PARALLEL_ENABLED=${PARALLEL_ENABLED-true}
# Add prefix to all lines of the output
function exec_with_prefix
{
set -eo pipefail
NAME="$1"
shift
if [ "$NAME" = "false" ]; then
Expand Down Expand Up @@ -38,10 +39,13 @@ function init_parallel
fi
}

PARALLEL_PIDS=""

function parallel
{
if [ "$PARALLEL_ENABLED" = "true" ]; then
exec_with_prefix $@ &
PARALLEL_PIDS="$PARALLEL_PIDS $!"
else
shift # ignore an argument with a prefix
$@
Expand All @@ -51,7 +55,25 @@ function parallel
function wait_for_parallel
{
if [ "$PARALLEL_ENABLED" = "true" ]; then
wait
echo "Done $1"
# https://stackoverflow.com/questions/49513335/bash-wait-exit-on-error-code
set -e
werr=0
err=0
for pid in $PARALLEL_PIDS; do
wait $pid || werr=$?
! [ $werr = 127 ] || break
err=$werr
## To handle *as soon as* first failure happens uncomment this:
[ $err = 0 ] || break
done
## If you want to still wait for children to finish before exiting
## parent (even if you handle the failed child early) uncomment this:
#trap 'wait || :' EXIT
if [ $err = 0 ]; then
echo "Done $1"
else
echo "Failed $1"
exit $err
fi
fi
}

0 comments on commit a09755a

Please sign in to comment.