diff mbox series

fetch2: ensure lockfiles are unlocked on exception

Message ID 20250920150813.753989-1-ross.burton@arm.com
State New
Headers show
Series fetch2: ensure lockfiles are unlocked on exception | expand

Commit Message

Ross Burton Sept. 20, 2025, 3:08 p.m. UTC
Ensure that every time a lockfile is created, it is also unlocked in a
finally: block. Otherwise, the lock file remains open and Python warns:

ResourceWarning: unclosed file <_io.FileIO
name='/tmp/bitbake-fetch-srfv29ek/download/git2/git.openembedded.org.bitbake.lock'
mode='ab+' closefd=True>

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 bitbake/lib/bb/fetch2/__init__.py | 60 ++++++++++++++++---------------
 1 file changed, 32 insertions(+), 28 deletions(-)
diff mbox series

Patch

diff --git a/bitbake/lib/bb/fetch2/__init__.py b/bitbake/lib/bb/fetch2/__init__.py
index 0ad987c596c..bdd4973945a 100644
--- a/bitbake/lib/bb/fetch2/__init__.py
+++ b/bitbake/lib/bb/fetch2/__init__.py
@@ -1067,10 +1067,10 @@  def try_mirror_url(fetch, origud, ud, ld, check = False):
     # Return of None or a value means we're finished
     # False means try another url
 
-    if ud.lockfile and ud.lockfile != origud.lockfile:
-        lf = bb.utils.lockfile(ud.lockfile)
-
     try:
+        if ud.lockfile and ud.lockfile != origud.lockfile:
+            lf = bb.utils.lockfile(ud.lockfile)
+
         if check:
             found = ud.method.checkstatus(fetch, ud, ld)
             if found:
@@ -1864,10 +1864,10 @@  class Fetch(object):
             m = ud.method
             done = False
 
-            if ud.lockfile:
-                lf = bb.utils.lockfile(ud.lockfile)
-
             try:
+                if ud.lockfile:
+                    lf = bb.utils.lockfile(ud.lockfile)
+
                 self.d.setVar("BB_NO_NETWORK", network)
                 if m.verify_donestamp(ud, self.d) and not m.need_update(ud, self.d):
                     done = True
@@ -2000,18 +2000,20 @@  class Fetch(object):
         unpack_tracer.start(root, self.ud, self.d)
 
         for u in urls:
-            ud = self.ud[u]
-            ud.setup_localpath(self.d)
+            try:
+                ud = self.ud[u]
+                ud.setup_localpath(self.d)
 
-            if ud.lockfile:
-                lf = bb.utils.lockfile(ud.lockfile)
+                if ud.lockfile:
+                    lf = bb.utils.lockfile(ud.lockfile)
 
-            unpack_tracer.start_url(u)
-            ud.method.unpack(ud, root, self.d)
-            unpack_tracer.finish_url(u)
+                unpack_tracer.start_url(u)
+                ud.method.unpack(ud, root, self.d)
+                unpack_tracer.finish_url(u)
 
-            if ud.lockfile:
-                bb.utils.unlockfile(lf)
+            finally:
+                if ud.lockfile:
+                    bb.utils.unlockfile(lf)
 
         unpack_tracer.complete()
 
@@ -2024,23 +2026,25 @@  class Fetch(object):
             urls = self.urls
 
         for url in urls:
-            if url not in self.ud:
-                self.ud[url] = FetchData(url, self.d)
-            ud = self.ud[url]
-            ud.setup_localpath(self.d)
+            try:
+                if url not in self.ud:
+                    self.ud[url] = FetchData(url, self.d)
+                ud = self.ud[url]
+                ud.setup_localpath(self.d)
 
-            if not ud.localfile and ud.localpath is None:
-                continue
+                if not ud.localfile and ud.localpath is None:
+                    continue
 
-            if ud.lockfile:
-                lf = bb.utils.lockfile(ud.lockfile)
+                if ud.lockfile:
+                    lf = bb.utils.lockfile(ud.lockfile)
 
-            ud.method.clean(ud, self.d)
-            if ud.donestamp:
-                bb.utils.remove(ud.donestamp)
+                ud.method.clean(ud, self.d)
+                if ud.donestamp:
+                    bb.utils.remove(ud.donestamp)
 
-            if ud.lockfile:
-                bb.utils.unlockfile(lf)
+            finally:
+                if ud.lockfile:
+                    bb.utils.unlockfile(lf)
 
     def expanded_urldata(self, urls=None):
         """