summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-24 12:12:46 +0300
committerPaul Buetow <paul@buetow.org>2026-05-24 12:12:46 +0300
commit223b422fbca462c07d3c3771e81dd2100d8e3a60 (patch)
tree6cbd2876eee7160937c1c45513bfe45c2ce4a4a1
parent6beadd91c11fee7b23017fac07a932f7ed8b21da (diff)
fix(photo-enhance): ensure .orient tempfiles are always cleaned up in enhance_one
Wrap enhance_one body in begin/ensure to unconditionally delete upload_path (and tmp_png) on every exit path, not just success. Prevents 50+ MB RAW->TIFF leaks when upload_image/submit_prompt/wait_for_output/save_with_corrections raises or when ComfyUI connection errors are caught.
-rwxr-xr-xphoto-enhance.rb85
1 files changed, 45 insertions, 40 deletions
diff --git a/photo-enhance.rb b/photo-enhance.rb
index 0b48ceb..2d6b8de 100755
--- a/photo-enhance.rb
+++ b/photo-enhance.rb
@@ -306,49 +306,54 @@ class PhotoEnhancer
# Bake in EXIF rotation before uploading — ComfyUI strips EXIF metadata.
upload_path = auto_orient_tempfile(src_path)
- retried = false
begin
- uploaded_name = @client.upload_image(upload_path)
- workflow = inject_input(@workflow, uploaded_name)
- prompt_id = @client.submit_prompt(workflow)
- @out.puts " prompt #{prompt_id}"
-
- filenames = @client.wait_for_output(prompt_id)
- raise "No outputs returned for #{src_path}" if filenames.empty?
- rescue RuntimeError => e
- # On connection refused (ComfyUI crashed / OOM), wait for systemd to restart
- # it and retry this photo once. Any other error propagates immediately.
- if !retried && e.message.include?('Cannot reach ComfyUI')
- retried = true
- @client.wait_for_recovery
- retry
+ retried = false
+ begin
+ uploaded_name = @client.upload_image(upload_path)
+ workflow = inject_input(@workflow, uploaded_name)
+ prompt_id = @client.submit_prompt(workflow)
+ @out.puts " prompt #{prompt_id}"
+
+ filenames = @client.wait_for_output(prompt_id)
+ raise "No outputs returned for #{src_path}" if filenames.empty?
+ rescue RuntimeError => e
+ # On connection refused (ComfyUI crashed / OOM), wait for systemd to restart
+ # it and retry this photo once. Any other error propagates immediately.
+ if !retried && e.message.include?('Cannot reach ComfyUI')
+ retried = true
+ @client.wait_for_recovery
+ retry
+ end
+ raise
end
- raise
- end
- # ComfyUI outputs PNG; download then convert to output format.
- tmp_png = "#{dest_path}.tmp.png"
- @client.download_output(filenames.first, tmp_png)
- save_with_corrections(tmp_png, dest_path, out_ext)
- File.delete(tmp_png) if File.exist?(tmp_png)
- File.delete(upload_path) if upload_path != src_path && File.exist?(upload_path)
-
- # Restore original EXIF metadata onto the enhanced JPEG.
- # ComfyUI strips all EXIF when it processes the image; this brings back
- # capture time, camera/lens info, ICC profile, and GPS coordinates.
- copy_exif(src_path, dest_path)
-
- # Download the JSON metadata written by WritePhotoMetadata and render it
- # as a human-readable .md report alongside the enhanced photo.
- # ComfyUI appends _NNNNN_ counter: "enhanced_abc123__00001_.png" → "enhanced_abc123_"
- prefix = filenames.first.sub(/_\d+_\.png$/, '')
- meta_file = "#{prefix}meta.json"
- md_path = File.join(File.dirname(dest_path),
- "#{File.basename(dest_path, File.extname(dest_path))}.md")
- download_and_write_md(meta_file, src_path, dest_path, md_path, prompt_id)
-
- @manifest.mark_done(src_path)
- @out.puts " -> #{dest_path} (#{kb(src_path)} KB -> #{kb(dest_path)} KB)"
+ # ComfyUI outputs PNG; download then convert to output format.
+ tmp_png = "#{dest_path}.tmp.png"
+ @client.download_output(filenames.first, tmp_png)
+ save_with_corrections(tmp_png, dest_path, out_ext)
+
+ # Restore original EXIF metadata onto the enhanced JPEG.
+ # ComfyUI strips all EXIF when it processes the image; this brings back
+ # capture time, camera/lens info, ICC profile, and GPS coordinates.
+ copy_exif(src_path, dest_path)
+
+ # Download the JSON metadata written by WritePhotoMetadata and render it
+ # as a human-readable .md report alongside the enhanced photo.
+ # ComfyUI appends _NNNNN_ counter: "enhanced_abc123__00001_.png" → "enhanced_abc123_"
+ prefix = filenames.first.sub(/_\d+_\.png$/, '')
+ meta_file = "#{prefix}meta.json"
+ md_path = File.join(File.dirname(dest_path),
+ "#{File.basename(dest_path, File.extname(dest_path))}.md")
+ download_and_write_md(meta_file, src_path, dest_path, md_path, prompt_id)
+
+ @manifest.mark_done(src_path)
+ @out.puts " -> #{dest_path} (#{kb(src_path)} KB -> #{kb(dest_path)} KB)"
+ ensure
+ # Always remove the oriented tempfile (and any downloaded PNG temp)
+ # so failures do not leave orphaned files on disk.
+ File.delete(tmp_png) if defined?(tmp_png) && File.exist?(tmp_png)
+ File.delete(upload_path) if upload_path != src_path && File.exist?(upload_path)
+ end
rescue StandardError => e
@out.puts " ERROR #{File.basename(src_path)}: #{e.message}"
end