Commit 1e0793a2 by sunqi02 Committed by 胡悦

fix: lifespan no longer holds long-lived SQLAlchemy session

Inject SessionLocal as factory into ActiveSessionService.
Short-lived sessions via with db_factory() as db: prevent
Broken pipe from stale connections.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent e7582531
...@@ -65,17 +65,18 @@ async def lifespan(app: FastAPI): ...@@ -65,17 +65,18 @@ async def lifespan(app: FastAPI):
config_loader = ConfigLoader() config_loader = ConfigLoader()
settings = config_loader.load_settings() settings = config_loader.load_settings()
db = SessionLocal() db_factory = SessionLocal
# 从数据库加载 Agent 配置 # 从数据库加载 Agent 配置
agents = config_loader.load_agents_from_db(db) with db_factory() as db:
agents = config_loader.load_agents_from_db(db)
logger.info( logger.info(
"Agent 配置已从 DB 加载: %d 个, steward=%s", "Agent 配置已从 DB 加载: %d 个, steward=%s",
len(agents), len(agents),
next((k for k, v in agents.items() if v.is_steward), None), next((k for k, v in agents.items() if v.is_steward), None),
) )
session_service = ActiveSessionService(db=db) session_service = ActiveSessionService(db=db_factory)
use_console_im = os.getenv("USE_CONSOLE_IM") use_console_im = os.getenv("USE_CONSOLE_IM")
if use_console_im is None: if use_console_im is None:
...@@ -118,7 +119,7 @@ async def lifespan(app: FastAPI): ...@@ -118,7 +119,7 @@ async def lifespan(app: FastAPI):
await agent_hub_instance.close() await agent_hub_instance.close()
if message_gate_instance: if message_gate_instance:
await message_gate_instance.close() await message_gate_instance.close()
db.close() # db_factory 是 class,不需要 close;短 session 由 with 自动管理
app = FastAPI( app = FastAPI(
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!