[PATCH v2] utils: Fix lockfile_to_exclusive to avoid deadlock

Richard Purdie <[email protected]>
Newsgroups org.openembedded.lists.bitbake-devel
Message-ID <[email protected]>
We need to drop the existing lock when trying to get the exclusive lock
else we'd deadlock with another process trying to do the same thing.

Signed-off-by: Richard Purdie <[email protected]>
---
 lib/bb/fetch2/__init__.py | 2 +-
 lib/bb/utils.py           | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/lib/bb/fetch2/__init__.py b/lib/bb/fetch2/__init__.py
index 4a3119ad09d..9cb268be503 100644
--- a/lib/bb/fetch2/__init__.py
+++ b/lib/bb/fetch2/__init__.py
@@ -1907,7 +1907,7 @@ class Fetch(object):
                 if m.verify_donestamp(ud, self.d) and not m.need_update(ud, self.d):
                     done = True
                 if not done:
-                    bb.utils.lockfile_to_exclusive(lf)
+                    lf = bb.utils.lockfile_to_exclusive(lf)
                     if m.try_premirror(ud, self.d):
                         done = m.try_mirrors(self, ud, self.d, 'PREMIRRORS')
 
diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index b635f68a153..37edd435350 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -587,12 +587,13 @@ def lockfile(name, shared=False, retry=True, block=False):
         if not retry:
             return None
 
+# We have to drop the existing lock to avoid deadlocks
 def lockfile_to_exclusive(lf):
     if not lf:
         return
-    fileno = lf.fileno()
-    fcntl.flock(fileno, fcntl.LOCK_EX)
-    return
+    name = lf.name
+    unlockfile(lf)
+    return lockfile(name)
 
 def unlockfile(lf):
     """
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.