From f7dabd3019fa46d7234abeabd4e175784500e266 Mon Sep 17 00:00:00 2001 From: annguyenNous Date: Wed, 3 Jun 2026 17:43:39 +0700 Subject: [PATCH] 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). --- gateway/platforms/api_server.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py index 22bf199b3..42ff0b581 100644 --- a/gateway/platforms/api_server.py +++ b/gateway/platforms/api_server.py @@ -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."""