Fix container sync races that leaked orphan blobs; add a system-prune CLI command

sync_workspace (user-triggered) and the reconciler's sync_bidirectional_sync
could run concurrently for the same project, and store_upload's read-then-
write on a changed path meant two racing imports each wrote their own blob
while only one ever got referenced - the loser leaked forever. Combined with
no build-artifact exclusion, an actively-compiling workspace hit this
constantly and leaked 5.9M orphan blobs (~96GB) in production before it was
caught.

Closes it at the root: api._sync_dir_bidirectional_locked serializes both
call sites per-project (non-blocking - a project already mid-sync is simply
skipped until the next tick), and IMPORT_SKIP_NAMES/IMPORT_SKIP_EXTENSIONS
keep build output (build/, dist/, *.o, *.pyc, ...) out of the walk entirely.

Recovering what already leaked is a separate concern: a new CLI subcommand
(plus matching make targets) sweeps soft-deleted attachment/project-file
blobs and any blob with zero DB reference at all, plus orphaned container
workspace directories. run_maintenance_cleanup.sh wraps the existing
prune/clear commands for routine disk upkeep.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BWJy6PrMMt5hwWxQwia2rd
This commit is contained in:
2026-09-08 03:43:49 +02:00
co-authored by Claude Sonnet 5
parent 85bd8fad47
commit 7880bf4b31
19 changed files with 1068 additions and 19 deletions
+68
View File
@@ -3,6 +3,7 @@
import asyncio
import ipaddress
import logging
import os
import socket
from datetime import datetime, timezone
from pathlib import Path
@@ -637,6 +638,73 @@ def restore_attachment(uid):
return True
def purge_soft_deleted_attachments(*, dry_run: bool = False) -> tuple[int, int]:
if "attachments" not in db.tables:
return 0, 0
table = get_table("attachments")
rows = list(table.find(table.table.columns.deleted_at.isnot(None)))
removed = 0
freed = 0
for row in rows:
directory = row.get("directory")
stored_name = row.get("stored_name")
if directory and stored_name:
path = ATTACHMENTS_DIR / directory / stored_name
try:
freed += path.stat().st_size
except OSError:
pass
for thumb in (ATTACHMENTS_DIR / directory).glob(
f"{Path(stored_name).stem}_thumb.*"
):
try:
freed += thumb.stat().st_size
except OSError:
pass
if not dry_run:
_unlink_attachment_files(row)
if not dry_run:
table.delete(id=row["id"])
removed += 1
return removed, freed
def sweep_orphan_attachment_blobs(*, dry_run: bool = False) -> tuple[int, int]:
if not ATTACHMENTS_DIR.exists():
return 0, 0
referenced_stems = set()
if "attachments" in db.tables:
for row in get_table("attachments").find():
directory = row.get("directory")
stored_name = row.get("stored_name")
if directory and stored_name:
referenced_stems.add((directory, Path(stored_name).stem))
removed = 0
freed = 0
base = str(ATTACHMENTS_DIR)
for root, _dirs, files in os.walk(base):
directory = os.path.relpath(root, base)
for name in files:
stem = Path(name).stem
if stem.endswith("_thumb"):
stem = stem[: -len("_thumb")]
if (directory, stem) in referenced_stems:
continue
file_path = os.path.join(root, name)
try:
size = os.path.getsize(file_path)
except OSError:
continue
if not dry_run:
try:
os.unlink(file_path)
except OSError:
continue
removed += 1
freed += size
return removed, freed
def soft_delete_target_attachments(target_type, target_uid, deleted_by):
stamp = datetime.now(timezone.utc).isoformat()
for row in get_table("attachments").find(
+2
View File
@@ -45,6 +45,7 @@ from devplacepy.cli.containers import (
)
from devplacepy.cli.quiz import cmd_quiz_prune
from devplacepy.cli.migrate import cmd_emoji_sync, cmd_migrate_data
from devplacepy.cli.system import cmd_system_prune
__all__ = [
"main",
@@ -91,4 +92,5 @@ __all__ = [
"cmd_quiz_prune",
"cmd_emoji_sync",
"cmd_migrate_data",
"cmd_system_prune",
]
+1 -11
View File
@@ -67,19 +67,9 @@ def cmd_containers_prune_builds(args):
def cmd_containers_gc_workspaces(args):
import shutil
from pathlib import Path
from devplacepy import config
from devplacepy.services.containers import store
active = {inst["project_uid"] for inst in store.all_instances()}
base = Path(config.CONTAINER_WORKSPACES_DIR)
removed = 0
if base.is_dir():
for child in base.iterdir():
if child.is_dir() and child.name not in active:
shutil.rmtree(child, ignore_errors=True)
removed += 1
removed = store.gc_workspaces()
_audit_cli("cli.containers.gc_workspaces", f"CLI removed {removed} unused workspace dirs", metadata={"count": removed})
print(
f"Removed {removed} unused workspace director{'y' if removed == 1 else 'ies'}"
+2
View File
@@ -17,6 +17,7 @@ from devplacepy.cli.game import register_game
from devplacepy.cli.quiz import register_quiz
from devplacepy.cli.gateway import register_gateway
from devplacepy.cli.messaging import register_messaging
from devplacepy.cli.system import register_system
def build_parser():
@@ -38,6 +39,7 @@ def build_parser():
register_gateway(sub)
register_messaging(sub)
register_accounts(sub)
register_system(sub)
return parser
+92
View File
@@ -0,0 +1,92 @@
# retoor <retoor@molodetz.nl>
from devplacepy.cli._shared import _audit_cli
def _human(n):
n = float(n)
for unit in ("B", "KB", "MB", "GB", "TB"):
if n < 1024:
return f"{n:.1f}{unit}"
n /= 1024
return f"{n:.1f}PB"
def cmd_system_prune(args):
from devplacepy.attachments import (
purge_soft_deleted_attachments,
sweep_orphan_attachment_blobs,
)
from devplacepy.project_files import (
purge_soft_deleted_project_files,
sweep_orphan_project_file_blobs,
)
from devplacepy.services.containers import store as container_store
dry_run = bool(args.dry_run)
verb = "Would remove" if dry_run else "Removed"
totals = {"items": 0, "freed": 0}
def report(name, count, freed):
totals["items"] += count
totals["freed"] += freed
print(f" {name}: {verb.lower()} {count} item(s), {_human(freed)}")
print("System prune - safe but aggressive disk-space cleanup")
print(
"Removes only content with zero live reference: soft-deleted attachment/"
"\nproject-file blobs, and blob files with no matching database row at all"
"\n(orphans, e.g. left behind by an interrupted or racing sync). Never"
"\ntouches live content."
)
if dry_run:
print("DRY RUN: nothing will be deleted.")
print()
count, freed = purge_soft_deleted_attachments(dry_run=dry_run)
report("soft-deleted attachments", count, freed)
count, freed = purge_soft_deleted_project_files(dry_run=dry_run)
report("soft-deleted project files", count, freed)
count, freed = sweep_orphan_attachment_blobs(dry_run=dry_run)
report("orphan attachment blobs", count, freed)
count, freed = sweep_orphan_project_file_blobs(dry_run=dry_run)
report("orphan project-file blobs", count, freed)
removed = container_store.gc_workspaces(dry_run=dry_run)
totals["items"] += removed
plural = "y" if removed == 1 else "ies"
print(f" orphaned container workspaces: {verb.lower()} {removed} director{plural}")
print()
suffix = " (estimate)" if dry_run else ""
print(f"{verb} {totals['items']} item(s) total, {_human(totals['freed'])} of disk{suffix}")
if not dry_run:
_audit_cli(
"cli.system.prune",
f"CLI system prune removed {totals['items']} item(s), freed {_human(totals['freed'])}",
metadata={"items": totals["items"], "bytes_freed": totals["freed"]},
)
def register_system(subparsers):
system = subparsers.add_parser("system", help="Cross-cutting system maintenance")
system_sub = system.add_subparsers(title="action", dest="action")
prune = system_sub.add_parser(
"prune",
help=(
"Safe but aggressive disk-space cleanup: purges soft-deleted "
"attachment/project-file blobs and any blob with zero database "
"reference at all, plus orphaned container workspace directories"
),
)
prune.add_argument(
"--dry-run",
action="store_true",
help="Report what would be removed without deleting anything",
)
prune.set_defaults(func=cmd_system_prune)
+6 -3
View File
@@ -18,6 +18,7 @@ def paginate(
order=None,
cursor_field="created_at",
viewer_uid=None,
limit=PAGE_SIZE,
**filters,
):
order = order or ["-" + cursor_field]
@@ -30,9 +31,9 @@ def paginate(
clauses.append(table.table.columns.user_uid.notin_(blocked))
if before:
clauses.append(table.table.columns[cursor_field] < before)
rows = list(table.find(*clauses, **filters, order_by=order, _limit=PAGE_SIZE + 1))
has_more = len(rows) > PAGE_SIZE
rows = rows[:PAGE_SIZE]
rows = list(table.find(*clauses, **filters, order_by=order, _limit=limit + 1))
has_more = len(rows) > limit
rows = rows[:limit]
next_cursor = rows[-1][cursor_field] if has_more and rows else None
return rows, next_cursor
@@ -64,6 +65,7 @@ def paginate_diverse(
cursor_field="created_at",
uid_key="user_uid",
viewer_uid=None,
limit=PAGE_SIZE,
**filters,
):
rows, next_cursor = paginate(
@@ -73,6 +75,7 @@ def paginate_diverse(
order=order,
cursor_field=cursor_field,
viewer_uid=viewer_uid,
limit=limit,
**filters,
)
return interleave_by_author(rows, uid_key=uid_key), next_cursor
+90 -1
View File
@@ -1,6 +1,7 @@
# retoor <retoor@molodetz.nl>
import logging
import os
import shutil
from datetime import datetime, timezone
from pathlib import Path
@@ -554,7 +555,7 @@ def _walk_workspace_files(src, skip):
for root, dirs, files in src.walk():
dirs[:] = [d for d in sorted(dirs) if d not in skip]
for name in sorted(files):
if name in skip:
if name in skip or Path(name).suffix in IMPORT_SKIP_EXTENSIONS:
continue
full = Path(root) / name
if full.is_symlink() or not full.is_file():
@@ -780,6 +781,36 @@ IMPORT_SKIP_NAMES = {
".DS_Store",
".idea",
".cache",
"build",
"dist",
"target",
"out",
"bin",
"obj",
".next",
".nuxt",
".gradle",
".tox",
"cmake-build-debug",
"cmake-build-release",
}
# Compiled/build-artifact extensions, regenerated wholesale on every build -
# never worth importing/syncing regardless of which directory they land in
# (unlike IMPORT_SKIP_NAMES, matched by suffix rather than exact name; see
# _walk_workspace_files). A missing exclusion here is what let an unlocked
# concurrent sync (see api._sync_dir_bidirectional_locked) leak millions of
# orphaned blobs from an actively-compiling container workspace.
IMPORT_SKIP_EXTENSIONS = {
".o",
".obj",
".pyc",
".pyo",
".class",
".so",
".dylib",
".dll",
".a",
".exe",
}
SYNC_SKIP_NAMES = IMPORT_SKIP_NAMES | {
".devplace_boot.py",
@@ -998,3 +1029,61 @@ def delete_all_project_files(project_uid: str) -> None:
if row.get("is_binary"):
_unlink_blob(row)
_table().delete(project_uid=project_uid)
def purge_soft_deleted_project_files(*, dry_run: bool = False) -> tuple[int, int]:
if "project_files" not in db.tables:
return 0, 0
table = _table()
rows = list(table.find(table.table.columns.deleted_at.isnot(None), is_binary=1))
removed = 0
freed = 0
for row in rows:
directory = row.get("directory")
stored_name = row.get("stored_name")
if directory and stored_name:
path = PROJECT_FILES_DIR / directory / stored_name
try:
freed += path.stat().st_size
except OSError:
pass
else:
if not dry_run:
_unlink_blob(row)
if not dry_run:
table.delete(id=row["id"])
removed += 1
return removed, freed
def sweep_orphan_project_file_blobs(*, dry_run: bool = False) -> tuple[int, int]:
if not PROJECT_FILES_DIR.exists():
return 0, 0
referenced = set()
if "project_files" in db.tables:
for row in _table().find(is_binary=1):
directory = row.get("directory")
stored_name = row.get("stored_name")
if directory and stored_name:
referenced.add((directory, stored_name))
removed = 0
freed = 0
base = str(PROJECT_FILES_DIR)
for root, _dirs, files in os.walk(base):
directory = os.path.relpath(root, base)
for name in files:
if (directory, name) in referenced:
continue
file_path = os.path.join(root, name)
try:
size = os.path.getsize(file_path)
except OSError:
continue
if not dry_run:
try:
os.unlink(file_path)
except OSError:
continue
removed += 1
freed += size
return removed, freed
+40
View File
@@ -408,6 +408,46 @@ gated on `time.monotonic()` independent of the 5s reconcile tick). The per-insta
boot-helper files (`.devplace_boot.py`/`.devplace_boot.sh`) are in `SYNC_SKIP_NAMES` so they
never round-trip into the project, and therefore never enter the manifest either.
**Both call sites are unsynchronized, and that used to leak millions of orphan blobs
(load-bearing, incident-derived).** `sync_workspace` (user-triggered) and
`sync_bidirectional_sync` (the reconciler tick) can run for the SAME project concurrently
whenever a sync outlasts the reconcile interval - trivial for any workspace that is
actively compiling. `project_files.store_upload` (the import path for a changed file)
writes a NEW blob under a fresh uuid, then updates the existing row to point at it; two
racing imports of the same changed path each write their own blob and both update the
same row (last writer wins), so the loser's freshly-written blob is referenced by nothing
- an orphan on every race, not just on a crash. Combined with no build-artifact exclusion,
an actively-compiling workspace regenerates thousands of files with fresh mtimes on every
build, which is exactly the high-churn pattern that maximizes how often the race fires;
sustained over time this leaked **5.9 million orphan blobs (~96GB)** in production, discovered
only when the host disk hit 100% full. Both are now closed at their root:
- **`api._sync_dir_bidirectional_locked`** wraps `project_files.sync_dir_bidirectional`
behind a per-`project_uid` `threading.Lock` (`api._sync_lock_for`, a lazily-created
registry - both `sync_workspace` and `sync_bidirectional_sync` call it instead of the
raw function). The acquire is **non-blocking**: a second sync for a project already mid-sync
is skipped outright (returns the zero-counts dict), never queued or awaited. This runs on
an `asyncio.to_thread` worker in both call sites, so blocking would tie up a thread pool
slot for no reason - a skipped project is simply picked up on the next tick or the user's
next explicit sync.
- **`project_files.IMPORT_SKIP_NAMES`** gained common build-output directory names (`build`,
`dist`, `target`, `out`, `bin`, `obj`, `.next`, `.nuxt`, `.gradle`, `.tox`,
`cmake-build-debug`, `cmake-build-release`) and a NEW parallel set,
**`IMPORT_SKIP_EXTENSIONS`** (`.o`, `.obj`, `.pyc`, `.pyo`, `.class`, `.so`, `.dylib`,
`.dll`, `.a`, `.exe`), matched by suffix in `_walk_workspace_files` rather than exact
name (`IMPORT_SKIP_NAMES`/`SYNC_SKIP_NAMES` are exact-name-only sets, which cannot express
"any file ending in `.o`"). Both apply to every `_walk_workspace_files` caller (`import_from_dir`
AND the sync path via `SYNC_SKIP_NAMES = IMPORT_SKIP_NAMES | {...}`), so compiled/build
artifacts are never imported into a project regardless of entry point, not just never synced.
Neither fix requires (or should ever regress into) a redesign of the manifest/reconciliation
model above - the lock only prevents two runs from touching the same project's `project_files`
rows at once, and the skip sets only shrink what `_walk_workspace_files` yields. Recovering
already-leaked blobs is a separate, data-layer concern: see `devplace system prune`
(`cli/system.py`) and `attachments.sweep_orphan_attachment_blobs`/
`project_files.sweep_orphan_project_file_blobs`, which sweep any blob with zero database
reference at all, regardless of what leaked it.
## Run-as user = identity + API key ONLY (load-bearing constraint)
An instance's `run_as_uid` column selects WHICH DevPlace user's identity and `api_key` are injected (`DEVPLACE_API_KEY`, `DEVPLACE_USER_UID`), resolved in `api.pravda_env` ahead of the `created_by`/`owner_uid` fallback chain. It does **NOT** change the container OS user, which is ALWAYS `pravda` (uid 1000) - required for the bind-mounted `/app` (DooD uid maps 1:1 to host). Validate it against an existing user via `api.validate_run_as`.
+40 -2
View File
@@ -5,6 +5,7 @@ import json
import re
import secrets
import socket
import threading
from pathlib import Path
from devplacepy import config, project_files, stealth
@@ -611,12 +612,49 @@ def run_spec_for(instance: dict, image_tag: str) -> RunSpec:
)
_SYNC_LOCKS: dict[str, threading.Lock] = {}
_SYNC_LOCKS_GUARD = threading.Lock()
_SYNC_EMPTY_COUNTS = {
"exported": 0,
"imported": 0,
"deleted_in_project": 0,
"deleted_in_workspace": 0,
}
def _sync_lock_for(project_uid: str) -> threading.Lock:
with _SYNC_LOCKS_GUARD:
lock = _SYNC_LOCKS.get(project_uid)
if lock is None:
lock = threading.Lock()
_SYNC_LOCKS[project_uid] = lock
return lock
def _sync_dir_bidirectional_locked(project_uid: str, workspace, user: dict) -> dict:
# The user-triggered sync and the reconciler's periodic sync both call
# this, unsynchronized. Without this lock, two overlapping runs for the
# SAME project race sync_dir_bidirectional -> store_upload: each writes
# its own fresh blob for a changed file, both update the same DB row
# (last writer wins), and the loser's blob is now referenced by
# nothing - an orphan on every race, forever. A non-blocking skip (never
# a blocking wait) is deliberate: this runs on an asyncio.to_thread
# worker, and a project mid-sync simply gets picked up on the next tick.
lock = _sync_lock_for(project_uid)
if not lock.acquire(blocking=False):
return dict(_SYNC_EMPTY_COUNTS)
try:
return project_files.sync_dir_bidirectional(project_uid, workspace, user)
finally:
lock.release()
async def sync_workspace(instance: dict, user: dict) -> dict:
workspace = instance.get("workspace_dir")
if not workspace:
raise ContainerError("instance has no workspace")
counts = await asyncio.to_thread(
project_files.sync_dir_bidirectional, instance["project_uid"], workspace, user
_sync_dir_bidirectional_locked, instance["project_uid"], workspace, user
)
store.record_event(
instance,
@@ -632,7 +670,7 @@ def sync_bidirectional_sync(instance: dict, user: dict) -> dict:
workspace = instance.get("workspace_dir")
if not workspace:
return {"exported": 0, "imported": 0}
counts = project_files.sync_dir_bidirectional(
counts = _sync_dir_bidirectional_locked(
instance["project_uid"], workspace, user
)
if counts["exported"] or counts["imported"]:
+19
View File
@@ -92,6 +92,25 @@ def all_instances() -> list:
return list(get_table("instances").find(deleted_at=None))
def gc_workspaces(*, dry_run: bool = False) -> int:
import shutil
from pathlib import Path
from devplacepy import config
active = {inst["project_uid"] for inst in all_instances()}
base = Path(config.CONTAINER_WORKSPACES_DIR)
if not base.is_dir():
return 0
removed = 0
for child in base.iterdir():
if child.is_dir() and child.name not in active:
if not dry_run:
shutil.rmtree(child, ignore_errors=True)
removed += 1
return removed
def find_instance_by_ingress(slug: str):
if not slug or not _exists("instances"):
return None