Repository navigation
fix: shared cross-worker first-time setup token (#1165) - #1246
Merged
Merged
Conversation
The first-time-setup token was a per-process module global, so prod (uvicorn --workers 2) generated a different token per worker — the operator copied one worker's token but POST /api/setup/admin-password load-balanced and 403'd ~50% of the time on the other worker. Surfaced once #858 made the token reach the logs at all. The token now lives in Redis (first-writer-wins SET NX, 24h TTL) so every worker shares one value, and validation reads it live at request time — eliminating the per-worker drift class entirely. When Redis is unreachable, setup is blocked, not silently degraded to a per-worker token: - GET /api/setup/status returns setup_available:false - POST /api/setup/admin-password returns 503 - the setup UI shows a "waiting for Redis" panel and polls; the token is re-issued + re-printed automatically once Redis recovers (no restart) The token is deleted from Redis on setup completion. The cached Redis client is dropped on op failure so a Redis restart self-heals. Fixes #1165
|
Resolve by running |
vybe
approved these changes
Jun 17, 2026
vybe
left a comment
Contributor
There was a problem hiding this comment.
Validated against /validate-pr methodology:
- Issue link #1165 (P1 type-bug, theme-reliability) resolves ✓
- base=dev ✓; feature-flow doc (first-time-setup.md) updated ✓; 9 new unit tests ✓
- Security: no secrets/keys/emails; no new env vars; no new top-level backend module (main.py is the existing entrypoint, modified to call shared ensure_setup_token()) ✓
- CI green incl. schema-parity + verify-non-root ✓
main.py change replaces per-worker token with shared cross-worker Redis token, printed once on issuing worker; Redis-down blocks setup and self-heals. Correct fix for the #858 follow-up flakiness.
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
uvicorn --workers 2) generated a different token per worker —POST /api/setup/admin-passwordload-balanced and 403'd ~50% of the time on the worker the operator didn't copy from. (Surfaced once bug: First-time setup token silently lost — print() block-buffered, breaks fresh installs #858 made the token reach the logs.)SET NX, 24h TTL), shared by all workers; validation reads it live at request time, eliminating the per-worker drift class entirely.Changes
src/backend/routers/setup.py—ensure_setup_token()/clear_setup_token()/_get_redis()(cached, resets on op failure);GET /statusaddssetup_available;POST /admin-passwordreturns 503 when Redis down, validates against the shared token, deletes the key on completion.src/backend/main.py— lifespan callsensure_setup_token()(issuing worker prints once; logs error if Redis down).src/frontend/src/views/SetupPassword.vue— "waiting for Redis" panel + 4s poll whensetup_available:false; handles 503; backward-compatible when the field is absent.tests/unit/test_1165_setup_token_shared.py(new, 9 tests) +test_858_dockerfile_unbuffered.py(follows the refactored emission path).docs/memory/feature-flows/first-time-setup.md— updated token model, status/validate, error table (503), security notes.Behavior on Redis down
GET /status→setup_available:false;POST /admin-password→ 503; UI shows "waiting for Redis" and polls; token is re-issued + re-printed automatically once Redis recovers — no restart.Test Plan
pytest tests/unit/test_1165_setup_token_shared.py -v(9 passed)pytest tests/unit/test_858_dockerfile_unbuffered.py(5 passed)--workers 2, confirm one token in logs validates regardless of workerAcceptance criteria (#1165)
POST /admin-passwordsucceeds regardless of worker (reads shared Redis value live)Noted (deliberate non-blocker)
Optional per-IP rate-limit on the unauthenticated
GET /status(2 Redis ops/poll, narrow pre-setup window) — skipped to keep the change minimal and avoid throttling the legit 4s frontend poll.Fixes #1165
🤖 Generated with Claude Code