Skip to content

Commit c506955

Browse files
fix(mm): fail when model exists at path instead of finding unused new path
When installing a model, the previous, graceful logic would increment a suffix on the destination path until found a free path for the model. But because model file installation and record creation are not in a transaction, we could end up moving the file successfully and fail to create the record: - User attempts to install an already-installed model - Attempt to move the downloaded model from download tempdir to destination path - The path already exists - Add `_1` or similar to the path until we find a path that is free - Move the model - Create the model record - FK constraint violation bc we already have a model w/ that name, but the model file has already been moved into the invokeai dir. Closes #8416
1 parent 9b220f6 commit c506955

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

invokeai/app/services/model_install/model_install_default.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,16 +623,13 @@ def _move_model(self, old_path: Path, new_path: Path) -> Path:
623623
if old_path == new_path:
624624
return old_path
625625

626+
if new_path.exists():
627+
raise FileExistsError(f"Cannot move {old_path} to {new_path}: destination already exists")
628+
626629
new_path.parent.mkdir(parents=True, exist_ok=True)
627630

628-
# if path already exists then we jigger the name to make it unique
629-
counter: int = 1
630-
while new_path.exists():
631-
path = new_path.with_stem(new_path.stem + f"_{counter:02d}")
632-
if not path.exists():
633-
new_path = path
634-
counter += 1
635631
move(old_path, new_path)
632+
636633
return new_path
637634

638635
def _probe(self, model_path: Path, config: Optional[ModelRecordChanges] = None):

0 commit comments

Comments
 (0)