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(image-index): log why a vocabulary cache write failed

Lincoln Stein4981c54895c7
diff --git a/invokeai/app/services/image_index/image_index_default.py b/invokeai/app/services/image_index/image_index_default.py
index dbf46f8e33..36ab8331fb 100644
--- a/invokeai/app/services/image_index/image_index_default.py
+++ b/invokeai/app/services/image_index/image_index_default.py
@@ -339,7 +339,16 @@ class ImageIndexService(ImageIndexServiceBase):
np.savez(staging_path, embeddings=embeddings, fingerprint=np.str_(fingerprint))
os.replace(staging_path, cache_path)
except Exception:
- self._invoker.services.logger.warning(f"Could not write cluster vocabulary cache to {cache_path}")
+ # With the exception, not just the path. The in-memory cache
+ # below is assigned either way, so a write failure costs
+ # nothing until the next restart and is invisible until someone
+ # goes looking at startup times. The `.tmp` bug above survived
+ # because this line said only that something had gone wrong,
+ # never what — the FileNotFoundError it swallowed names the
+ # missing staging file outright.
+ self._invoker.services.logger.warning(
+ f"Could not write cluster vocabulary cache to {cache_path}", exc_info=True
+ )
self._vocab_cache = (vocabulary, embeddings)
def _get_text_encoder(self) -> tuple[Any, Any, bool]:
diff --git a/tests/app/services/image_index/test_image_index_service.py b/tests/app/services/image_index/test_image_index_service.py
index 01e3d155a2..21a11b2996 100644
--- a/tests/app/services/image_index/test_image_index_service.py
+++ b/tests/app/services/image_index/test_image_index_service.py
@@ -1767,6 +1767,34 @@ def test_a_cached_vocabulary_is_reused_instead_of_re_embedded(tmp_path, monkeypa
assert np.array_equal(cached, matrix)
+def test_a_failed_cache_write_says_what_went_wrong(tmp_path, monkeypatch: pytest.MonkeyPatch) -> None:
+ # Nothing else reports this failure: the in-memory cache below the handler
+ # is assigned either way, so labelling keeps working and the only symptom
+ # is a slow startup nobody attributes to it. The `.tmp` staging-name bug
+ # survived for exactly that reason — the warning named the cache path but
+ # never the FileNotFoundError that would have identified it outright.
+ matrix = np.arange(3 * DIM, dtype=EMBEDDING_DTYPE).reshape(3, DIM)
+ service = _vocab_build_service(tmp_path, monkeypatch, matrix)
+ recorded: list[tuple[str, object]] = []
+ service._invoker.services.logger = SimpleNamespace( # type: ignore[union-attr]
+ warning=lambda message, exc_info=False: recorded.append((str(message), exc_info))
+ )
+
+ def _refuse(src: object, dst: object) -> None:
+ raise PermissionError("simulated: another instance holds the cache open")
+
+ monkeypatch.setattr(image_index_default.os, "replace", _refuse)
+
+ service._build_vocab_embeddings()
+
+ assert len(recorded) == 1
+ message, exc_info = recorded[0]
+ assert "Could not write cluster vocabulary cache" in message
+ assert exc_info is True
+ # The run itself is unaffected — which is why the warning has to carry it.
+ assert service._vocab_cache is not None
+
+
def test_batch_normalization_zeroes_a_degenerate_row_instead_of_failing(service: ImageIndexService) -> None:
# One bad row out of ~11,700 must not discard the whole vocabulary build:
# that raises past the endpoint's handling as a 500 and, nothing having been