Skip to content

Conversation

@neomorphic
Copy link
Member

When running uvicorn with multiple workers (--workers 10), all worker
processes were attempting to run alembic migrations simultaneously,
causing race conditions and errors.

Implemented file-based locking using fcntl to ensure only one process
runs migrations while other workers wait. The lock file is created in
the system temp directory with a hash of the database URL to support
multiple databases.

Also removed the redundant global _migrations_run flag which only
prevented duplicate runs within a single process, not across multiple
worker processes.

Added test-multi-worker task to pyproject.toml for testing with 10 workers.

@krokicki

When running uvicorn with multiple workers (--workers 10), all worker
processes were attempting to run alembic migrations simultaneously,
causing race conditions and errors.

Implemented file-based locking using fcntl to ensure only one process
runs migrations while other workers wait. The lock file is created in
the system temp directory with a hash of the database URL to support
multiple databases.

Also removed the redundant global _migrations_run flag which only
prevented duplicate runs within a single process, not across multiple
worker processes.

Added test-multi-worker task to pyproject.toml for testing with 10 workers.
@neomorphic
Copy link
Member Author

I am on the fence about including the test-multi-worker task, but added it so others can have their say.

@neomorphic neomorphic requested a review from krokicki January 23, 2026 16:12
Copy link
Member

@krokicki krokicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the idea, but I think this is adding complexity to solve a problem that maybe doesn't need to exist. Running migrations automatically on server startup was already questionable, and now we're adding locking mechanisms on top of it, and then later if we go multi-node a local file won't work so we'll need to change it again.

Could we just move migration to a separate manual step and keep things simple?

@neomorphic
Copy link
Member Author

sure

@neomorphic
Copy link
Member Author

Closing this with the idea that we move all database migration to a manual process that is run when a new version of the server code is pulled down.

@neomorphic neomorphic closed this Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants