Skip to content

bug: voice WebSocket 403 — in-memory session lost across Uvicorn workers #704

Closed

Description

@vybe

Summary

The voice WebSocket endpoint (/ws/voice/{id}) returns HTTP 403 approximately 50% of the time. The POST /api/agents/{name}/voice/start REST call succeeds and creates a session, but the subsequent WebSocket connection hits a different Uvicorn worker that has no knowledge of the session. The session is stored in an in-memory dict on GeminiVoiceService, which is not shared across workers.

Component

Backend / Voice Chat (VOICE-001)

Priority

P1

Error

INFO: "WebSocket /ws/voice/{session_id}?token=<JWT>" 403

Browser JS console:

WebSocket connection to 'wss://[HOST]/ws/voice/{session_id}?token=<JWT>' failed

Location

  • File: src/backend/services/gemini_voice.py
  • Line: ~176 — self._sessions: dict[str, VoiceSession] = {}
  • File: src/backend/routers/voice.py
  • Line: ~232 — session = voice_service.get_session(voice_session_id)
  • Config: docker-compose.prod.yml line 83 — --workers 2

Root Cause

GeminiVoiceService._sessions is a plain in-memory Python dict on the module-level singleton. With --workers 2 in production, each Uvicorn worker process gets its own copy. Round-robin dispatch means:

  1. POST /voice/start → worker A stores session in worker A's _sessions
  2. GET /ws/voice/{id}?token=JWT → worker B has empty _sessions → get_session() returns None → websocket.close(4004) before accept → HTTP 403

Failure rate is ~50% under 2-worker config. Token/JWT is valid — the auth check is never reached.

Reproduction Steps

  1. Deploy with --workers 2 (production default in docker-compose.prod.yml)
  2. Start a voice session: POST /api/agents/{name}/voice/start
  3. Immediately connect WebSocket: GET /ws/voice/{session_id}?token=<JWT>
  4. Observe 403 approximately every other attempt

Suggested Fix

Move voice session storage out of process-local memory into a shared store. Two options:

Option A (recommended): Store sessions in Redis with a short TTL (e.g. 5 minutes). Session data is small (IDs, user_id, agent_name, state enum).

# In GeminiVoiceService.create_session():
await redis_client.setex(f"voice_session:{session_id}", 300, session.model_dump_json())

# In GeminiVoiceService.get_session():
data = await redis_client.get(f"voice_session:{session_id}")
return VoiceSession.model_validate_json(data) if data else None

Option B (quick workaround): Drop to --workers 1 in docker-compose.prod.yml. Removes concurrency benefit but sessions are consistent.

command: uvicorn main:app --host 0.0.0.0 --port 8000 --workers 1 --timeout-worker-healthcheck 120

Note: The active audio streaming state (Gemini Live API connection, transcript buffer) still needs to live in the worker handling the WebSocket long-lived connection — only the session metadata needs to be shared for the initial auth/lookup step.

Environment

  • Trinity version: f7450ca (feat(voice): agent workspace + canvas panel)
  • Docker: production docker-compose.prod.yml with --workers 2

Related

  • src/backend/services/gemini_voice.py — GeminiVoiceService._sessions
  • src/backend/routers/voice.py — voice_websocket() handler
  • docker-compose.prod.yml — backend command

Activity

  1. vybe commented on May 7, 2026

    @vybe
    ContributorAuthor

    /claim

  2. added
    status-in-progress
    on May 7, 2026
  3. github-actions commented on May 7, 2026

    @github-actions

    Claimed by @vybe. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/704-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #704
  4. added
    status-in-dev
    and removed
    status-in-progress
    on May 7, 2026
  5. vybe commented on May 10, 2026

    @vybe
    ContributorAuthor

    Fixed in PR #706 (e17ac8f on dev). Cross-worker session 403 resolved via Redis-backed session registry replacing the in-memory dict.

  6. reopened this on May 10, 2026
  7. vybe commented on Jun 1, 2026

    @vybe
    ContributorAuthor

    Shipped in v0.6.0 (released via #998).

  8. removed
    status-in-dev
    on Jun 1, 2026
  9. added a commit that references this issue on Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p1type-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions