mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-10-03 17:15:19 -07:00
fix startup db session leak on error (rollback + close in finally)
This commit is contained in:
+6
-5
@@ -147,10 +147,10 @@ def _register_lifecycle(application: FastAPI) -> None:
|
|||||||
init_queue()
|
init_queue()
|
||||||
|
|
||||||
# Mark stale "generating" records as failed -- leftovers from a killed process
|
# Mark stale "generating" records as failed -- leftovers from a killed process
|
||||||
try:
|
from sqlalchemy import text as sa_text
|
||||||
from sqlalchemy import text as sa_text
|
|
||||||
|
|
||||||
db = next(get_db())
|
db = next(get_db())
|
||||||
|
try:
|
||||||
result = db.execute(
|
result = db.execute(
|
||||||
sa_text(
|
sa_text(
|
||||||
"UPDATE generations SET status = 'failed', "
|
"UPDATE generations SET status = 'failed', "
|
||||||
@@ -161,7 +161,6 @@ def _register_lifecycle(application: FastAPI) -> None:
|
|||||||
if result.rowcount > 0:
|
if result.rowcount > 0:
|
||||||
logger.info("Marked %d stale generation(s) as failed", result.rowcount)
|
logger.info("Marked %d stale generation(s) as failed", result.rowcount)
|
||||||
|
|
||||||
# Log database stats
|
|
||||||
from .database import VoiceProfile as DBVoiceProfile, Generation as DBGeneration
|
from .database import VoiceProfile as DBVoiceProfile, Generation as DBGeneration
|
||||||
|
|
||||||
profile_count = db.query(DBVoiceProfile).count()
|
profile_count = db.query(DBVoiceProfile).count()
|
||||||
@@ -169,9 +168,11 @@ def _register_lifecycle(application: FastAPI) -> None:
|
|||||||
logger.info("Profiles: %d, Generations: %d", profile_count, generation_count)
|
logger.info("Profiles: %d, Generations: %d", profile_count, generation_count)
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.close()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
logger.warning("Could not clean up stale generations: %s", e)
|
logger.warning("Could not clean up stale generations: %s", e)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
backend_type = get_backend_type()
|
backend_type = get_backend_type()
|
||||||
logger.info("Backend: %s", backend_type.upper())
|
logger.info("Backend: %s", backend_type.upper())
|
||||||
|
|||||||
Reference in New Issue
Block a user