Skip to content

Commit 7c0dfd7

Browse files
fix(api): deleting large images fails
This issue is caused by a race condition. When a large image is served to the client, it is done using a streaming `FileResponse`. This concurrently serves the image straight from disk. The file is kept open by FastAPI until the image is fully served. When a user deletes an image before the file is done serving, the delete fails because the file is still held by FastAPI. To reproduce the issue: - Create a very large image (8k reliably creates the issue). - Create a smaller image, so that the first image in the gallery is not the large image. - Refresh the app. The small image should be selected. - Select the large image and immediately delete it. You have to be fast, to delete it before it finishes loading. - In the terminal, we expect to see an error saying `Failed to delete image file`, and the image does not disappear from the UI. - After a short wait, once the image has fully loaded, try deleting it again. We expect this to work. The workaround is to instead serve the image from memory. Loading the image to memory is very fast, so there is only a tiny window in which we could create the race condition, but it technically could still occur, because FastAPI is asynchronous and handles requests concurrently. Once we load the image into memory, deletions of that image will work. Then we return a normal `Response` object with the image bytes. This is essentially what `FileResponse` does - except it uses `anyio.open_file`, which is async. The tradeoff is that the server thread is blocked while opening the file. I think this is a fair tradeoff. A future enhancement could be to implement soft deletion of images (db is already set up for this), and then clean up deleted image files on startup/shutdown. We could move back to using the async `FileResponse` for best responsiveness in the server without any risk of race conditions.
1 parent 2c1a912 commit 7c0dfd7

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

invokeai/app/api/routers/images.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,14 @@ async def get_image_workflow(
233233
)
234234
async def get_image_full(
235235
image_name: str = Path(description="The name of full-resolution image file to get"),
236-
) -> FileResponse:
236+
) -> Response:
237237
"""Gets a full-resolution image file"""
238238

239239
try:
240240
path = ApiDependencies.invoker.services.images.get_path(image_name)
241-
242-
if not ApiDependencies.invoker.services.images.validate_path(path):
243-
raise HTTPException(status_code=404)
244-
245-
response = FileResponse(
246-
path,
247-
media_type="image/png",
248-
filename=image_name,
249-
content_disposition_type="inline",
250-
)
241+
with open(path, "rb") as f:
242+
content = f.read()
243+
response = Response(content, media_type="image/png")
251244
response.headers["Cache-Control"] = f"max-age={IMAGE_MAX_AGE}"
252245
return response
253246
except Exception:
@@ -268,15 +261,14 @@ async def get_image_full(
268261
)
269262
async def get_image_thumbnail(
270263
image_name: str = Path(description="The name of thumbnail image file to get"),
271-
) -> FileResponse:
264+
) -> Response:
272265
"""Gets a thumbnail image file"""
273266

274267
try:
275268
path = ApiDependencies.invoker.services.images.get_path(image_name, thumbnail=True)
276-
if not ApiDependencies.invoker.services.images.validate_path(path):
277-
raise HTTPException(status_code=404)
278-
279-
response = FileResponse(path, media_type="image/webp", content_disposition_type="inline")
269+
with open(path, "rb") as f:
270+
content = f.read()
271+
response = Response(content, media_type="image/webp")
280272
response.headers["Cache-Control"] = f"max-age={IMAGE_MAX_AGE}"
281273
return response
282274
except Exception:

0 commit comments

Comments
 (0)