Skip to content

Official Codebase of 3DCodeVerse

Python

render: thread-safe cache store, GPU mode in the object cache key, one CV3D_RENDER_GPU

gaoypengc7214271a498
diff --git a/harness/codeverse/config.py b/harness/codeverse/config.py
index 88b88409..8a19df5e 100644
--- a/harness/codeverse/config.py
+++ b/harness/codeverse/config.py
@@ -225,6 +225,9 @@ class Settings(BaseSettings):
#: knob; both spellings now work and the doctor prints the short one.
_FLAT_ALIASES: ClassVar[dict[str, tuple[str, str]]] = {
"CV3D_MAX_IN_FLIGHT": ("rate", "max_in_flight"),
+ # the spelling every doc and the node side (gpu_launch.cjs) use — pydantic-settings
+ # only read CV3D_RENDER__GPU, so the documented knob was dead on 2 of 3 tracks
+ "CV3D_RENDER_GPU": ("render", "gpu"),
FEWER_TURNS_ENV: ("limits", "fewer_turns"),
SEED_RECIPES_ENV: ("limits", "seed_recipes"),
}
@@ -236,9 +239,14 @@ class Settings(BaseSettings):
if raw is None or raw.strip() == "":
continue
sub = getattr(self, section)
- value: int | bool
- if type(sub).model_fields[field].annotation is bool:
+ value: int | bool | str
+ ann = type(sub).model_fields[field].annotation
+ if ann is bool:
value = _env_flag(raw, env)
+ elif ann is str: # render.gpu is the only str alias: validate its enum here
+ value = raw.strip().lower()
+ if value not in ("auto", "on", "off"):
+ raise ValueError(f"{env}={raw!r}: expected auto|on|off")
else:
try:
value = int(raw)
diff --git a/harness/codeverse/spatial/render.py b/harness/codeverse/spatial/render.py
index c0d8a6fe..7c8a504b 100644
--- a/harness/codeverse/spatial/render.py
+++ b/harness/codeverse/spatial/render.py
@@ -8,10 +8,12 @@ requested ``out_dir`` so every call still yields a self-contained directory.
from __future__ import annotations
+import contextlib
import hashlib
import json
import os
import shutil
+import threading
import time
from collections.abc import Sequence
from pathlib import Path
@@ -113,6 +115,10 @@ def render_glb(
"background": background,
"anim_time": anim_time,
"shadow": bool(shadow),
+ # requested mode, not the resolved backend: 'auto' fragments from 'on'/'off',
+ # which is accepted over a hit serving the other backend's pixels (and lying
+ # about RenderSet.renderer) when someone flips CV3D_RENDER_GPU between runs
+ "gpu": gpu,
"rig": _rig_signature(),
}
t0 = time.time()
@@ -178,17 +184,20 @@ def _run_render(glb: Path, out_dir: Path, params: dict[str, Any], *, gpu: str, t
def _store_in_cache(cache_dir: Path, out_dir: Path, record: dict[str, Any]) -> None:
- tmp = cache_dir.parent / f".{cache_dir.name}.{os.getpid()}.tmp"
- if tmp.exists():
- shutil.rmtree(tmp)
- tmp.mkdir(parents=True)
- for v in record["views"]:
- shutil.copy2(out_dir / f"view_{v['name']}.png", tmp / f"view_{v['name']}.png")
- (tmp / "views.json").write_text(json.dumps(record, indent=1))
- if cache_dir.exists(): # another process won the race; keep theirs
- shutil.rmtree(tmp)
- return
- tmp.replace(cache_dir)
+ # per-writer tmp + tolerant rename (the vlm_judge._render_slices shape): a pid-only
+ # tmp name let two threads of one judge fan-out share a dir — the loser's rmtree
+ # deleted the winner's half-copied PNGs AFTER a successful render
+ tmp = cache_dir.parent / f".{cache_dir.name}.{os.getpid()}-{threading.get_ident()}.tmp"
+ shutil.rmtree(tmp, ignore_errors=True)
+ try:
+ tmp.mkdir(parents=True)
+ for v in record["views"]:
+ shutil.copy2(out_dir / f"view_{v['name']}.png", tmp / f"view_{v['name']}.png")
+ (tmp / "views.json").write_text(json.dumps(record, indent=1))
+ with contextlib.suppress(OSError): # a concurrent identical writer won the race; keep theirs
+ tmp.replace(cache_dir)
+ finally: # a mid-copy crash (or losing the race) must not leave the .tmp behind
+ shutil.rmtree(tmp, ignore_errors=True)
def _restore_from_cache(cache_dir: Path, out_dir: Path) -> dict[str, Any] | None:
diff --git a/harness/tests/core/test_config_env_aliases.py b/harness/tests/core/test_config_env_aliases.py
index 9ff481d5..4f82b532 100644
--- a/harness/tests/core/test_config_env_aliases.py
+++ b/harness/tests/core/test_config_env_aliases.py
@@ -27,6 +27,23 @@ def test_empty_alias_is_ignored_and_garbage_is_loud(monkeypatch):
Settings()
+def test_render_gpu_is_read_by_both_spellings_and_garbage_is_loud(monkeypatch):
+ """CV3D_RENDER_GPU is the spelling every doc and the node side use — it used to be
+ read by NOTHING in Settings (only CV3D_RENDER__GPU was), so the documented knob
+ was dead on 2 of 3 tracks."""
+ monkeypatch.delenv("CV3D_RENDER__GPU", raising=False)
+ monkeypatch.setenv("CV3D_RENDER_GPU", "off")
+ assert Settings().render.gpu == "off"
+ monkeypatch.setenv("CV3D_RENDER_GPU", "ON") # case-tolerant
+ assert Settings().render.gpu == "on"
+ monkeypatch.delenv("CV3D_RENDER_GPU", raising=False)
+ monkeypatch.setenv("CV3D_RENDER__GPU", "off")
+ assert Settings().render.gpu == "off"
+ monkeypatch.setenv("CV3D_RENDER_GPU", "maybe")
+ with pytest.raises(ValueError, match="CV3D_RENDER_GPU"):
+ Settings()
+
+
def test_a_negative_cap_is_rejected_by_both_spellings(monkeypatch):
"""Both spellings reject negative caps; zero remains the unlimited sentinel."""
monkeypatch.delenv("CV3D_RATE__MAX_IN_FLIGHT", raising=False)
diff --git a/harness/tests/spatial_tools/test_render_common.py b/harness/tests/spatial_tools/test_render_common.py
index 3efe1a4c..f0125cb9 100644
--- a/harness/tests/spatial_tools/test_render_common.py
+++ b/harness/tests/spatial_tools/test_render_common.py
@@ -151,6 +151,77 @@ def test_cached_render_glb_leaves_the_cache_to_render_glb(stool_ctx: ToolContext
assert not list(calls[0].glob("renderset.json")) # no second marker on disk
+def test_store_in_cache_survives_a_concurrent_identical_writer(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ """The pid-only tmp name let two threads of one judge fan-out share a tmp dir: the
+ loser's cleanup deleted the winner's half-copied PNGs AFTER a successful render
+ (FileNotFoundError out of a call whose render had succeeded). Per-writer names +
+ tolerant rename: no exception, and the cache ends up complete."""
+ import shutil
+ import threading
+ import time
+
+ from codeverse.spatial import render as R
+
+ out = tmp_path / "out"
+ out.mkdir()
+ record = {"views": [{"name": f"v{i}"} for i in range(6)]}
+ for i in range(6):
+ (out / f"view_v{i}.png").write_bytes(b"x" * 1000)
+ cache_dir = tmp_path / "cache" / "deadbeefdeadbeefdeadbeef"
+ cache_dir.parent.mkdir(parents=True)
+
+ orig = shutil.copy2
+ monkeypatch.setattr(R.shutil, "copy2", lambda src, dst, **kw: (time.sleep(0.03), orig(src, dst, **kw))[1])
+
+ errs: list[str] = []
+
+ def store(delay: float) -> None:
+ time.sleep(delay)
+ try:
+ R._store_in_cache(cache_dir, out, record)
+ except Exception as e: # noqa: BLE001 — the failure mode under test
+ errs.append(f"{type(e).__name__}: {e}")
+
+ threads = [threading.Thread(target=store, args=(d,)) for d in (0.0, 0.08)]
+ for t in threads:
+ t.start()
+ for t in threads:
+ t.join()
+ assert errs == []
+ assert (cache_dir / "views.json").is_file()
+ assert sorted(p.name for p in cache_dir.glob("view_*.png")) == [f"view_v{i}.png" for i in range(6)]
+ assert not list(cache_dir.parent.glob("*.tmp")), "no tmp dirs left behind"
+
+
+def test_object_render_cache_is_keyed_by_gpu_mode(stool_glb: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ """gpu=on and gpu=off used to share one cache key, so a hit served the OTHER
+ backend's pixels and misreported RenderSet.renderer. The requested mode joins
+ the key ('auto' fragments from 'on'/'off' by design — owner default)."""
+ from types import SimpleNamespace
+
+ from codeverse.spatial import render as R
+
+ runs: list[tuple[str, Path]] = []
+
+ def fake_run_render(glb, out_dir, params, *, gpu, timeout_s):
+ runs.append((gpu, Path(out_dir)))
+ (Path(out_dir) / "view_front.png").write_bytes(b"png-" + gpu.encode())
+ return {"views": [{"name": "front"}], "renderer": f"webgl-{gpu}"}
+
+ fake_settings = SimpleNamespace(cache_dir=tmp_path / "cache",
+ render=SimpleNamespace(gpu="auto"),
+ limits=SimpleNamespace(render_timeout_s=5))
+ monkeypatch.setattr(R, "get_settings", lambda: fake_settings)
+ monkeypatch.setattr(R, "_run_render", fake_run_render)
+
+ a = R.render_glb(stool_glb, tmp_path / "a", views=[OBJECT_VIEWS[0]], sheet=False, gpu="on")
+ b = R.render_glb(stool_glb, tmp_path / "b", views=[OBJECT_VIEWS[0]], sheet=False, gpu="off")
+ assert len(runs) == 2, "gpu=off must not be served gpu=on's cached pixels"
+ assert a.renderer == "webgl-on" and b.renderer == "webgl-off"
+ c = R.render_glb(stool_glb, tmp_path / "c", views=[OBJECT_VIEWS[0]], sheet=False, gpu="on")
+ assert len(runs) == 2 and c.renderer == "webgl-on", "same mode still hits the cache"
+
+
def test_tool_unavailable_is_reported_by_the_registry(stool_ctx: ToolContext, monkeypatch: pytest.MonkeyPatch) -> None:
"""No tool catches ToolUnavailable itself any more — ToolDef.call does it for
all of them, with the tool's own registered name."""