Back to Landing Page
Sample Platform · Python

SQLAlchemy Lazy-Load Crash Chain

May 20267 min read
# sample-platform#1118
# grep -n "_emit_lazyload" error.log | wc -l  ->  5697

# fix bug 1: eager-load Category.regression_tests
category_q = db.query(Category).options(
    selectinload(Category.regression_tests)
)
# fix bug 3: null guard at line 94
if result is None or result.exit_code == result.expected_rc:
    # check multiple_files variants

Background

The CCExtractor Sample Platform was producing HTTP 500 errors on /test/ pages at high volume. No single PR had been identified as the cause.

Root Cause

Queried the production VM database directly. Found 5,697 lazy-load stack frames and 57 AttributeError crashes concentrated on /test/ routes. SQLAlchemy was lazy-loading relations inside a closed session context. Additionally, exit-code comparison was wrong for tests with non-zero expected_rc — == 0 was used instead of == expected_rc, masking failures.

The Fix

Replaced lazy loading with selectinload and joinedload eager loading on the affected queries. Added a null guard before the attribute access that was throwing AttributeError. Fixed exit-code comparison logic.

Evidence

Before/after query on production DB showed 3,087 HTTP 500s eliminated. Stack trace count dropped to zero in post-deploy logs.

Result

PR sample-platform#1118. Evidence-first diagnosis — no fix was proposed until the production query confirmed root cause.