Skip to content

fix: shared cross-worker first-time setup token (#1165) - #1246

Merged
vybe merged 1 commit into
devfrom
feature/1165-shared-setup-token
Jun 17, 2026
Merged

vybe merged 1 commit into
devfrom
feature/1165-shared-setup-token

Conversation

@obasilakis

Copy link
Copy Markdown
Contributor

Summary

  • The first-time-setup token was a per-process module global, so prod (uvicorn --workers 2) generated a different token per worker — POST /api/setup/admin-password load-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.)
  • Token now lives in Redis (first-writer-wins SET NX, 24h TTL), shared by all workers; 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 — and self-heals when Redis returns (no restart).

Changes

  • src/backend/routers/setup.py — ensure_setup_token() / clear_setup_token() / _get_redis() (cached, resets on op failure); GET /status adds setup_available; POST /admin-password returns 503 when Redis down, validates against the shared token, deletes the key on completion.
  • src/backend/main.py — lifespan calls ensure_setup_token() (issuing worker prints once; logs error if Redis down).
  • src/frontend/src/views/SetupPassword.vue — "waiting for Redis" panel + 4s poll when setup_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

  • New unit tests pass: pytest tests/unit/test_1165_setup_token_shared.py -v (9 passed)
  • pytest tests/unit/test_858_dockerfile_unbuffered.py (5 passed)
  • Code review (correctness + cleanup) — 4 findings applied
  • Security review — no critical/high
  • Manual: fresh install with --workers 2, confirm one token in logs validates regardless of worker

Acceptance criteria (#1165)

  • All workers report the same token (first-writer-wins; only issuer logs)
  • POST /admin-password succeeds regardless of worker (reads shared Redis value live)
  • Single-use (delete-on-complete) + constant-time compare retained
  • Dev (1 worker) unchanged

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

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
@github-actions

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@vybe
vybe merged commit a2f6310 into dev Jun 17, 2026
18 checks passed
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.

2 participants