NanPan 36b124c7fc
fix(persistence): drain alembic workers across cancellation (#5631)
* fix(persistence): drain alembic workers across cancellation

* docs(persistence): clarify alembic cancellation ownership

* docs(persistence): align alembic helper ownership contract

* fix(persistence): restore docstring underline
2026-09-22 22:17:11 +08:00

1007 B

Persistence lifecycle

Postgres bootstrap owns its session-scoped advisory lock until pg_advisory_unlock completes. Drain that unlock across host cancellation before leaving the SQLAlchemy connection context; repeated cancellation must not return a pooled session while it still holds the bootstrap mutex. Ordinary database errors remain best-effort and are logged.

When database.postgres_schema is configured, both async ORM connections and the synchronous SQLAlchemy connections used by DB-backed custom agents and managed subagents must use the same search_path; preserve this invariant when adding another persistence entry point.

Alembic stamp/upgrade workers started inside bootstrap_schema() remain owned by the bootstrap critical section until the worker finishes. Drain those asyncio.to_thread() calls across host cancellation before releasing the in-process SQLite bootstrap lock or PostgreSQL advisory lock; otherwise another bootstrap can overlap a still-running migration worker.