fix: address code review findings

- Add retry (2 attempts) on CivitAI 429 rate limit instead of silently skipping
- Use check_same_thread=False for SQLite connection (aiohttp context)
- Add debug logging to silent exception handlers in proxy

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-15 14:44:12 +01:00
parent 63ddaaa70d
commit f6e39df157
3 changed files with 24 additions and 21 deletions
+4 -4
View File
@@ -599,8 +599,8 @@ async def lm_remote_proxy_middleware(request: web.Request, handler):
body = json.loads(response.body) body = json.loads(response.body)
enriched = _enrich_list_response(body) enriched = _enrich_list_response(body)
return web.json_response(enriched) return web.json_response(enriched)
except Exception: except Exception as exc:
pass # Return original response on any error logger.debug("[LM-Remote] Stats enrichment failed: %s", exc)
# Inject stats UI script into HTML pages # Inject stats UI script into HTML pages
if (path in _PROXY_PAGE_ROUTES or path.rstrip("/") in _PROXY_PAGE_ROUTES): if (path in _PROXY_PAGE_ROUTES or path.rstrip("/") in _PROXY_PAGE_ROUTES):
if response.status == 200: if response.status == 200:
@@ -616,8 +616,8 @@ async def lm_remote_proxy_middleware(request: web.Request, handler):
charset="utf-8", charset="utf-8",
status=response.status, status=response.status,
) )
except Exception: except Exception as exc:
pass logger.debug("[LM-Remote] HTML injection failed: %s", exc)
return response return response
# Not a LoRA Manager route — fall through # Not a LoRA Manager route — fall through
+1 -1
View File
@@ -32,7 +32,7 @@ class StatsDB:
def init(self) -> None: def init(self) -> None:
"""Create the database and table if they don't exist.""" """Create the database and table if they don't exist."""
self._conn = sqlite3.connect(str(self._db_path)) self._conn = sqlite3.connect(str(self._db_path), check_same_thread=False)
self._conn.row_factory = sqlite3.Row self._conn.row_factory = sqlite3.Row
self._conn.execute("PRAGMA journal_mode=WAL") self._conn.execute("PRAGMA journal_mode=WAL")
self._conn.executescript(_SCHEMA) self._conn.executescript(_SCHEMA)
+7 -4
View File
@@ -72,16 +72,18 @@ class StatsFetchService:
) )
return self._session return self._session
async def _fetch_model(self, model_id: int) -> dict | None: async def _fetch_model(self, model_id: int, _retries: int = 2) -> dict | None:
"""Fetch a single model's data from CivitAI API.""" """Fetch a single model's data from CivitAI API."""
url = f"{_CIVITAI_API}/models/{model_id}" url = f"{_CIVITAI_API}/models/{model_id}"
session = await self._get_session() session = await self._get_session()
for attempt in range(_retries + 1):
try: try:
async with session.get(url) as resp: async with session.get(url) as resp:
if resp.status == 429: if resp.status == 429:
logger.warning("[LM-Remote] CivitAI rate limited, backing off") logger.warning("[LM-Remote] CivitAI rate limited, backing off (attempt %d)",
await asyncio.sleep(5) attempt + 1)
return None await asyncio.sleep(5 * (attempt + 1))
continue
if resp.status != 200: if resp.status != 200:
logger.debug("[LM-Remote] CivitAI returned %d for model %d", logger.debug("[LM-Remote] CivitAI returned %d for model %d",
resp.status, model_id) resp.status, model_id)
@@ -91,6 +93,7 @@ class StatsFetchService:
logger.warning("[LM-Remote] CivitAI fetch failed for model %d: %s", logger.warning("[LM-Remote] CivitAI fetch failed for model %d: %s",
model_id, exc) model_id, exc)
return None return None
return None
async def fetch_stats_for_models( async def fetch_stats_for_models(
self, self,