Prevent image save counter collisions

This commit is contained in:
2026-10-15 14:51:43 +00:00
parent 09a340e8f3
commit 64a8e8a439
2 changed files with 92 additions and 26 deletions
+36 -25
View File
@@ -3,6 +3,7 @@
import os
import json
import re
import threading
import time
import uuid
from collections import OrderedDict
@@ -15,6 +16,7 @@ import folder_paths
_IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif", ".tif", ".tiff"}
_SERVE_TOKENS = OrderedDict()
_SAVE_COUNTER_LOCK = threading.Lock()
def resolve_serve_token(token):
@@ -32,7 +34,10 @@ def _list_folder_image_files(real_folder, recursive=False):
def _next_counter_for_relative_path(base_directory, relative_template):
template = str(relative_template).replace("%counter%", "*")
relative_template = str(relative_template).replace("\\", "/")
if "%counter%" not in relative_template:
return 1
template = relative_template.replace("%counter%", "*")
parent = Path(base_directory) / Path(template).parent
stem = Path(template).name
if not parent.exists():
@@ -40,6 +45,8 @@ def _next_counter_for_relative_path(base_directory, relative_template):
pattern = re.compile(re.escape(stem).replace(r"\*", r"(\d+)") + r"(?:\.[^.]+)?$")
highest = 0
for path in parent.iterdir():
if not path.is_file():
continue
match = pattern.match(path.name)
if match:
highest = max(highest, int(match.group(1)))
@@ -130,30 +137,34 @@ class DumasSaveImageNode:
base.mkdir(parents=True, exist_ok=True)
ui_images = []
extension = ".jpg" if format == "jpg" else f".{format}"
for index, image in enumerate(images):
width, height = int(image.shape[2]), int(image.shape[1])
stem = str(pattern or "image_%counter%")
stem = stem.replace("%input%", _safe_token(name)).replace("%input2%", _safe_token(name_2))
stem = stem.replace("%width%", str(width)).replace("%height%", str(height)).replace("%batch_num%", str(index))
relative = stem.replace("\\", "/") + extension
counter = _next_counter_for_relative_path(str(base), relative)
relative = relative.replace("%counter%", str(counter).zfill(3))
path = base / Path(relative)
path.parent.mkdir(parents=True, exist_ok=True)
pil = _image_to_pil(image)
if format == "jpg":
pil.convert("RGB").save(path, "JPEG", quality=max(1, min(100, int(quality))))
elif format == "webp":
pil.save(path, "WEBP", quality=max(1, min(100, int(quality))))
else:
pil.save(path, "PNG", pnginfo=_build_pnginfo(prompt, extra_pnginfo) if embed_workflow else None)
if _is_within_directory(str(output_dir), str(path)):
subfolder = str(path.parent.relative_to(output_dir)).replace(os.sep, "/")
ui_images.append({"filename": path.name, "subfolder": "" if subfolder == "." else subfolder, "type": "output"})
else:
token = uuid.uuid4().hex
_SERVE_TOKENS[token] = str(path)
ui_images.append({"filename": path.name, "subfolder": str(path.parent).replace(os.sep, "/"), "type": "external", "token": token})
# Counter selection and the corresponding writes must be one critical
# section. Without this, overlapping ComfyUI executions can both see
# the same highest existing counter and overwrite one another.
with _SAVE_COUNTER_LOCK:
for index, image in enumerate(images):
width, height = int(image.shape[2]), int(image.shape[1])
stem = str(pattern or "image_%counter%")
stem = stem.replace("%input%", _safe_token(name)).replace("%input2%", _safe_token(name_2))
stem = stem.replace("%width%", str(width)).replace("%height%", str(height)).replace("%batch_num%", str(index))
relative = stem.replace("\\", "/") + extension
counter = _next_counter_for_relative_path(str(base), relative)
relative = relative.replace("%counter%", str(counter).zfill(3))
path = base / Path(relative)
path.parent.mkdir(parents=True, exist_ok=True)
pil = _image_to_pil(image)
if format == "jpg":
pil.convert("RGB").save(path, "JPEG", quality=max(1, min(100, int(quality))))
elif format == "webp":
pil.save(path, "WEBP", quality=max(1, min(100, int(quality))))
else:
pil.save(path, "PNG", pnginfo=_build_pnginfo(prompt, extra_pnginfo) if embed_workflow else None)
if _is_within_directory(str(output_dir), str(path)):
subfolder = str(path.parent.relative_to(output_dir)).replace(os.sep, "/")
ui_images.append({"filename": path.name, "subfolder": "" if subfolder == "." else subfolder, "type": "output"})
else:
token = uuid.uuid4().hex
_SERVE_TOKENS[token] = str(path)
ui_images.append({"filename": path.name, "subfolder": str(path.parent).replace(os.sep, "/"), "type": "external", "token": token})
return {"ui": {"images": ui_images}}