InvokeAI/invokeai/app
psychedelicious@windows 7c0dfd74a5 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.
2024-07-13 14:46:41 +10:00
..
api fix(api): deleting large images fails 2024-07-13 14:46:41 +10:00
assets/images tweaks in response to psychedelicious review of PR 2023-07-26 15:27:04 +10:00
invocations fix(app): vae tile size field description 2024-07-12 06:30:27 -07:00
services Warn on invalid model configs in the DB rather than crashing. 2024-07-11 21:05:55 -04:00
shared tidy(nodes): move all field things to fields.py 2024-03-01 10:42:33 +11:00
util Add ruff rule to disallow relative parent imports. 2024-07-04 09:35:37 -04:00
__init__.py fix: make invocation_context.py accessible to mkdocs 2024-03-01 10:42:33 +11:00
api_app.py fix(app): windows indefinite hang while finding port 2024-07-13 14:46:41 +10:00
run_app.py feat: single app entrypoint with CLI arg parsing 2024-03-19 09:24:28 +11:00