Type
bug
Severity
medium
Area
persistence.py — load_json_document()
Description
load_json_document accepts a default parameter suggesting it should return a safe fallback on failure, but json.loads() will raise JSONDecodeError on corrupt files (e.g., from a partial write during a crash or power loss):
def load_json_document(path: Path, default: Any) -> Any:
raw = _read_text(path)
if raw is None:
return default
return json.loads(raw) # Raises on invalid JSON — no fallback
Since scan metadata, settings, and customer configs all use this function, a single corrupt JSON file can crash the entire application on startup.
Proposed Fix
def load_json_document(path: Path, default: Any) -> Any:
raw = _read_text(path)
if raw is None:
return default
try:
return json.loads(raw)
except json.JSONDecodeError:
logger.warning("Corrupt JSON at %s — using default", path)
return default
Related Issues
None