fix: prevent duplicate startup embedding jobs across workers#1286
Open
tarjan1 wants to merge 2 commits into
Open
fix: prevent duplicate startup embedding jobs across workers#1286tarjan1 wants to merge 2 commits into
tarjan1 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Prevent duplicate startup embedding initialization when SQLBot runs with
multiple Uvicorn worker processes.
Problem
Uvicorn executes the FastAPI lifespan independently in every worker process.
As a result, each worker could submit the same terminology, data-training,
table, and datasource embedding initialization jobs during application startup.
These jobs load embedding models and perform CPU-intensive calculations.
Running them repeatedly across workers can consume significant CPU and memory,
slow down application startup, and affect other services on the same host.
Process-local synchronization cannot prevent duplication across workers.
Solution
startup embedding jobs.
which is not provisioned by the current default deployment.
shutdown.
Known limitation
PostgreSQL advisory locks are local to a database. This solution coordinates
workers only when they connect to the same PostgreSQL database.
If SQLBot workers use different databases or PostgreSQL instances, they will
not share the same lock. Such deployments would require a shared external
coordinator, such as Redis.
Verification
Automated tests
The tests cover:
The changed files also pass Ruff lint, Ruff format, and
git diff --check.Multi-worker verification
The application was started against PostgreSQL 17.10 with:
Observed results:
SQLBot was running.
Scope
This change does not modify API contracts or database schemas.
Closes #1285