From edbf409a40297e6f3b3d240b90be7edaa4aab258 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 23 Nov 2023 11:36:56 +0100 Subject: [PATCH] osbuild: fix missing initialization of fd in osbuild.loop.Loop When osbuild.loop.Loop calls `__init__()` it assigns the `self.fd` on open. However if that open call fails for whatever reason (not found, permissions) the cleanup in `__del__` will fail in confusing ways because `self.fd` is not initialized yet. It also prevents the correct error from getting reported. A tiny test is added to ensure this does not regress. --- osbuild/loop.py | 1 + test/mod/test_loop.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/osbuild/loop.py b/osbuild/loop.py index a9345279..77334c3f 100644 --- a/osbuild/loop.py +++ b/osbuild/loop.py @@ -120,6 +120,7 @@ class Loop: self.devname = f"loop{minor}" self.minor = minor self.on_close = None + self.fd = -1 with contextlib.ExitStack() as stack: if not dir_fd: diff --git a/test/mod/test_loop.py b/test/mod/test_loop.py index 9422114d..0b5ba6bb 100644 --- a/test/mod/test_loop.py +++ b/test/mod/test_loop.py @@ -253,3 +253,8 @@ def test_on_close(tempdir): lo.close() ctl.close() + + +def test_loop_handles_error_in_init(): + with pytest.raises(FileNotFoundError): + lopo = loop.Loop("non-existing")