From ce286c82fcd319e3b270a827296342895891607d Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Mon, 11 Nov 2024 12:48:15 +0530 Subject: [PATCH] Handle race condition in get on a mutli self-hosted runners setup --- src/scripts/unix.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/scripts/unix.sh b/src/scripts/unix.sh index f8905025a..e141961c5 100644 --- a/src/scripts/unix.sh +++ b/src/scripts/unix.sh @@ -84,12 +84,19 @@ get() { if [ "$mode" = "-s" ]; then sudo curl "${curl_opts[@]}" "${links[0]}" else + lock_path="$file_path.lock" + until sudo mkdir "$lock_path" 2>/dev/null; do + echo "Another process is download the file $file_path, waiting" + sleep 1 + done + trap 'sudo rm -rf "$lock_path"' EXIT SIGINT SIGTERM for link in "${links[@]}"; do status_code=$(sudo curl -w "%{http_code}" -o "$file_path" "${curl_opts[@]}" "$link") [ "$status_code" = "200" ] && break done [ "$execute" = "-e" ] && sudo chmod a+x "$file_path" [ "$mode" = "-v" ] && echo "$status_code" + sudo rm -rf "$lock_path" >/dev/null 2>&1 || true fi }