Skip to content

Bug: persistence.py load_json_document crashes on corrupt JSON files instead of returning default #205

@techmore

Description

@techmore

Type

bug

Severity

medium

Area

persistence.pyload_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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions