Patchwork [9/9] icecc: Fix race condition when packaging toolchain

login
register
mail settings
Submitter Tobias Henkel
Date Nov. 12, 2013, 8:34 a.m.
Message ID <1384245243-20247-10-git-send-email-tobias.henkel@oss.bmw-carit.de>
Download mbox | patch
Permalink /patch/61487/
State Accepted
Commit 78c6e69bbe1989561353801db4fcf265b94a21fa
Headers show

Comments

Tobias Henkel - Nov. 12, 2013, 8:34 a.m.
From: Tobias Henkel <tobias.henkel@bmw-carit.de>

In the current implementation there can be a race condition while
creating the toolchain archive causing the build to break.

This is fixed by locking the toolchain archiving step using flock.

Signed-off-by: Tobias Henkel <tobias.henkel@bmw-carit.de>
---
 meta/classes/icecc.bbclass | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

Patch

diff --git a/meta/classes/icecc.bbclass b/meta/classes/icecc.bbclass
index e3b05ac..970c557 100644
--- a/meta/classes/icecc.bbclass
+++ b/meta/classes/icecc.bbclass
@@ -202,6 +202,21 @@  def icc_get_and_check_tool(bb, d, tool):
     else:
         return t
 
+wait_for_file() {
+    local TIME_ELAPSED=0
+    local FILE_TO_TEST=$1
+    local TIMEOUT=$2
+    until [ -f "$FILE_TO_TEST" ]
+    do
+        TIME_ELAPSED=`expr $TIME_ELAPSED + 1`
+        if [ $TIME_ELAPSED -gt $TIMEOUT ]
+        then
+            return 1
+        fi
+        sleep 1
+    done
+}
+
 def set_icecc_env():
     # dummy python version of set_icecc_env
     return
@@ -247,10 +262,22 @@  set_icecc_env() {
         ICECC_AS="`which as`"
     fi
 
-    if [ ! -r "${ICECC_VERSION}" ]
+    if [ ! -f "${ICECC_VERSION}.done" ]
     then
         mkdir -p "`dirname "${ICECC_VERSION}"`"
-        ${ICECC_ENV_EXEC} "${ICECC_CC}" "${ICECC_CXX}" "${ICECC_AS}" "${ICECC_VERSION}"
+
+        # the ICECC_VERSION generation step must be locked by a mutex
+        # in order to prevent race conditions
+        if flock -n "${ICECC_VERSION}.lock" \
+            ${ICECC_ENV_EXEC} "${ICECC_CC}" "${ICECC_CXX}" "${ICECC_AS}" "${ICECC_VERSION}"
+        then
+            touch "${ICECC_VERSION}.done"
+        elif [ ! wait_for_file "${ICECC_VERSION}.done" 30 ]
+        then
+            # locking failed so wait for ${ICECC_VERSION}.done to appear
+            bbwarn "Timeout waiting for ${ICECC_VERSION}.done"
+            return
+        fi
     fi
 
     export ICECC_VERSION ICECC_CC ICECC_CXX