[bitbake-devel,v2] bb.utils.fileslocked: don't leak files if yield throws

Submitted by Nicolas Cornu via bitbake-devel on Dec. 10, 2019, 7:10 p.m. | Patch ID: 167891

Details

Message ID 1576005020-49841-1-git-send-email-chris.laplante@agilent.com
State New
Headers show

Commit Message

Nicolas Cornu via bitbake-devel Dec. 10, 2019, 7:10 p.m.
Discovered with a recipe under devtool. The ${S}/singletask.lock file (added by
externalsrc.bbclass) was leaked, giving a warning like:

  WARNING: <PN>+git999-r0 do_populate_lic: /home/laplante/yocto/sources/poky/bitbake/lib/bb/build.py:582: ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/laplante/yocto/build/workspace/sources/<PN>/singletask.lock' mode='a+' encoding='UTF-8'>
    exec_func(task, localdata)

Signed-off-by: Chris Laplante <chris.laplante@agilent.com>
---
v2: added missing sign-off

 lib/bb/utils.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

--
2.7.4

Patch hide | download patch | download mbox

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 8d40bcd..d65265c 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -428,10 +428,11 @@  def fileslocked(files):
         for lockfile in files:
             locks.append(bb.utils.lockfile(lockfile))

-    yield
-
-    for lock in locks:
-        bb.utils.unlockfile(lock)
+    try:
+        yield
+    finally:
+        for lock in locks:
+            bb.utils.unlockfile(lock)

 @contextmanager
 def timeout(seconds):