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:
@ -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."""
|
||||
|
||||
Reference in New Issue
Block a user