From 64a8e8a4390af4c2e7712b1d2d15eab1f8abbf8c Mon Sep 17 00:00:00 2001 From: Chris Dumas Date: Thu, 15 Oct 2026 14:51:43 +0000 Subject: [PATCH] Prevent image save counter collisions --- dumas_image_nodes.py | 61 +++++++++++++++++++-------------- tests/test_dumas_image_nodes.py | 57 +++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 26 deletions(-) diff --git a/dumas_image_nodes.py b/dumas_image_nodes.py index 2e74e1c..f66861d 100644 --- a/dumas_image_nodes.py +++ b/dumas_image_nodes.py @@ -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}} diff --git a/tests/test_dumas_image_nodes.py b/tests/test_dumas_image_nodes.py index 2c1a734..6a65fd4 100644 --- a/tests/test_dumas_image_nodes.py +++ b/tests/test_dumas_image_nodes.py @@ -11,6 +11,8 @@ class FakeImage: def save(self, path, *args, **kwargs): self.saved_paths.append((path, args, kwargs)) + os.makedirs(os.path.dirname(path), exist_ok=True) + open(path, "wb").close() def convert(self, _mode): return self @@ -19,8 +21,23 @@ class FakeImage: class FakeBatch: shape = (1, 6, 8, 3) + def cpu(self): + return self + + def numpy(self): + return self + + def clip(self, _low, _high): + return self + + def __mul__(self, _value): + return self + + def astype(self, _dtype): + return self + def __getitem__(self, _index): - return types.SimpleNamespace(cpu=lambda: self, numpy=lambda: self, astype=lambda _dtype: self) + return self class DumasImageNodeTests(unittest.TestCase): @@ -67,6 +84,44 @@ class DumasImageNodeTests(unittest.TestCase): ("image", "mask", "width", "height", "filename", "index", "total"), ) + def test_counter_uses_highest_existing_file(self): + with tempfile.TemporaryDirectory() as directory: + for name in ("image_001.png", "image_004.png", "image_002.png.jpg"): + open(os.path.join(directory, name), "wb").close() + + self.assertEqual( + self.nodes._next_counter_for_relative_path(directory, "image_%counter%.png"), + 5, + ) + + def test_save_batch_assigns_distinct_counter_names(self): + with tempfile.TemporaryDirectory() as directory: + FakeImage.saved_paths.clear() + node = self.nodes.DumasSaveImageNode() + node.save_images( + [FakeBatch(), FakeBatch()], + directory, + "batch_%counter%", + "png", + 95, + False, + True, + ) + node.save_images( + [FakeBatch(), FakeBatch()], + directory, + "batch_%counter%", + "png", + 95, + False, + True, + ) + + self.assertEqual( + [os.path.basename(path) for path, _args, _kwargs in FakeImage.saved_paths[-2:]], + ["batch_003.png", "batch_004.png"], + ) + if __name__ == "__main__": unittest.main()