fix(api-server): guard json.loads against corrupted SQLite data in response cache

The ResponseStore.get() method calls json.loads(row[0]) without any
error handling. If the SQLite responses table contains corrupted JSON
data (e.g. from a crash mid-write or disk corruption), this raises
an unhandled JSONDecodeError that propagates to the caller.

Fix: wrap in try/except (json.JSONDecodeError, TypeError). On parse
failure, log a warning, evict the corrupted entry from the cache, and
return None (consistent with the function's Optional return type).
This commit is contained in:
annguyenNous
2026-06-03 17:43:39 +07:00
committed by Teknium
parent 7314757876
commit f7dabd3019

View File

@ -423,7 +423,19 @@ class ResponseStore:
(time.time(), response_id),
)
self._conn.commit()
return json.loads(row[0])
try:
return json.loads(row[0])
except (json.JSONDecodeError, TypeError):
logger.warning(
"Corrupted JSON in response store for id=%s, evicting entry",
response_id,
)
self._conn.execute(
"DELETE FROM responses WHERE response_id = ?",
(response_id,),
)
self._conn.commit()
return None
def put(self, response_id: str, data: Dict[str, Any]) -> None:
"""Store a response, evicting the oldest if at capacity."""