Skip to content

DeleteImage fails if it is sent right after the image is generated #1194

@Lawrr

Description

@Lawrr

Expected Behavior

  1. I send an API request to GenerateText2Image and get returned the path to the image
  2. I then send an API request to DeleteImage and it deletes the image successfully

Actual Behavior

  1. I send an API request to GenerateText2Image and get returned the path to the image
  2. I then send an API request to DeleteImage and it returns an error

Steps to Reproduce

  1. Send an API request to GenerateText2Image
  2. Send an API request to DeleteImage (will be dependent on timing, see logs below)

Debug Logs

Logs (with some additional log lines added):

Error 1 - If GetOrCreatePreviewFor wins

22:12:01.641 [Debug] [ComfyUI-0/STDERR] Prompt executed in 25.48 seconds
22:12:02.162 [Info] T2API.saveImage start
22:12:02.190 [Info] session.SaveImage start
22:12:02.197 [Info] session.SaveImage done
22:12:02.200 [Info] async Utilities.RunCheckedTask start
22:12:02.202 [Info] T2API.saveImage done
22:12:02.207 [Info] Generated an image in 7.53 sec (prep) and 26.00 sec (gen)
22:12:02.221 [Info] Returning images to client
22:12:02.274 [Info] File.WriteAllBytes start
22:12:02.277 [Info] File.WriteAllBytes done
22:12:02.280 [Info] API.HandleAsyncRequest - start handling API request
22:12:02.281 [Info] OutputMetadataTracker.GetOrCreatePreviewFor start
22:12:02.316 [Warning] Error reading image preview for file 'C:/Folders/StableDiffusion/SwarmUI/Output/local/raw/foo.jpg': System.IO.IOException: The process cannot access the file 'C:\Folders\StableDiffusion\SwarmUI\Output\local\raw\2025-12-23\foo.jpg' because it is being used by another process.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.File.ReadAllBytes(String path)
   at SwarmUI.Utils.OutputMetadataTracker.GetOrCreatePreviewFor(String file) in C:\Folders\StableDiffusion\SwarmUI\src\Utils\OutputMetadataTracker.cs:line 276
22:12:02.320 [Info] OutputMetadataTracker.GetOrCreatePreviewFor done
22:12:02.322 [Debug] Saved an output file as 'C:\Folders\StableDiffusion\SwarmUI\Output\local\raw\2025-12-23\foo.jpg'

Error 2 - If DeleteImage wins

21:24:47.647 [Debug] [ComfyUI-0/STDERR] Prompt executed in 23.18 seconds
21:24:48.108 [Info] T2API.saveImage start
21:24:48.111 [Info] session.SaveImage start
21:24:48.117 [Info] session.SaveImage done
21:24:48.119 [Info] async Utilities.RunCheckedTask start
21:24:48.120 [Info] T2API.saveImage done
21:24:48.124 [Info] Generated an image in 9.53 sec (prep) and 23.64 sec (gen)
21:24:48.133 [Info] Returning images to client
21:24:48.185 [Info] File.WriteAllBytes start
21:24:48.189 [Info] File.WriteAllBytes done
21:24:48.191 [Info] API.HandleAsyncRequest - start handling API request
21:24:48.200 [Warning] Error reading image preview for file 'C:/Folders/StableDiffusion/SwarmUI/Output/local/raw/foo.jpg' because it is being used by another process.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.File.ReadAllBytes(String path)
   at SwarmUI.Utils.OutputMetadataTracker.GetOrCreatePreviewFor(String file) in C:\Folders\StableDiffusion\SwarmUI\src\Utils\OutputMetadataTracker.cs:line 276
21:24:48.210 [Debug] Saved an output file as 'C:\Folders\StableDiffusion\SwarmUI\Output\local\raw\2025-12-23\foo.jpg'

Other

Seems to be a race condition between the DeleteImage API request and this line:

OutputMetadataTracker.GetOrCreatePreviewFor(fullPath.Replace('\\', '/'));

but in theory it could also happen if the image hasn't been saved yet by

File.WriteAllBytes(fullPath, actualFile.RawData);

Note: Only started running into this after I updated to master. Going back to the 0.9.7-Beta release doesn't seem to have this issue. It seems like for whatever reason, previously the DeleteImage API request began being handled after the image was fully finished saving in that release. i.e. logs looked like this instead:

21:11:08.886 [Debug] [ComfyUI-0/STDERR] Prompt executed in 23.34 seconds
21:11:09.321 [Info] T2API.saveImage start
21:11:09.323 [Info] session.SaveImage start
21:11:09.330 [Info] session.SaveImage done
21:11:09.332 [Info] async Utilities.RunCheckedTask start
21:11:09.333 [Info] T2API.saveImage done
21:11:09.337 [Info] Generated an image in 9.50 sec (prep) and 23.78 sec (gen)
21:11:09.346 [Info] Returning images to client
21:11:09.398 [Info] File.WriteAllBytes start
21:11:09.401 [Info] File.WriteAllBytes done
21:11:09.402 [Debug] Saved an output file as 'C:\Folders\StableDiffusion\SwarmUI\Output\local\raw\2025-12-23\foo.jpg'
21:11:09.403 [Info] API.HandleAsyncRequest - start handling API request

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions