Feed Lambda 500 Error - SubSessionMetadata Query After Session Close
Date: 2026-05-26 Status: Solved Root Cause: Use of closed database session when querying SubSessionMetadata
Summary
MemoriesFeedFunction returns HTTP 500 after PR #534 deployment. The PR added sub_session_id grouping with SubSessionMetadata lookups, but the code queries the database session after it was already closed.
Failure Signature
- Error: HTTP 500 Internal Server Error from MemoriesFeedFunction
- Occurs after PR #534 deployment which added sub_session_id grouping
- Triggered when requesting the memories feed endpoint
- Root cause: SQLAlchemy session closed before SubSessionMetadata query attempt
Root Cause
In main/server/api/memories/feed/app.py: 1. Line 172-204: Database session is created and used to fetch WorldMMSegment data, then closed 2. Line 240: Code attempts to query SubSessionMetadata using the closed db object 3. Result: SQLAlchemy raises an error because the session is no longer active
The issue is a structural problem: the metadata query happens in the response-building loop (lines 214-276) AFTER the database session was closed at line 204.
Fix Summary
Moved the SubSessionMetadata queries inside the try block BEFORE closing the database session. The metadata is now fetched eagerly along with the segments, avoiding use of a closed session.
Changes: 1. Fetch all relevant SubSessionMetadata records for visible group keys before closing the session 2. Store them in a dict for O(1) lookup during response building 3. Remove the db.query() call after session close
Verification
Test: Query the memories feed endpoint after fix deployment - Expected: Returns valid feed data with sub_session titles where available - Before fix: Would error on SubSessionMetadata lookup - After fix: Completes successfully