command.upgrade hangs with uvicorn and fastapi #1722
Replies: 3 comments 5 replies
-
hi - there's no bug here in alembic. Hangs can be caused by PG table locks, or if your application / fastAPI has some kind of locking going on. It seems significant that your app seems to have something else going on with the database based on the message "2025-09-03 17:27:07,833 - app.database.init_db - INFO - init_db.py:27 - Database tables already exist", that's not anything Alembic does, so your issue may be related to whatever is the source of that message. |
Beta Was this translation helpful? Give feedback.
-
So I managed to get logs at the very end of await asyncio.to_thread(command.upgrade, cfg, "head") |
Beta Was this translation helpful? Give feedback.
-
Somewhere else in the code, we create an engine for all of our database operations: # app/database/connection.py
from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine(DATABASE_URL, echo=False, future=True) I changed I tried re-using this engine: from app.database.connection import engine
...
alembic_cfg = Config("alembic.ini")
async with engine.begin() as connection:
alembic_cfg.attributes["connection"] = connection
await asyncio.to_thread(command.upgrade, alembic_cfg, "head") Alas it did not give different results, but the fact that I get a constant stream of operations in my log... Seems somewhat fishy. It seems that I have things to investigate on my own side. Thank you for your time and helpful insights. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
I am trying to run a FastAPI server but the startup process hangs on the migration.
Expected behavior
I should see the confirmation in my logs that the migration has completed and then the rest of the startup process going on with the API running:
This works fine when I comment the
command.upgrade
call.To Reproduce
Versions.
Additional context
This seems very similar to #1155 but the solution of adding
disable_existing_loggers=False
to thefileConfig
call inenv.py
did not function for me.I have not modified the env.py
When running migrations from the command line I have the same logs but the process finishes normally with a 0 exit code
Beta Was this translation helpful? Give feedback.
All reactions