Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(core): keep the transfer error when a .partial file cannot be removed (#5344)

hoobnn3e0bfb40b593
diff --git a/changelog.d/5344.fixed.md b/changelog.d/5344.fixed.md
new file mode 100644
index 000000000..c646d355b
--- /dev/null
+++ b/changelog.d/5344.fixed.md
@@ -0,0 +1 @@
+`load_state_dict_from_url`, `download_file_from_url` and `download_hf_file` keep handling a failed transfer as such when its temporary `.partial` file cannot be removed, for example because another process still holds it open on Windows. The `PermissionError` from that cleanup used to replace the transfer error: a truncated or timed-out transfer was then not retried, the `RuntimeError` named the `PermissionError` as its cause, and a `KeyboardInterrupt` during the transfer was swallowed and the next source tried. Now the transfer error is kept and the leftover file is reported in a warning, to be deleted by hand.
diff --git a/kornia/core/download.py b/kornia/core/download.py
index 198092ab2..c4e77ae2b 100644
--- a/kornia/core/download.py
+++ b/kornia/core/download.py
@@ -721,8 +721,17 @@ def _download_url_to_file(
f"that take no timeout, such as pretrained model constructors"
) from e
finally:
- if os.path.exists(partial):
+ try:
os.remove(partial)
+ except FileNotFoundError:
+ pass
+ except OSError as e:
+ # Another process (an antivirus scanner on Windows, say) can still hold the file open. Raising here
+ # would replace the error that ended the transfer, which decides whether the download is retried.
+ _warn(
+ f"Could not remove the temporary download file {partial}: {e}. "
+ f"Delete it by hand once no other process holds it."
+ )
def _prefetch_to_cache(url: str, kwargs: dict[str, Any], timeout: float) -> bool:
diff --git a/tests/core/test_download.py b/tests/core/test_download.py
index 52ae5b505..42aa7215c 100644
--- a/tests/core/test_download.py
+++ b/tests/core/test_download.py
@@ -19,6 +19,7 @@ from __future__ import annotations
import collections
import copy
+import errno
import hashlib
import http.client
import http.server
@@ -2394,6 +2395,70 @@ class TestTruncatedTransfer:
assert dst.read_bytes() == b"previous"
assert [p.name for p in tmp_path.iterdir()] == ["t.bin"]
+ @staticmethod
+ def _partial_in_use(monkeypatch, code: int = errno.EACCES) -> None:
+ # Removing the temporary file fails, as when another process holds it open on Windows. ``OSError`` maps
+ # EACCES to ``PermissionError``; EBUSY stays a plain ``OSError``.
+ remove = os.remove
+
+ def in_use(path):
+ if str(path).endswith(".partial"):
+ raise OSError(code, "in use by another process", path)
+ remove(path)
+
+ monkeypatch.setattr(download_mod.os, "remove", in_use)
+
+ @pytest.mark.parametrize("code", [errno.EACCES, errno.EBUSY], ids=["eacces", "ebusy"])
+ def test_a_failed_cleanup_does_not_replace_the_transfer_error(
+ self, scripted_server, monkeypatch, tmp_path, code
+ ) -> None:
+ responses, url, _ = scripted_server
+ responses["/t.bin"] = (1000, b"z" * 10)
+ self._partial_in_use(monkeypatch, code)
+
+ with pytest.warns(UserWarning, match="Could not remove the temporary download file"):
+ with pytest.raises(download_mod._TruncatedTransfer):
+ download_mod._download_url_to_file(url("/t.bin"), str(tmp_path / "t.bin"), progress=False, timeout=5.0)
+
+ assert [p.suffix for p in tmp_path.iterdir()] == [".partial"]
+
+ def test_a_failed_cleanup_does_not_replace_an_interrupt(self, monkeypatch, tmp_path) -> None:
+ def interrupted(*args, **kwargs):
+ raise KeyboardInterrupt
+
+ monkeypatch.setattr(download_mod, "urlopen", interrupted)
+ self._partial_in_use(monkeypatch)
+
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ with pytest.raises(KeyboardInterrupt):
+ download_mod._download_url_to_file("http://127.0.0.1:9/t.bin", str(tmp_path / "t.bin"), progress=False)
+
+ def test_a_short_body_is_still_retried_when_cleanup_fails(self, scripted_server, monkeypatch, tmp_path) -> None:
+ # A PermissionError from the cleanup is not transient, so it used to end the retries after the first attempt.
+ responses, url, hits = scripted_server
+ responses["/t.bin"] = (1000, b"z" * 10)
+ self._partial_in_use(monkeypatch)
+
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ with pytest.raises(RuntimeError) as excinfo:
+ download_file_from_url(url("/t.bin"), model_dir=str(tmp_path / "cache"), progress=False)
+
+ assert isinstance(excinfo.value.__cause__, http.client.IncompleteRead)
+ assert hits["/t.bin"] == download_mod._MAX_ATTEMPTS
+
+ def test_a_complete_transfer_warns_nothing(self, scripted_server, tmp_path) -> None:
+ # The temporary file is already renamed into place, so the cleanup finds nothing to remove or report.
+ responses, url, _ = scripted_server
+ responses["/t.bin"] = b"z" * 10
+
+ with warnings.catch_warnings():
+ warnings.simplefilter("error", UserWarning)
+ download_mod._download_url_to_file(url("/t.bin"), str(tmp_path / "t.bin"), progress=False, timeout=5.0)
+
+ assert [p.name for p in tmp_path.iterdir()] == ["t.bin"]
+
class TestCacheNameEdgeCases:
"""Review follow-ups on the cache-name checks (#5217, #5219)."""