Add server-side temporary storage controls for 3.9.3

This commit is contained in:
Miura Yoshiki
2026-10-13 12:18:23 +00:00
parent e9b1294ccd
commit d8fbf287a9
34 changed files with 2288 additions and 66 deletions
+1 -1
View File
@@ -85,7 +85,7 @@ class H3DecodeCacheHelper:
def decode(self, cache_mode, ram_budget_mb, disk_budget_gb, reset_token,
video_samples=None, video_vae=None, audio_samples=None, audio_vae=None):
with self._lock:
with self.service.store.operation(), self._lock:
mode = self._scalar(cache_mode, "cache_mode")
ram = int(self._scalar(ram_budget_mb, "ram_budget_mb"))
disk = int(self._scalar(disk_budget_gb, "disk_budget_gb"))
+130 -2
View File
@@ -10,6 +10,7 @@ import tempfile
import threading
import uuid
import weakref
from contextlib import nullcontext
from collections import OrderedDict
from dataclasses import dataclass, field
@@ -18,11 +19,20 @@ import torch
from .identity import CacheBypass, tensor_blocks, tensor_digest
if "." in __package__:
from .. import storage_settings, storage_fs
from ..storage_memory import system_memory_bytes
else: # The existing standalone cache test/probe entry points remain supported.
storage_settings = storage_fs = None
system_memory_bytes = None
MiB = 1024**2
GiB = 1024**3
def available_ram():
if system_memory_bytes is not None:
return system_memory_bytes()[0] or 0
try:
import psutil # already provided by ComfyUI; optional for cache operation
return int(psutil.virtual_memory().available)
@@ -50,14 +60,35 @@ class Entry:
def _cleanup_private_files(state):
"""Best effort at object/process exit; never close arrays still owned downstream."""
with state.get("lock", nullcontext()):
_cleanup_private_files_locked(state)
def _cleanup_private_files_locked(state):
for path, entry in list(state["entries"].items()):
if not entry.live():
try:
if path.parent in state.get("identities", {}):
storage_fs.check_identity(path.parent, state["identities"][path.parent])
path.unlink(missing_ok=True)
state["entries"].pop(path, None)
except OSError:
pass
if state["root"] is not None:
for root, owner in state.get("owners", {}).items():
try:
storage_fs.check_identity(root, owner["identity"])
for name in owner["files"]:
path = root / name
if path not in state["entries"] and storage_fs.regular_file(path):
try:
path.unlink()
except OSError:
pass
except OSError:
pass
for root, expected in state.get("identities", {}).items():
storage_settings.remove_empty_cache_root(root, expected)
if state["root"] is not None and state["root"] not in state.get("identities", {}):
try:
state["root"].rmdir()
except OSError:
@@ -78,6 +109,10 @@ class DecodeStore:
self.retired = []
self.root = None
self.temp_parent = temp_parent if temp_parent is not None else (os.environ.get("H3_DECODE_CACHE_TEMP_DIR") or None)
self._explicit_parent = temp_parent is not None
self._default_parent = self.temp_parent
self._owners = {}
self._scratch_token = uuid.uuid4().hex
self.memory_probe = memory_probe
self.disk_probe = disk_probe
self.headroom_bytes = headroom_bytes
@@ -90,9 +125,14 @@ class DecodeStore:
self.max_entries = 256
# Finalizer state has only paths, disk-entry metadata and weak leases;
# no decoded RAM tensor, VAE, store, node, or GPU model is retained.
self._cleanup_state = {"root": None, "entries": {}}
self._cleanup_state = {"root": None, "entries": {}, "identities": {},
"lock": self._lock, "owners": self._owners}
weakref.finalize(self, _cleanup_private_files, self._cleanup_state)
def operation(self):
manager = storage_settings.get_manager() if storage_settings else None
return manager.operation() if manager else nullcontext()
def configure(self, mode, ram_limit, disk_limit, reset_token):
with self._lock:
settings = (mode, int(ram_limit), int(disk_limit), int(reset_token))
@@ -119,6 +159,8 @@ class DecodeStore:
if entry.live():
return False
try:
if entry.path.parent in self._owners:
storage_fs.check_identity(entry.path.parent, self._owners[entry.path.parent]["identity"])
entry.path.unlink(missing_ok=True)
self._cleanup_state["entries"].pop(entry.path, None)
return True
@@ -145,6 +187,8 @@ class DecodeStore:
def close(self):
self.clear()
for root, owner in self._owners.items():
storage_settings.remove_empty_cache_root(root, owner["identity"])
if self.root is not None:
try:
self.root.rmdir() # only an empty helper-owned directory
@@ -152,9 +196,24 @@ class DecodeStore:
pass
def _directory(self):
manager = storage_settings.get_manager() if storage_settings else None
if manager is not None:
manager.register_store(self)
default = self._default_parent or tempfile.gettempdir()
parent = Path(default).resolve() if self._explicit_parent else manager.resolve("decode", default)
if self.root is not None and self.root.parent != parent:
self.root = None
self.temp_parent = parent
if self.root is None:
self.root = Path(tempfile.mkdtemp(prefix=f"h3-decode-cache-{os.getpid()}-", dir=self.temp_parent))
self._cleanup_state["root"] = self.root
if manager is not None:
owner = storage_settings.create_cache_owner(self.root)
self._owners[self.root] = owner
self._cleanup_state["identities"][self.root] = owner["identity"]
manager.record_root("decode", self.root.parent)
if self.root in self._owners:
storage_fs.check_identity(self.root, self._owners[self.root]["identity"])
return self.root
def _ram_room(self, size):
@@ -178,6 +237,8 @@ class DecodeStore:
def _read_disk(self, entry):
path = entry.path
if path is not None and path.parent in self._owners:
storage_fs.check_identity(path.parent, self._owners[path.parent]["identity"])
if path is None or path.is_symlink() or path.stat().st_size != entry.file_bytes:
raise CacheBypass("disk cache file missing/changed")
# Verify the exact serialized payload before exposing a mapping.
@@ -254,6 +315,8 @@ class DecodeStore:
return None
root = self._directory()
filename = uuid.uuid4().hex
if root in self._owners:
storage_settings.register_cache_file(root, self._owners[root], filename)
temp = root / (filename + ".tmp")
final = root / (filename + ".npy")
dtype = {torch.float16: np.dtype("float16"), torch.float32: np.dtype("float32"),
@@ -292,6 +355,71 @@ class DecodeStore:
with self._lock:
return key in self.entries
def inspect_scratch(self, *, cleanup=False, parent):
"""Pure status unless explicitly cleaning; preserve aliases and RAM entries."""
with self._lock:
result = {}
removed = {"removed_files": 0, "removed_bytes": 0, "errors": []}
entries = [(key, entry) for key, entry in self.entries.items()]
entries += [(None, entry) for entry in self.retired]
for key, entry in entries:
path = entry.path
if path is None or path.parent.parent != Path(parent):
continue
try:
if path.parent in self._owners:
storage_fs.check_identity(path.parent, self._owners[path.parent]["identity"])
if not path.is_file() or path.is_symlink():
continue
size = path.stat().st_size
live = any(ref() is not None for ref in entry.leases)
if cleanup and not live and self._delete(entry):
if key is not None:
self.entries.pop(key, None)
else:
self.retired = [item for item in self.retired if item is not entry]
removed["removed_files"] += 1
removed["removed_bytes"] += size
else:
result[str(path)] = "in_use" if live else "deletable"
if cleanup and not live:
removed["errors"].append(f"Could not delete {path.name}; file retained")
except OSError as exc:
result[str(path)] = "unknown"
if cleanup:
removed["errors"].append(str(exc))
# An interrupted unpublished write can leave a registered .tmp.
# The store lock proves no writer is active here; the entry ledger
# continues to protect any published mapping, including retired ones.
for root, owner in self._owners.items():
if root.parent != Path(parent):
continue
try:
storage_fs.check_identity(root, owner["identity"])
for name in owner["files"]:
path = root / name
if path in self._cleanup_state["entries"] or not storage_fs.regular_file(path):
continue
size = path.stat().st_size
if cleanup:
try:
path.unlink()
removed["removed_files"] += 1
removed["removed_bytes"] += size
except OSError as exc:
result[str(path)] = "deletable"
removed["errors"].append(str(exc))
else:
result[str(path)] = "deletable"
except OSError:
pass
if cleanup:
for root, owner in self._owners.items():
if root.parent == Path(parent) and root != self.root:
storage_settings.remove_empty_cache_root(root, owner["identity"])
result[f"__removed_{self._scratch_token}"] = removed
return result
def stats(self):
with self._lock:
self._reap()