fix(db): stabilize connection refreshing and prevent ResourceClosedError

- Update sql_connect to refresh global db object via reconnect_db
- Add returns_rows check and safe fetch block in sql_select
- Prevents 500 errors during transient database connection issues
This commit is contained in:
Scott Idem
2026-01-21 12:49:47 -05:00
parent b2ee1f2760
commit 89bf87cb62
2 changed files with 21 additions and 8 deletions

View File

@@ -291,7 +291,18 @@ def sql_select(
return [] if as_list else None
# Fetch all rows first to determine actual count reliably
rows = result.all()
try:
# Check if the result set actually contains rows before fetching
if hasattr(result, 'returns_rows') and not result.returns_rows:
log.warning("SQL Result does not return rows (ResourceClosedError prevented).")
return [] if as_list else None
rows = result.all()
except Exception as e:
log.error(f"SQL Fetch Error: {e}")
set_last_sql_error(e)
return [] if as_list else None
count = len(rows)
if count == 0: