diff --git a/app/routers/api.py b/app/routers/api.py index e53694b..69320d5 100644 --- a/app/routers/api.py +++ b/app/routers/api.py @@ -4,15 +4,15 @@ from typing import Dict, List, Optional, Set, Union from sqlalchemy import text import json import time -import secrets +# import secrets import jwt as pyjwt # Avoid conflict with app.lib_jwt -from app.db_connection import db +# from app.db_connection import db from app.lib_general import sign_jwt, decode_jwt, log, logging from app.config import settings from app.db_sql import sql_insert, sql_update, sql_select, redis_lookup_id_random, get_id_random -from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template +# from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template from app.routers.dependencies_v3 import DeprecationParams from app.models.api_models import Api_Base from app.models.response_models import Resp_Body_Base, mk_resp diff --git a/documentation/TODO__Agents.md b/documentation/TODO__Agents.md index d7507c0..457734f 100644 --- a/documentation/TODO__Agents.md +++ b/documentation/TODO__Agents.md @@ -12,6 +12,15 @@ - [x] **Config Refactor:** Switch `app/config.py` to `pydantic-settings` to use direct Env Vars (Stop mounting config files). - [x] **Locking:** Generate a `requirements.lock` for bit-identical builds. +## 🔌 DB Connection Hardening (April 2026 Audit) +> Identified during pre-show review. Issues 1 and 2 likely explain observed random connection lags. + +- [ ] **[P1] Remove zombie `db_connection.py` import** — `app/routers/api.py` imports `db` from `app/db_connection.py`, creating a parasitic second SQLAlchemy engine at startup that is never updated by `reconnect_db()` after bootstrap. The imported `db` is only used in a commented-out line (`api.py:268`). Fix: remove the import; delete or archive `db_connection.py`. +- [ ] **[P1] Fix retry mechanism in `sql_update` / `run_sql_select`** — On `OperationalError`, both call `sql_connect()` → `reconnect_db()` which calls `engine.dispose()`, nuking the entire connection pool mid-flight. Under concurrent requests this kills other in-flight connections. Fix: remove the `sql_connect()` retry call; SQLAlchemy's `pool_pre_ping=True` already handles stale connections — just open a fresh `engine.connect()` for the retry without disposing the pool. +- [ ] **[P2] Add retry logic to `sql_insert` and `sql_select`** — Both are missing the `OperationalError` retry that `sql_update` and `run_sql_select` have. A DB blip during an INSERT (scan record, badge log, etc.) fails silently and returns `False` with no recovery attempt. +- [ ] **[P3] Guard `db = engine.connect()` in `lib_sql_core.py` with try/except** — Line 48 is a bare connect at module load time with no error handling. If MariaDB isn't ready (Docker race), this throws unhandled and crashes the worker. Wrap in try/except like `db_connection.py` already does. +- [ ] **[P4] Expose `pool_size` / `max_overflow` as env vars** — `create_ae_engine()` calls `settings.DB.get('pool_size', 10)` but `settings.DB` property doesn't include those keys, so they're always hardcoded 10/20. Add `AE_DB_POOL_SIZE` / `AE_DB_POOL_MAX_OVERFLOW` to `config.py`. + ## 📋 Feature Tasks - [x] **Core Isolation:** Harden `apply_forced_account_filter` to Fail-Closed. - [x] **IDAA Baseline:** Remove `public_read` from Event, CMS, and Archive objects.