From 1bc72701a54840e573cc8a703755864803c1af47 Mon Sep 17 00:00:00 2001
From: "Fabian.Jakub" <fabian@jakub.com>
Date: Wed, 24 May 2023 12:15:36 +0200
Subject: [PATCH] use mktemp file instead of a lock file around spack/upstreams

---
 platforms/common/spack_utils.sh | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/platforms/common/spack_utils.sh b/platforms/common/spack_utils.sh
index e441424..0976792 100644
--- a/platforms/common/spack_utils.sh
+++ b/platforms/common/spack_utils.sh
@@ -6,26 +6,19 @@ function _install_spack() {
 
   _init_spack "$SPACK_INIT_CMD" "$SPACK_ROOT"
 
-  # Adding a lock file to prevent race conditions.
-  UPSTREAMS_LOCK=${SPACK_ROOT}/etc/spack/upstreams.lock
-  while [ -f "${UPSTREAMS_LOCK}" ]; do
-    sleep 1
-  done
-
   if [ ! -z "${SPACK_UPSTREAMS}" ]; then
-    touch ${UPSTREAMS_LOCK}
     UPSTREAMS_CONFIG=${SPACK_ROOT}/etc/spack/upstreams.yaml
-    echo "upstreams:" > $UPSTREAMS_CONFIG
+    tmpfile=$(mktemp)
+
+    echo "upstreams:" > $tmpfile
     i=0
     for UP in ${SPACK_UPSTREAMS}; do
       echo "Adding upstream repo to config $UPSTREAMS_CONFIG <= $UP"
       i=$((i+1))
-      cat << EOF >> $UPSTREAMS_CONFIG
-  spack-instance-$i:
-      install_tree: $UP
-EOF
+      echo "  spack-instance-$i:" >> $tmpfile
+      echo "    install_tree: $UP" >> $tmpfile
     done
-    rm ${UPSTREAMS_LOCK}
+    mv $tmpfile $UPSTREAMS_CONFIG
   fi
 
   if [ ! -z "${SPACK_EXTERNALS}" ] ; then
-- 
GitLab