Isolate STL analysis in child process; fix 3MF memory usage
- _load_geometry(): compute extents/volume from individual Scene meshes without concatenating — avoids the RAM spike that crashed the container - analyse_in_background(): spawns a child process (spawn context, not fork) with a 120s timeout; OOM/crash in child no longer takes down the app - _failed set: tracks files that timed out or crashed; spinner stops and a warning with inline override form is shown instead of looping forever - analysis_failed state passed to template; override form shown immediately when analysis failed (no expand needed) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
"""STL/3MF geometry analysis with background process isolation."""
|
||||
import logging
|
||||
import multiprocessing as _mp
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.stl_cache import StlCache
|
||||
@@ -8,7 +11,27 @@ from app.models.stl_cache import StlCache
|
||||
logger = logging.getLogger("pops")
|
||||
|
||||
_ANALYSABLE = {".stl", ".3mf"}
|
||||
_TIMEOUT = 120 # seconds before we give up
|
||||
|
||||
_in_progress: set[str] = set()
|
||||
_failed: set[str] = set()
|
||||
|
||||
|
||||
def _load_geometry(filepath: str):
|
||||
"""Return (extents_array, volume_mm3). Handles Scene without concatenating."""
|
||||
import trimesh
|
||||
loaded = trimesh.load(filepath)
|
||||
if isinstance(loaded, trimesh.Scene):
|
||||
meshes = list(loaded.geometry.values())
|
||||
if not meshes:
|
||||
raise ValueError("3MF scene contains no meshes")
|
||||
bounds = np.array([m.bounds for m in meshes]) # (N, 2, 3)
|
||||
extents = bounds[:, 1, :].max(axis=0) - bounds[:, 0, :].min(axis=0)
|
||||
volume_mm3 = sum(abs(float(m.volume)) for m in meshes)
|
||||
else:
|
||||
extents = np.array(loaded.extents)
|
||||
volume_mm3 = abs(float(loaded.volume))
|
||||
return extents, volume_mm3
|
||||
|
||||
|
||||
def analyse(filename: str, db: Session) -> StlCache | None:
|
||||
@@ -23,14 +46,7 @@ def analyse(filename: str, db: Session) -> StlCache | None:
|
||||
return None
|
||||
|
||||
try:
|
||||
import trimesh
|
||||
loaded = trimesh.load(str(filepath))
|
||||
if isinstance(loaded, trimesh.Scene):
|
||||
mesh = trimesh.util.concatenate(list(loaded.geometry.values()))
|
||||
else:
|
||||
mesh = loaded
|
||||
extents = mesh.extents # [size_x, size_y, size_z] in mm
|
||||
volume_mm3 = abs(mesh.volume) # mm³ (negative for inverted normals)
|
||||
extents, volume_mm3 = _load_geometry(str(filepath))
|
||||
entry = StlCache(
|
||||
filename=filename,
|
||||
cube_x=round(float(extents[0]), 2),
|
||||
@@ -46,18 +62,38 @@ def analyse(filename: str, db: Session) -> StlCache | None:
|
||||
return None
|
||||
|
||||
|
||||
def _worker(filename: str) -> None:
|
||||
"""Child process entry point — creates its own DB session."""
|
||||
from app.db import SessionLocal
|
||||
db = SessionLocal()
|
||||
try:
|
||||
analyse(filename, db)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def analyse_in_background(filename: str) -> None:
|
||||
"""Run analyse() in a background task with its own DB session."""
|
||||
"""Spawn an isolated child process for analysis; kill it after _TIMEOUT."""
|
||||
if filename in _in_progress:
|
||||
return
|
||||
_in_progress.add(filename)
|
||||
_failed.discard(filename)
|
||||
try:
|
||||
from app.db import SessionLocal
|
||||
db = SessionLocal()
|
||||
try:
|
||||
analyse(filename, db)
|
||||
finally:
|
||||
db.close()
|
||||
ctx = _mp.get_context("spawn")
|
||||
p = ctx.Process(target=_worker, args=(filename,), daemon=True)
|
||||
p.start()
|
||||
p.join(timeout=_TIMEOUT)
|
||||
if p.is_alive():
|
||||
logger.warning("STL analysis timed out for %s — killing process", filename)
|
||||
p.kill()
|
||||
p.join()
|
||||
_failed.add(filename)
|
||||
elif p.exitcode != 0:
|
||||
logger.warning("STL analysis process exited with code %s for %s", p.exitcode, filename)
|
||||
_failed.add(filename)
|
||||
except Exception:
|
||||
logger.exception("STL background task error for %s", filename)
|
||||
_failed.add(filename)
|
||||
finally:
|
||||
_in_progress.discard(filename)
|
||||
|
||||
@@ -66,6 +102,10 @@ def is_computing(filename: str) -> bool:
|
||||
return filename in _in_progress
|
||||
|
||||
|
||||
def is_failed(filename: str) -> bool:
|
||||
return filename in _failed
|
||||
|
||||
|
||||
def get_cache_map(filenames: list[str], db: Session) -> dict[str, StlCache]:
|
||||
"""Bulk-fetch cached entries for a list of filenames."""
|
||||
rows = db.query(StlCache).filter(StlCache.filename.in_(filenames)).all()
|
||||
|
||||
Reference in New Issue
Block a user