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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
"""Race handling on the board_images batch routes.
Two races found in review, both between a batch loop's read and its write:
- The scoped DELETE in the batch remove can match zero rows when a concurrent session moves the
image between the DTO read and the write. The row count is the only signal the scope held, and
the classification depends on where the image went.
- The per-name destination re-check in the batch add can start refusing mid-batch when the
destination board is revoked or deleted. That refusal is the request's problem, not the
name's: treated as a skip it answers 201 with empty lists, which the client reads as success
and clears the user's selection over.
"""
from unittest.mock import MagicMock
import pytest
from fastapi import HTTPException
from fastapi.testclient import TestClient
from invokeai.app.api.dependencies import ApiDependencies
from invokeai.app.api_app import app
from invokeai.app.services.invoker import Invoker
from invokeai.app.services.shared.database.database import Database
class MockApiDependencies(ApiDependencies):
invoker: Invoker
def __init__(self, invoker) -> None:
self.invoker = invoker
@pytest.fixture
def client() -> TestClient:
return TestClient(app)
def _install(monkeypatch: pytest.MonkeyPatch, mock_invoker: Invoker) -> None:
mock_deps = MockApiDependencies(mock_invoker)
# Several of these are None on the conftest's mock services; the routes need whole service
# doubles, installed via monkeypatch so they are restored between tests.
for name in ("image_moves", "images", "image_records", "board_images", "board_image_records", "board_records"):
monkeypatch.setattr(mock_invoker.services, name, MagicMock())
mock_invoker.services.image_moves.is_maintenance_active.return_value = False
monkeypatch.setattr("invokeai.app.api.routers.board_images.ApiDependencies", mock_deps)
monkeypatch.setattr("invokeai.app.api.routers._access.ApiDependencies", mock_deps)
monkeypatch.setattr("invokeai.app.api.routers.image_move_maintenance.ApiDependencies", mock_deps)
monkeypatch.setattr("invokeai.app.api.auth_dependencies.ApiDependencies", mock_deps)
@pytest.mark.parametrize(
("now_on_board", "record_exists", "expect_removed", "expect_failed", "expect_boards"),
[
# Moved to another board mid-batch: the ask -- off every board -- is not satisfied, and
# a retry will re-read and re-authorize against the board it actually sits on now.
("board-q", True, [], ["raced.png"], []),
# Concurrently uncategorized by someone else: the postcondition holds, and reporting it
# removed is what lets this client's stale view of the old board catch up. Safe in
# removed_images, unlike a deleted name: the DTO exists, so tag refetches succeed.
(None, True, ["raced.png"], [], ["none", "board-p"]),
# Deleted concurrently: a skip, matching the route's existing treatment of a name that
# vanished before the loop reached it. removed_images would drive a 404 refetch.
(None, False, [], [], []),
],
)
def test_remove_classifies_a_zero_row_scoped_delete_by_where_the_image_went(
monkeypatch: pytest.MonkeyPatch,
mock_invoker: Invoker,
client: TestClient,
now_on_board: str | None,
record_exists: bool,
expect_removed: list[str],
expect_failed: list[str],
expect_boards: list[str],
) -> None:
_install(monkeypatch, mock_invoker)
dto = MagicMock()
dto.board_id = "board-p"
monkeypatch.setattr(mock_invoker.services.images, "get_dto", MagicMock(return_value=dto))
# The default single-user identity is an admin, so the write-access check passes without
# touching board storage -- which is fine: the subject here is the write, not the check.
monkeypatch.setattr(mock_invoker.services.board_images, "remove_image_from_board", MagicMock(return_value=0))
monkeypatch.setattr(
mock_invoker.services.board_image_records, "get_board_for_image", MagicMock(return_value=now_on_board)
)
if record_exists:
monkeypatch.setattr(mock_invoker.services.image_records, "get", MagicMock(return_value=MagicMock()))
else:
from invokeai.app.services.image_records.image_records_common import ImageRecordNotFoundException
monkeypatch.setattr(
mock_invoker.services.image_records, "get", MagicMock(side_effect=ImageRecordNotFoundException)
)
response = client.post("/api/v1/board_images/batch/delete", json={"image_names": ["raced.png"]})
assert response.status_code == 201
body = response.json()
assert body["removed_images"] == expect_removed
assert body["failed_images"] == expect_failed
assert set(body["affected_boards"]) == set(expect_boards)
def test_remove_reports_a_zero_row_name_whose_existence_cannot_be_decided_as_failed(
monkeypatch: pytest.MonkeyPatch, mock_invoker: Invoker, client: TestClient
) -> None:
"""A transient storage error during the existence probe must not manufacture a success:
reported removed, the client's tag-driven getImageDTO refetch 404s if the image was in
fact deleted. A name whose state cannot be decided is reported as failed, never as done."""
import sqlite3
_install(monkeypatch, mock_invoker)
dto = MagicMock()
dto.board_id = "board-p"
monkeypatch.setattr(mock_invoker.services.images, "get_dto", MagicMock(return_value=dto))
monkeypatch.setattr(mock_invoker.services.board_images, "remove_image_from_board", MagicMock(return_value=0))
monkeypatch.setattr(mock_invoker.services.board_image_records, "get_board_for_image", MagicMock(return_value=None))
monkeypatch.setattr(
mock_invoker.services.image_records,
"get",
MagicMock(side_effect=sqlite3.OperationalError("database is locked")),
)
response = client.post("/api/v1/board_images/batch/delete", json={"image_names": ["undecidable.png"]})
assert response.status_code == 201
body = response.json()
assert body["removed_images"] == []
assert body["failed_images"] == ["undecidable.png"]
def test_facade_passes_the_row_count_through() -> None:
"""The middle layer: CI has no type checker, so a dropped `return` here silently restores
report-a-removal-that-did-not-happen at the route (`None == 0` is False), while the route
tests mock this service and the sqlite test pins the layer below it."""
from invokeai.app.services.board_images.board_images_default import BoardImagesService
service = BoardImagesService.__new__(BoardImagesService)
invoker = MagicMock()
invoker.services.board_image_records.remove_image_from_board = MagicMock(return_value=0)
service._BoardImagesService__invoker = invoker # pyright: ignore[reportAttributeAccessIssue]
assert service.remove_image_from_board("raced.png", "board-p") == 0
def test_remove_still_reports_a_nonzero_scoped_delete_as_removed(
monkeypatch: pytest.MonkeyPatch, mock_invoker: Invoker, client: TestClient
) -> None:
_install(monkeypatch, mock_invoker)
dto = MagicMock()
dto.board_id = "board-p"
monkeypatch.setattr(mock_invoker.services.images, "get_dto", MagicMock(return_value=dto))
monkeypatch.setattr(mock_invoker.services.board_images, "remove_image_from_board", MagicMock(return_value=1))
response = client.post("/api/v1/board_images/batch/delete", json={"image_names": ["ok.png"]})
assert response.status_code == 201
body = response.json()
assert body["removed_images"] == ["ok.png"]
assert body["failed_images"] == []
assert set(body["affected_boards"]) == {"none", "board-p"}
def test_add_reports_names_refused_by_a_destination_revoked_mid_batch(
monkeypatch: pytest.MonkeyPatch, mock_invoker: Invoker, client: TestClient
) -> None:
"""A revoked destination fails the remaining names; it must not empty into a silent 201."""
_install(monkeypatch, mock_invoker)
monkeypatch.setattr(mock_invoker.services.image_records, "get_user_id", MagicMock(return_value="system"))
monkeypatch.setattr(mock_invoker.services.board_image_records, "get_board_for_image", MagicMock(return_value=None))
monkeypatch.setattr(mock_invoker.services.board_images, "add_image_to_board", MagicMock(return_value=None))
# Pre-loop check passes, first per-name check passes, then the board flips Private (or is
# deleted): every later re-check refuses. Patched at the route seam because the default
# single-user identity is an admin, for whom the real helper never refuses.
calls = {"n": 0}
def write_access(board_id: str, current_user: object) -> None:
calls["n"] += 1
if calls["n"] > 2:
raise HTTPException(status_code=403, detail="Not authorized to modify this board")
monkeypatch.setattr("invokeai.app.api.routers.board_images._assert_board_write_access", write_access)
response = client.post(
"/api/v1/board_images/batch",
json={"board_id": "board-x", "image_names": ["first.png", "second.png", "third.png"]},
)
assert response.status_code == 201
body = response.json()
assert body["added_images"] == ["first.png"]
# The refused names are failures the client can toast and retry -- not skips that leave a
# 201 with empty lists for the UI to read as success.
assert set(body["failed_images"]) == {"second.png", "third.png"}
def test_remove_returns_the_row_count(database: Database) -> None:
"""The storage layer itself: the row count is the only signal the scoped DELETE's scope
held, and a `None` return silently restores report-a-removal-that-did-not-happen at the
route (`None == 0` is False)."""
from sqlalchemy import insert
from invokeai.app.services.board_image_records.board_image_records_default import BoardImageRecordStorage
from invokeai.app.services.board_records.board_records_default import BoardRecordStorage
from invokeai.app.services.shared.database.schema.images import images
boards = BoardRecordStorage(database)
on_board, other_board = boards.save("P", "system").board_id, boards.save("Q", "system").board_id
with database.begin(write=True) as conn:
conn.execute(
insert(images).values(
image_name="raced.png", image_origin="internal", image_category="general", width=1, height=1
)
)
storage = BoardImageRecordStorage(database)
storage.add_image_to_board(on_board, "raced.png")
assert storage.remove_image_from_board("raced.png", other_board) == 0
assert storage.remove_image_from_board("raced.png", on_board) == 1
assert storage.remove_image_from_board("raced.png", on_board) == 0
@pytest.mark.parametrize(
("now_on_board", "record_exists", "expect_removed", "expect_failed", "expect_boards"),
[
# Same three classifications as the batch loop above -- the single-image route runs
# the identical read-then-scoped-DELETE sequence, so it loses the identical race. It
# used to ignore the row count entirely and answer removed_images=[name] for all three
# of these, a false success the client had no way to see through.
("board-q", True, [], ["raced.png"], []),
(None, True, ["raced.png"], [], ["none", "board-p"]),
(None, False, [], [], []),
],
)
def test_single_remove_classifies_a_zero_row_scoped_delete_like_the_batch_route(
monkeypatch: pytest.MonkeyPatch,
mock_invoker: Invoker,
client: TestClient,
now_on_board: str | None,
record_exists: bool,
expect_removed: list[str],
expect_failed: list[str],
expect_boards: list[str],
) -> None:
_install(monkeypatch, mock_invoker)
dto = MagicMock()
dto.board_id = "board-p"
monkeypatch.setattr(mock_invoker.services.images, "get_dto", MagicMock(return_value=dto))
monkeypatch.setattr(mock_invoker.services.board_images, "remove_image_from_board", MagicMock(return_value=0))
monkeypatch.setattr(
mock_invoker.services.board_image_records, "get_board_for_image", MagicMock(return_value=now_on_board)
)
if record_exists:
monkeypatch.setattr(mock_invoker.services.image_records, "get", MagicMock(return_value=MagicMock()))
else:
from invokeai.app.services.image_records.image_records_common import ImageRecordNotFoundException
monkeypatch.setattr(
mock_invoker.services.image_records, "get", MagicMock(side_effect=ImageRecordNotFoundException)
)
response = client.request("DELETE", "/api/v1/board_images/", json={"image_name": "raced.png"})
assert response.status_code == 201
body = response.json()
assert body["removed_images"] == expect_removed
assert body["failed_images"] == expect_failed
assert set(body["affected_boards"]) == set(expect_boards)
def test_single_remove_of_an_uncategorized_image_reports_removed_without_a_write(
monkeypatch: pytest.MonkeyPatch, mock_invoker: Invoker, client: TestClient
) -> None:
"""No board_images row ever carries board_id="none", so the scoped DELETE could not match:
issuing it and then classifying the guaranteed zero-row miss would spend reads confirming
what the DTO already said. The postcondition holds, so it is removed, with no write at all."""
_install(monkeypatch, mock_invoker)
dto = MagicMock()
dto.board_id = None
monkeypatch.setattr(mock_invoker.services.images, "get_dto", MagicMock(return_value=dto))
remove = MagicMock(return_value=1)
monkeypatch.setattr(mock_invoker.services.board_images, "remove_image_from_board", remove)
response = client.request("DELETE", "/api/v1/board_images/", json={"image_name": "loose.png"})
assert response.status_code == 201
body = response.json()
assert body["removed_images"] == ["loose.png"]
assert body["failed_images"] == []
assert body["affected_boards"] == ["none"]
remove.assert_not_called()
def test_single_remove_still_reports_a_nonzero_scoped_delete_as_removed(
monkeypatch: pytest.MonkeyPatch, mock_invoker: Invoker, client: TestClient
) -> None:
_install(monkeypatch, mock_invoker)
dto = MagicMock()
dto.board_id = "board-p"
monkeypatch.setattr(mock_invoker.services.images, "get_dto", MagicMock(return_value=dto))
monkeypatch.setattr(mock_invoker.services.board_images, "remove_image_from_board", MagicMock(return_value=1))
response = client.request("DELETE", "/api/v1/board_images/", json={"image_name": "ok.png"})
assert response.status_code == 201
body = response.json()
assert body["removed_images"] == ["ok.png"]
assert body["failed_images"] == []
assert set(body["affected_boards"]) == {"none", "board-p"}