From fa788684e6fcadabd43d5aaa3fe6520e2230a85c Mon Sep 17 00:00:00 2001 From: Rahul Anil Amrutsagar Date: Sat, 14 Nov 2026 13:20:47 +0530 Subject: [PATCH 1/2] Delete partial file when local download_object_range fails (#2223) --- CHANGES.rst | 5 +++++ libcloud/storage/drivers/local.py | 11 ++++++++--- libcloud/test/storage/test_local.py | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d4fe5a0529..214c05242e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -117,6 +117,11 @@ Compute Storage ~~~~~~~ +- [Local Storage] Delete the partially written file when ``download_object_range`` + fails and ``delete_on_failure`` is set. + (GITHUB-2223) + [Rahul - @raamrutsagarb25-tech] + - [S3] Add ``ex_allow_path_double_slashes`` to preserve object key slashes per driver without changing the global URL normalization setting. (GITHUB-1654) diff --git a/libcloud/storage/drivers/local.py b/libcloud/storage/drivers/local.py index c6fc1f83ab..3421bcfc31 100644 --- a/libcloud/storage/drivers/local.py +++ b/libcloud/storage/drivers/local.py @@ -505,9 +505,14 @@ def download_object_range( obj=obj, start_bytes=start_bytes, end_bytes=end_bytes ) - with open(file_path, "wb") as fp: - for chunk in iterator: - fp.write(chunk) + try: + with open(file_path, "wb") as fp: + for chunk in iterator: + fp.write(chunk) + except Exception: + if delete_on_failure and os.path.exists(file_path): + os.remove(file_path) + raise return True diff --git a/libcloud/test/storage/test_local.py b/libcloud/test/storage/test_local.py index 773124bd1a..eb01da0ecf 100644 --- a/libcloud/test/storage/test_local.py +++ b/libcloud/test/storage/test_local.py @@ -579,6 +579,23 @@ def test_download_object_range_success(self): self.remove_tmp_file(tmppath) os.unlink(destination_path) + def test_download_object_range_deletes_file_on_failure(self): + container = self.driver.create_container("range-failure") + obj = self.driver.upload_object_via_stream(iter([b"payload"]), container, "object") + + os.unlink(self.driver.get_object_cdn_url(obj)) + destination_path = os.path.join(self.key, "download") + + with self.assertRaises(FileNotFoundError): + self.driver.download_object_range( + obj=obj, + destination_path=destination_path, + start_bytes=0, + delete_on_failure=True, + ) + + self.assertFalse(os.path.exists(destination_path)) + def test_download_object_range_as_stream_success(self): content = b"0123456789123456789" tmppath = self.make_tmp_file(content=content) From 37030ec8725ff9c45fb9d6a7a83c3b35969e8719 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 17 Nov 2026 16:40:04 +0100 Subject: [PATCH 2/2] Address review feedback: verify partial-file cleanup with a failing stream - Replace the previous test, which deleted the source object before the download (raising FileNotFoundError before any data was yielded), with a mocked stream that yields a chunk and then raises. This actually exercises the partially-written-file cleanup path. - Add a test asserting the partial file is preserved when delete_on_failure=False. - Guard os.remove() in download_object_range's failure handler so an error removing the partial file doesn't mask the original download exception. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- libcloud/storage/drivers/local.py | 7 +++- libcloud/test/storage/test_local.py | 56 ++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/libcloud/storage/drivers/local.py b/libcloud/storage/drivers/local.py index 3421bcfc31..b1c5ecfe49 100644 --- a/libcloud/storage/drivers/local.py +++ b/libcloud/storage/drivers/local.py @@ -511,7 +511,12 @@ def download_object_range( fp.write(chunk) except Exception: if delete_on_failure and os.path.exists(file_path): - os.remove(file_path) + try: + os.remove(file_path) + except OSError: + # Don't let a failure to clean up the partially + # written file mask the original download exception. + pass raise return True diff --git a/libcloud/test/storage/test_local.py b/libcloud/test/storage/test_local.py index eb01da0ecf..1c9b61d489 100644 --- a/libcloud/test/storage/test_local.py +++ b/libcloud/test/storage/test_local.py @@ -583,19 +583,59 @@ def test_download_object_range_deletes_file_on_failure(self): container = self.driver.create_container("range-failure") obj = self.driver.upload_object_via_stream(iter([b"payload"]), container, "object") - os.unlink(self.driver.get_object_cdn_url(obj)) destination_path = os.path.join(self.key, "download") - with self.assertRaises(FileNotFoundError): - self.driver.download_object_range( - obj=obj, - destination_path=destination_path, - start_bytes=0, - delete_on_failure=True, - ) + error = OSError("stream failed") + + def failing_iterator(*args, **kwargs): + yield b"partial data" + raise error + + with mock.patch.object( + self.driver, "download_object_range_as_stream", side_effect=failing_iterator + ): + with self.assertRaises(OSError) as raised: + self.driver.download_object_range( + obj=obj, + destination_path=destination_path, + start_bytes=0, + delete_on_failure=True, + ) + self.assertIs(raised.exception, error) self.assertFalse(os.path.exists(destination_path)) + def test_download_object_range_preserves_partial_file_when_delete_on_failure_is_false(self): + container = self.driver.create_container("range-failure-preserve") + obj = self.driver.upload_object_via_stream(iter([b"payload"]), container, "object") + + destination_path = os.path.join(self.key, "download") + + error = OSError("stream failed") + + def failing_iterator(*args, **kwargs): + yield b"partial data" + raise error + + with mock.patch.object( + self.driver, "download_object_range_as_stream", side_effect=failing_iterator + ): + with self.assertRaises(OSError) as raised: + self.driver.download_object_range( + obj=obj, + destination_path=destination_path, + start_bytes=0, + delete_on_failure=False, + ) + + self.assertIs(raised.exception, error) + self.assertTrue(os.path.exists(destination_path)) + + with open(destination_path, "rb") as fp: + self.assertEqual(fp.read(), b"partial data") + + os.unlink(destination_path) + def test_download_object_range_as_stream_success(self): content = b"0123456789123456789" tmppath = self.make_tmp_file(content=content)