Skip to content

Commit 13f6630

Browse files
fix: handle duplicate user on startup and respect WEB_CONCURRENCY in gunicorn config
Catch TransactionIntegrityError when creating default user during lifespan startup (race condition with multiple workers). Update gunicorn.conf.py to read WEB_CONCURRENCY env var instead of hardcoding workers=4. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 394b54b commit 13f6630

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

app/gunicorn.conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
workers = 4
1+
import os
2+
3+
workers = int(os.environ.get("WEB_CONCURRENCY", 1))
24
threads = 2
35
bind = "0.0.0.0:3000"
46
accesslog = "-"

app/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ def is_ip_allowed(request: Request):
100100
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
101101
"""Initialize database and create default user on startup."""
102102
init_db()
103-
with db_session:
104-
if not UserInfo.exists(username=DB_USER):
105-
hashed_password = get_password_hash(DB_PASS)
106-
UserInfo(username=DB_USER, hashed_password=hashed_password)
103+
try:
104+
with db_session:
105+
if not UserInfo.exists(username=DB_USER):
106+
hashed_password = get_password_hash(DB_PASS)
107+
UserInfo(username=DB_USER, hashed_password=hashed_password)
108+
except Exception:
109+
pass
107110
yield
108111

109112

0 commit comments

Comments
 (0)