Skip to content

Commit 8102847

Browse files
committed
Add a timeout to installing php dependencies on macos
The recent concurrent downloader leads to infinite runtime on certain setup-php runs
1 parent 2102e09 commit 8102847

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/scripts/darwin.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ add_php() {
181181
fi
182182
if [[ "$existing_version" != "false" && -z "$suffix" ]]; then
183183
if [ "$action" = "upgrade" ]; then
184+
brew_retry 3 install --only-dependencies "$php_formula" 2>/dev/null || true
184185
brew upgrade -f --overwrite "$php_formula"
185186
else
186187
brew unlink "$php_keg"
187188
fi
188189
else
190+
brew_retry 3 install --only-dependencies "$php_formula" 2>/dev/null || true
189191
brew install -f --overwrite "$php_formula" || brew upgrade -f --overwrite "$php_formula"
190192
fi
191193
sudo chown -R "$(id -un)":"$(id -gn)" "$brew_prefix"

src/scripts/tools/brew.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,39 @@ add_brew() {
5555
add_brew_bins_to_path "$brew_prefix"
5656
}
5757

58+
# Function to run brew with retries and timeout.
59+
brew_retry() {
60+
local timeout=300
61+
local max_tries=${1:-5}
62+
shift
63+
local tries=0
64+
while :; do
65+
tries=$((tries + 1))
66+
sudo rm -rf "$brew_prefix"/var/homebrew/locks/*
67+
brew "$@" &
68+
local cmd_pid=$!
69+
(
70+
sleep "$timeout"
71+
if kill -0 "$cmd_pid" 2>/dev/null; then
72+
echo "Timed out after ${timeout}s, killing brew (pid $cmd_pid)"
73+
kill -TERM "$cmd_pid" 2>/dev/null
74+
fi
75+
) &
76+
local killer_pid=$!
77+
wait "$cmd_pid"
78+
local status=$?
79+
kill -TERM "$killer_pid" 2>/dev/null
80+
if [[ $status -eq 0 ]]; then
81+
return 0
82+
fi
83+
if (( tries >= max_tries )); then
84+
echo "brew command failed after $tries tries (last status $status)"
85+
return "$status"
86+
fi
87+
echo "brew exited with status $status, retrying..."
88+
done
89+
}
90+
5891
# Function to configure brew constants.
5992
configure_brew() {
6093
brew_path="$(command -v brew)"

0 commit comments

Comments
 (0)