Bug Fix where - in exceptional cases - a connection without correct search path could be used (out of the connection pool).

This commit is contained in:
Josako
2025-10-24 11:42:50 +02:00
parent a43825f5f0
commit b3ee2f7ce9
3 changed files with 115 additions and 12 deletions

View File

@@ -6,6 +6,7 @@ from dataclasses import dataclass
from flask import current_app
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import joinedload
from sqlalchemy import text
from common.extensions import db, cache_manager
from common.models.interaction import ChatSession, Interaction
@@ -111,6 +112,14 @@ class ChatSessionCacheHandler(CacheHandler[CachedSession]):
Note:
Only adds the interaction if it has an answer
"""
# Log connection context right before any potential lazy load of interaction properties
try:
sp = db.session.execute(text("SHOW search_path")).scalar()
cid = id(db.session.connection().connection)
current_app.logger.info(f"DBCTX before_lazy_load conn_id={cid} search_path={sp}")
except Exception:
pass
if not interaction.specialist_results:
return # Skip incomplete interactions

View File

@@ -351,16 +351,27 @@ def execute_specialist(self, tenant_id: int, specialist_id: int, arguments: Dict
return response
except Exception as e:
# Ensure DB session is usable after an error
try:
db.session.rollback()
except Exception:
pass
stacktrace = traceback.format_exc()
ept.send_update(task_id, "EveAI Specialist Error", {'Error': str(e)})
current_app.logger.error(f'execute_specialist: Error executing specialist: {e}\n{stacktrace}')
new_interaction.processing_error = str(e)[:255]
try:
db.session.add(new_interaction)
db.session.commit()
except SQLAlchemyError as e:
stacktrace = traceback.format_exc()
current_app.logger.error(f'execute_specialist: Error updating interaction: {e}\n{stacktrace}')
if new_interaction is not None:
new_interaction.processing_error = str(e)[:255]
try:
db.session.add(new_interaction)
db.session.commit()
except SQLAlchemyError as e:
# On failure to update, rollback and log
try:
db.session.rollback()
except Exception:
pass
stacktrace = traceback.format_exc()
current_app.logger.error(f'execute_specialist: Error updating interaction: {e}\n{stacktrace}')
self.update_state(state=states.FAILURE)
raise