Skip to content

Commit

Permalink
Use timeout utility instead of double-sub-shell hell
Browse files Browse the repository at this point in the history
It turned out that for incremental builds for Node.js, the test always waited
10m no matter that the build finished sooner.

It's not clear why it happened, but when debugging, it seemed like some weired
race-condition happened that let some sleeps hang.

Anyway, it's not clear whether there is a reason to not use timeout utility
that seems to serve exactly for this reason.
  • Loading branch information
hhorak authored and phracek committed Sep 25, 2024
1 parent cb3fdd3 commit 008d6ea
Showing 1 changed file with 2 additions and 18 deletions.
20 changes: 2 additions & 18 deletions test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,14 @@ ct_build_image_and_parse_id() {
local ret_val
local dockerfile
local command
local pid_build
local pid_sleep
local sleep_time
log_file="$(mktemp)"
sleep_time="10m"
[ -n "$1" ] && dockerfile="-f $1"
command="$(echo "docker build --no-cache $dockerfile $2" | tr -d "'")"
# running command in subshell, the subshell in background, storing pid to variable
(
$command > "$log_file" 2>&1
) & pid_build=$!
# creating second subshell with trap function on ALRM signal
# the subshell sleeps for 10m, then kills the first subshell
(
trap 'exit 0' ALRM; sleep "$sleep_time" && kill $pid_build
) & pid_sleep=$!
# waiting for build subshell to finish, either with success, or killed from sleep subshell
wait $pid_build
# shellcheck disable=SC2086
timeout $sleep_time $command > "$log_file" 2>&1
ret_val=$?
# send ALRM signal to the sleep subshell, so it exits even in case the 10mins
# not yet passed. If the kill was successful (the wait subshell received ALRM signal)
# then the build was not finished yet, so the return value is set to 1
kill -s ALRM $pid_sleep 2>/dev/null || ret_val=1

if [ $ret_val -eq 0 ]; then
APP_IMAGE_ID="$(tail -n 1 "$log_file")"
fi
Expand Down

0 comments on commit 008d6ea

Please sign in to comment.