Skip to content

Official Codebase of 3DCodeVerse

Python

runlock: --force must not evict a run someone is using

gaoypengb268836ad6cd
diff --git a/harness/codeverse/cli/_common.py b/harness/codeverse/cli/_common.py
index adde8ae3..ff02f38a 100644
--- a/harness/codeverse/cli/_common.py
+++ b/harness/codeverse/cli/_common.py
@@ -88,10 +88,18 @@ def make_slug(prompt: str, track: str, language: str, explicit: str | None = Non
def create_workspace(root: Path, *, force: bool) -> Workspace:
+ from codeverse.runlock import RunLocked, assert_free
+
ws = Workspace(root)
if ws.root.exists() and any(ws.root.iterdir()):
if not force:
raise CliError(f"{ws.root} already exists; use --force to overwrite or --slug for a new name")
+ # --force overwrites a DEAD run, never a live one: the rmtree below would delete
+ # that process's lock file along with its workspace (codeverse/runlock.py)
+ try:
+ assert_free(ws.root)
+ except RunLocked as e:
+ raise CliError(str(e), code=2) from None
shutil.rmtree(ws.root)
ws.create()
return ws
diff --git a/harness/codeverse/runlock.py b/harness/codeverse/runlock.py
index bc25b56a..d8788124 100644
--- a/harness/codeverse/runlock.py
+++ b/harness/codeverse/runlock.py
@@ -69,6 +69,38 @@ def _holder(path: Path) -> dict | None:
return rec
+def assert_free(run_root: Path | str, *, action: str = "overwrite") -> None:
+ """Raise :class:`RunLocked` if a live process holds this run.
+
+ Call this BEFORE destroying or recreating a run directory. ``--force`` must mean
+ "overwrite a DEAD run", never "evict a running one": ``create_workspace(force=True)``
+ does ``shutil.rmtree`` on the run root, which deletes the lock file itself, so without
+ this check a forced run silently wipes a live holder's workspace out from under it and
+ then takes a fresh lock with no error. Measured 2026-08-25 on
+ tsr_scn_boat_workshop_v2: two independent ``3dcv make`` on one slug, 33 seconds apart.
+
+ This narrows the window rather than closing it completely — two ``--force`` calls that
+ interleave between this check and the wipe can still both proceed. Closing that
+ properly needs a lock held outside the directory being deleted; the check here removes
+ the case that actually happens, which is a force against a run someone is already
+ using.
+ """
+ if (held := _holder(_lock_path(run_root))) is not None:
+ raise RunLocked(_held_message(run_root, held, action=action))
+
+
+def _held_message(run_root: Path | str, held: dict, *, action: str) -> str:
+ started = time.strftime("%H:%M:%S", time.localtime(held.get("started", 0)))
+ name = Path(run_root).name
+ return (
+ f"refusing to {action} run {name}: it is being run right now by pid {held['pid']} "
+ f"(started {started}: {held.get('what') or '3dcv'}). Two processes on one run "
+ f"corrupt each other's state. Look at it with `3dcv status {name}`, or stop that "
+ f"ONE process with `kill {held['pid']}` — never `pkill -f 3dcv`, which kills every "
+ f"other run on this machine too."
+ )
+
+
@contextmanager
def run_lock(run_root: Path | str, *, what: str = "") -> Iterator[None]:
"""Hold the run directory for this process, or raise :class:`RunLocked`.
@@ -79,14 +111,7 @@ def run_lock(run_root: Path | str, *, what: str = "") -> Iterator[None]:
path = _lock_path(run_root)
path.parent.mkdir(parents=True, exist_ok=True)
if (held := _holder(path)) is not None:
- started = time.strftime("%H:%M:%S", time.localtime(held.get("started", 0)))
- raise RunLocked(
- f"run {Path(run_root).name} is already being run by pid {held['pid']} "
- f"(started {started}: {held.get('what') or '3dcv'}). Two processes on one run "
- f"corrupt each other's state. Look at it with `3dcv status {Path(run_root).name}`, "
- f"or stop that ONE process with `kill {held['pid']}` — never `pkill -f 3dcv`, "
- f"which kills every other run on this machine too."
- )
+ raise RunLocked(_held_message(run_root, held, action="enter"))
rec = {"pid": os.getpid(), "started": time.time(), "what": what}
tmp = path.with_suffix(".lock.tmp")
tmp.write_text(json.dumps(rec))
diff --git a/harness/tests/core/test_runlock.py b/harness/tests/core/test_runlock.py
index 11bd15d2..fe2aa7a7 100644
--- a/harness/tests/core/test_runlock.py
+++ b/harness/tests/core/test_runlock.py
@@ -60,3 +60,40 @@ def test_an_unreadable_lock_never_bricks_a_run(tmp_path, junk):
p.write_text(junk)
with run_lock(tmp_path):
pass
+
+
+# --------------------------------------------------------- --force must not evict a live run
+def test_force_refuses_to_wipe_a_run_a_live_process_is_holding(tmp_path):
+ """`--force` means "overwrite a DEAD run", never "evict a running one".
+
+ create_workspace(force=True) does shutil.rmtree on the run root, which deletes the
+ lock file itself — so before this check a forced run wiped a live holder's workspace
+ out from under it and then took a fresh lock, with no error and no message.
+ Reproduced 2026-08-25 on tsr_scn_boat_workshop_v2: two independent `3dcv make` on one
+ slug, 33 seconds apart, both writing.
+ """
+ from codeverse.cli._common import CliError, create_workspace
+
+ root = tmp_path / "held_run"
+ root.mkdir()
+ (root / "spec.json").write_text("{}") # non-empty, so --force would rmtree it
+ with run_lock(root, what="3dcv make held_run"):
+ with pytest.raises(CliError) as ei:
+ create_workspace(root, force=True)
+ assert str(os.getpid()) in str(ei.value)
+ assert (root / "spec.json").exists(), "the live holder's workspace must survive"
+
+
+def test_force_still_overwrites_a_dead_run(tmp_path):
+ """The check must not make a crashed run un-forceable — that would be worse than the
+ bug it fixes, because 13 runs needed exactly this after the pkill incident."""
+ import json
+
+ from codeverse.cli._common import create_workspace
+
+ root = tmp_path / "dead_run"
+ (root / ".3dcv").mkdir(parents=True)
+ (root / "spec.json").write_text("{}")
+ _lock_path(root).write_text(json.dumps({"pid": 4194303, "started": 0, "what": "3dcv make ghost"}))
+ create_workspace(root, force=True) # must not raise
+ assert not (root / "spec.json").exists(), "a dead run's directory is replaced as before"