feat: age-based GC for temp images

save_tensor_png now reaps img_*.png files older than COMFY_BRIDGE_TMP_TTL
(env, default 3600s) after each write, so .comfy_bridge_tmp/ no longer
grows forever. Robust to races (ignores FileNotFoundError/OSError) and
never deletes the file just written.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ethanfel
2026-06-28 13:59:19 +02:00
co-authored by Claude Opus 4.8
parent 4c0e9d9b07
commit aea5ffdd5c
2 changed files with 57 additions and 0 deletions
+18
View File
@@ -1,10 +1,27 @@
import glob
import os
import time
import uuid
import numpy as np
import torch
from PIL import Image
def _reap_old(out_dir: str, keep: str) -> None:
"""Delete img_*.png in out_dir older than COMFY_BRIDGE_TMP_TTL (default
3600s) so the temp dir doesn't grow forever. Never touches `keep`."""
ttl = float(os.environ.get("COMFY_BRIDGE_TMP_TTL", "3600"))
cutoff = time.time() - ttl
for f in glob.glob(os.path.join(out_dir, "img_*.png")):
if f == keep:
continue
try:
if os.path.getmtime(f) < cutoff:
os.unlink(f)
except (FileNotFoundError, OSError):
pass # races / permission churn -> best-effort
def save_tensor_png(tensor: torch.Tensor, out_dir: str) -> str:
"""ComfyUI IMAGE [B,H,W,C] float 0..1 -> PNG file, returns path."""
os.makedirs(out_dir, exist_ok=True)
@@ -14,6 +31,7 @@ def save_tensor_png(tensor: torch.Tensor, out_dir: str) -> str:
img = Image.fromarray(arr, mode="RGB")
path = os.path.join(out_dir, f"img_{uuid.uuid4().hex}.png")
img.save(path)
_reap_old(out_dir, keep=path)
return path