Repository navigation
Description
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.ymlline 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:
POST /voice/start→ worker A stores session in worker A's_sessionsGET /ws/voice/{id}?token=JWT→ worker B has empty_sessions→get_session()returnsNone→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
- Deploy with
--workers 2(production default indocker-compose.prod.yml) - Start a voice session:
POST /api/agents/{name}/voice/start - Immediately connect WebSocket:
GET /ws/voice/{session_id}?token=<JWT> - 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 NoneOption 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 120Note: 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.ymlwith--workers 2
Related
src/backend/services/gemini_voice.py—GeminiVoiceService._sessionssrc/backend/routers/voice.py—voice_websocket()handlerdocker-compose.prod.yml— backend command
Activity
vybe commentedon May 7, 2026
/claim
github-actions commentedon May 7, 2026
Claimed by @vybe. Label
status-in-progressadded.Next steps:
feature/704-<short-slug>Fixes #704fix(voice): cross-worker session 403 + audit kwargs TypeError (#704 #705
vybe commentedon May 10, 2026
Fixed in PR #706 (e17ac8f on dev). Cross-worker session 403 resolved via Redis-backed session registry replacing the in-memory dict.
vybe commentedon Jun 1, 2026
Shipped in v0.6.0 (released via #998).
Release: v0.6.1 (#1171)