Merge pull request 'fix(api): document dispose_engine lazy import + regression test (CAR-1135)' (#45) from barcode-betty/fix-car-1135-dispose-engine into dev
CI / lint (push) Successful in 4s
CI / typecheck (push) Successful in 19s
CI / test (push) Successful in 23s
CI / build-and-push (push) Successful in 1m30s

This commit was merged in pull request #45.
This commit is contained in:
2026-06-10 05:13:18 +00:00
4 changed files with 23 additions and 3 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ class CacheClient:
return None
if isinstance(value, bytes):
return value.decode("utf-8", errors="replace")
return value
return str(value)
async def set(self, key: str, value: str, ttl_seconds: int = 300) -> None:
if not self._client:
+6
View File
@@ -25,6 +25,12 @@ from cartsnitch_api.routes.user import router as user_router
@asynccontextmanager
async def lifespan(app: FastAPI):
# Lazy import: keep `dispose_engine` out of the top-level imports so a
# stale or partially-built database.py never breaks module load on
# container start. The function is required for graceful pool cleanup
# on shutdown; if the import fails, the cache_client.close() that
# follows the yield would mask it. See CAR-1135 for the original
# ImportError that motivated this pattern.
from cartsnitch_api.database import dispose_engine
await cache_client.initialize()
+2 -2
View File
@@ -147,8 +147,8 @@ def _get_client_ip(request: Request) -> str:
"""Extract client IP, respecting X-Forwarded-For behind a reverse proxy."""
forwarded = request.headers.get("x-forwarded-for")
if forwarded:
return forwarded.split(",")[0].strip()
return request.client.host if request.client else "unknown"
return str(forwarded.split(",")[0].strip())
return str(request.client.host) if request.client else "unknown"
def _get_rate_limit_key(request: Request) -> tuple[str, RateLimitBackend]:
+14
View File
@@ -3,8 +3,22 @@
import pytest
from httpx import ASGITransport, AsyncClient
from cartsnitch_api.database import dispose_engine
from cartsnitch_api.main import app
def test_dispose_engine_importable_from_database():
"""Regression for CAR-1135: api main.py used to import dispose_engine
at module level. A stale database.py (no dispose_engine) crashed the
container at import time with ImportError on line 9. The fix moved
the import inside the lifespan function, but `dispose_engine` must
still be importable from `cartsnitch_api.database` for the lifespan
teardown to actually close pooled connections.
"""
assert callable(dispose_engine)
assert dispose_engine.__name__ == "dispose_engine"
EXPECTED_ROUTES = [
# Auth (3 — register/login/refresh are handled by Better-Auth service)
("get", "/auth/me"),