Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(model-manager): recover completed file copies

JPPhoto9e1ea1accd8f
diff --git a/invokeai/app/services/model_install/model_install_default.py b/invokeai/app/services/model_install/model_install_default.py
index 6e5e13fcc7..9af51aff4c 100644
--- a/invokeai/app/services/model_install/model_install_default.py
+++ b/invokeai/app/services/model_install/model_install_default.py
@@ -1,6 +1,7 @@
"""Model installation class."""
import ctypes
+import filecmp
import gc
import json
import locale
@@ -536,10 +537,23 @@ class ModelInstallService(ModelInstallServiceBase):
move(src, dst)
return
except PermissionError:
- # shutil.move can leave a partial destination after a cross-filesystem copy. Its contents
- # are unknown, so never retry or remove it.
if dst.exists() or dst.is_symlink():
- raise
+ # On Windows, shutil.move may copy a file successfully and then fail unlinking its
+ # source. Accept that state only when both regular files have identical contents.
+ if (
+ src.is_file()
+ and not src.is_symlink()
+ and dst.is_file()
+ and not dst.is_symlink()
+ and filecmp.cmp(src, dst, shallow=False)
+ ):
+ try:
+ src.unlink()
+ return
+ except PermissionError:
+ pass
+ else:
+ raise
gc.collect()
if tries_left == 1:
raise
diff --git a/tests/app/services/model_install/test_model_install.py b/tests/app/services/model_install/test_model_install.py
index 0b14c936cf..600c1fbfc3 100644
--- a/tests/app/services/model_install/test_model_install.py
+++ b/tests/app/services/model_install/test_model_install.py
@@ -134,6 +134,36 @@ def test_install(
assert model_record.source == embedding_file.as_posix()
+def test_file_install_retries_copy_then_unlink_permission_error(
+ mm2_installer: ModelInstallServiceBase,
+ embedding_file: Path,
+ mm2_app_config: InvokeAIAppConfig,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Windows can finish shutil.move's copy before failing to unlink its source."""
+ original_bytes = embedding_file.read_bytes()
+ real_move = shutil.move
+ calls = 0
+
+ def copy_then_fail_unlink(src: Path, dst: Path):
+ nonlocal calls
+ calls += 1
+ if calls == 1:
+ shutil.copy2(src, dst)
+ raise PermissionError("simulated Windows source unlink failure")
+ return real_move(src, dst)
+
+ monkeypatch.setattr(model_install_default, "move", copy_then_fail_unlink)
+
+ key = mm2_installer.install_path(embedding_file)
+
+ installed = mm2_installer.record_store.get_model(key)
+ installed_path = mm2_app_config.models_path / installed.path
+ assert installed_path.read_bytes() == original_bytes
+ assert not embedding_file.exists()
+ assert calls == 1
+
+
def test_directory_install_retries_windows_move_failures(
mm2_installer: ModelInstallServiceBase,
diffusers_dir: Path,