From 9371eb9eaa3d0a7cab876eb4c7b70f519dfbd915 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Fri, 2 Aug 2019 00:55:21 +0200 Subject: [PATCH] ObjectStore/get_tree: make sure to clean up the context manager Even if the yield raises an exception, we must always unmount to clean up. Signed-off-by: Tom Gundersen --- osbuild/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/osbuild/__init__.py b/osbuild/__init__.py index f2dcd00a..714779b5 100644 --- a/osbuild/__init__.py +++ b/osbuild/__init__.py @@ -145,8 +145,10 @@ class ObjectStore: with tempfile.TemporaryDirectory(dir=self.store) as tmp: if tree_id: subprocess.run(["mount", "-o", "bind,ro,mode=0755", f"{self.refs}/{tree_id}", tmp], check=True) - yield tmp - subprocess.run(["umount", "--lazy", tmp], check=True) + try: + yield tmp + finally: + subprocess.run(["umount", "--lazy", tmp], check=True) else: # None was given as tree_id, just return an empty directory yield tmp