Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions btest-bg-run-helper
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
#
# Internal helper for btest-bg-run.

is_windows=0
case "$(uname -s)" in
MINGW* | MSYS* | CYGWIN*) is_windows=1 ;;
esac

# On Windows, kill the child's process tree using its Windows PID.
# taskkill /T terminates the process and all its descendants.
# MSYS_NO_PATHCONV prevents MSYS from interpreting /F, /T, /PID as paths.
win_kill_tree() {
local wpid
if [ $is_windows -eq 1 ] && [ -f .winpid ]; then
wpid=$(cat .winpid)
if [ -n "$wpid" ]; then
MSYS_NO_PATHCONV=1 taskkill /F /T /PID "$wpid" &>/dev/null
fi
fi
}

cleanup() {
# Ignore SIGTERM during cleanup to prevent terminating
# this process when sending signals to the process group.
Expand All @@ -20,16 +38,19 @@ cleanup() {
# On Windows (without setsid) the helper shares a process
# group with the parent, so kill 0 would take out the
# entire test harness. Only kill the child directly.
case "$(uname -s)" in
MINGW* | MSYS* | CYGWIN*) ;;
*) kill 0 &>/dev/null ;;
esac
if [ $is_windows -eq 0 ]; then
kill 0 &>/dev/null
fi

if [ -n "$pid" ]; then
kill -0 "$pid" &>/dev/null && kill "$pid"
sleep 1
kill -0 "$pid" &>/dev/null && kill -9 "$pid" && echo 9 >.exitcode
fi

# Fallback: on Windows, use taskkill to ensure the entire
# process tree is terminated even if kill missed descendants.
win_kill_tree
fi
}

Expand All @@ -40,6 +61,12 @@ eval "$* &"
pid=$!
echo $$ >.pid

# Record the child's Windows PID so that btest-bg-wait can
# reliably terminate it even if MSYS PIDs become stale.
if [ $is_windows -eq 1 ] && [ -f /proc/$pid/winpid ]; then
cat /proc/$pid/winpid >.winpid
fi

wait $pid
echo $? >.exitcode
pid=""
20 changes: 20 additions & 0 deletions btest-bg-wait
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ function check_procs {
return 0
}

is_windows=0
case "$(uname -s)" in
MINGW* | MSYS* | CYGWIN*) is_windows=1 ;;
esac

function kill_procs {
for p in $procs; do
if [ ! -e "$p/.exitcode" ]; then
Expand All @@ -62,6 +67,21 @@ function kill_procs {
sleep 1
fi
done

# On Windows, use taskkill as a fallback to ensure child
# processes and their descendants are terminated even if
# MSYS signals did not reach them.
if [ $is_windows -eq 1 ]; then
for p in $procs; do
if [ -f "$p/.winpid" ]; then
local wpid
wpid=$(cat "$p/.winpid")
if [ -n "$wpid" ]; then
MSYS_NO_PATHCONV=1 taskkill /F /T /PID "$wpid" &>/dev/null
fi
fi
done
fi
}

function collect_output {
Expand Down
Loading